Skip to content

Commit 784b526

Browse files
committed
Variables cleanup: BoolVariable
Part 1 of a series, updating the BoolVariable implementation, tests and docstrings. Note some of the test strings will change again when the change to the "main" module (Variables/__init__.py) change lands. Signed-off-by: Mats Wichmann <[email protected]>
1 parent 3b7d6bc commit 784b526

File tree

5 files changed

+29
-36
lines changed

5 files changed

+29
-36
lines changed

CHANGES.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ RELEASE VERSION/DATE TO BE FILLED IN LATER
4949
build pdf versions which are then ignored.
5050
- Add the ability to print a Variables object for debugging purposes
5151
(provides a __str__ method in the class).
52-
- Update manpage and user guide for Variables usage.
52+
- Clean up Variables: more consistently call them variables (finish the
53+
old change from Options) in docstrings, etc.; some typing and other
54+
tweaks. Update manpage and user guide for Variables usage.
5355

5456

5557
RELEASE 4.7.0 - Sun, 17 Mar 2024 17:22:20 -0700

RELEASE.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ CHANGED/ENHANCED EXISTING FUNCTIONALITY
3939
NOTE: With this change, user created Actions should now catch and handle
4040
expected exceptions (whereas previously many of these were silently caught
4141
and suppressed by the SCons Action exection code).
42+
- The implementation of Variables was slightly refactored, there should
43+
not be user-visible changes.
4244

4345
FIXES
4446
-----

SCons/Variables/BoolVariable.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
2828
opts = Variables()
2929
opts.Add(BoolVariable('embedded', 'build for an embedded system', False))
30-
...
30+
env = Environment(variables=opts)
3131
if env['embedded']:
3232
...
3333
"""
@@ -54,13 +54,13 @@ def _text2bool(val: str) -> bool:
5454
Raises:
5555
ValueError: if *val* cannot be converted to boolean.
5656
"""
57-
5857
lval = val.lower()
5958
if lval in TRUE_STRINGS:
6059
return True
6160
if lval in FALSE_STRINGS:
6261
return False
63-
raise ValueError("Invalid value for boolean option: %s" % val)
62+
# TODO: leave this check to validator?
63+
raise ValueError(f"Invalid value for boolean variable: {val!r}")
6464

6565

6666
def _validator(key, val, env) -> None:
@@ -73,23 +73,25 @@ def _validator(key, val, env) -> None:
7373
Raises:
7474
KeyError: if *key* is not set in *env*
7575
UserError: if the value of *key* is not ``True`` or ``False``.
76-
"""
77-
if not env[key] in (True, False):
78-
raise SCons.Errors.UserError(
79-
'Invalid value for boolean option %s: %s' % (key, env[key])
80-
)
8176
77+
"""
78+
if env[key] not in (True, False):
79+
msg = f'Invalid value for boolean variable {key!r}: {env[key]}'
80+
raise SCons.Errors.UserError(msg) from None
8281

83-
def BoolVariable(key, help, default) -> Tuple[str, str, str, Callable, Callable]:
82+
# lint: W0622: Redefining built-in 'help' (redefined-builtin)
83+
def BoolVariable(key, help: str, default) -> Tuple[str, str, str, Callable, Callable]:
8484
"""Return a tuple describing a boolean SCons Variable.
8585
86-
The input parameters describe a boolean option. Returns a tuple
87-
including the correct converter and validator.
88-
The *help* text will have ``(yes|no)`` automatically appended to show the
89-
valid values. The result is usable as input to :meth:`Add`.
86+
The input parameters describe a boolean variable, using a string
87+
value as described by :const:`TRUE_STRINGS` and :const:`FALSE_STRINGS`.
88+
Returns a tuple including the correct converter and validator.
89+
The *help* text will have ``(yes|no)`` automatically appended to
90+
show the valid values. The result is usable as input to
91+
:meth:`~SCons.Variables.Variables.Add`.
9092
"""
91-
help = '%s (yes|no)' % help
92-
return (key, help, default, _validator, _text2bool)
93+
help = f'{help} (yes|no)'
94+
return key, help, default, _validator, _text2bool
9395

9496
# Local Variables:
9597
# tab-width:4

SCons/Variables/BoolVariableTests.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,14 @@ def test_converter(self) -> None:
6767

6868
for t in true_values:
6969
x = o.converter(t)
70-
assert x, "converter returned false for '%s'" % t
70+
assert x, f"converter returned False for {t!r}"
7171

7272
for f in false_values:
7373
x = o.converter(f)
74-
assert not x, "converter returned true for '%s'" % f
74+
assert not x, f"converter returned True for {f!r}"
7575

76-
caught = False
77-
try:
76+
with self.assertRaises(ValueError):
7877
o.converter('x')
79-
except ValueError:
80-
caught = True
81-
assert caught, "did not catch expected ValueError for 'x'"
8278

8379
def test_validator(self) -> None:
8480
"""Test the BoolVariable validator"""
@@ -98,19 +94,11 @@ def test_validator(self) -> None:
9894
o.validator('F', 0, env)
9995

10096
# negative checks
101-
caught = False
102-
try:
97+
with self.assertRaises(SCons.Errors.UserError):
10398
o.validator('N', 0, env)
104-
except SCons.Errors.UserError:
105-
caught = True
106-
assert caught, "did not catch expected UserError for value %s" % env['N']
10799

108-
caught = False
109-
try:
100+
with self.assertRaises(KeyError):
110101
o.validator('NOSUCHKEY', 0, env)
111-
except KeyError:
112-
caught = True
113-
assert caught, "did not catch expected KeyError for 'NOSUCHKEY'"
114102

115103

116104
if __name__ == "__main__":

test/Variables/BoolVariable.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
Test the BoolVariable canned Variable type.
2828
"""
2929

30-
3130
import TestSCons
3231

3332
test = TestSCons.TestSCons()
@@ -69,10 +68,10 @@ def check(expect):
6968

7069
expect_stderr = """
7170
scons: *** Error converting option: warnings
72-
Invalid value for boolean option: irgendwas
71+
Invalid value for boolean variable: 'irgendwas'
7372
""" + test.python_file_line(SConstruct_path, 13)
7473

75-
test.run(arguments='warnings=irgendwas', stderr = expect_stderr, status=2)
74+
test.run(arguments='warnings=irgendwas', stderr=expect_stderr, status=2)
7675

7776
test.pass_test()
7877

0 commit comments

Comments
 (0)