Skip to content

Commit

Permalink
Merge pull request #6571 from wxtim/fix.underscores_in_integers
Browse files Browse the repository at this point in the history
Assume task parameters with underscores to be strings.
  • Loading branch information
oliver-sanders authored Jan 28, 2025
2 parents b942907 + 17d7385 commit 749b8ce
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
1 change: 1 addition & 0 deletions changes.d/6571.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Disabled PEP-515-style integer coercion of task parameters containing underscores (e.g. `084_132` was becoming `84132`). This fix returns older behaviour seen in Cylc 7.
1 change: 0 additions & 1 deletion cylc/flow/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,6 @@ def __init__(

# parameter values and templates are normally needed together.
self.parameters = (parameter_values, parameter_templates)

LOG.debug("Expanding [runtime] namespace lists and parameters")

# Set default parameter expansion templates if necessary.
Expand Down
16 changes: 14 additions & 2 deletions cylc/flow/parsec/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -1007,22 +1007,30 @@ def coerce_parameter_list(cls, value, keys):
>>> CylcConfigValidator.coerce_parameter_list('a, b, c', None)
['a', 'b', 'c']
>>> CylcConfigValidator.coerce_parameter_list('084_132', None)
['084_132']
>>> CylcConfigValidator.coerce_parameter_list('072, a', None)
['072', 'a']
"""
items = []
can_only_be = None # A flag to prevent mixing str and int range
for item in cls.strip_and_unquote_list(keys, value):
values = cls.parse_int_range(item)
if values is not None:
if can_only_be == str:
if can_only_be is str:
raise IllegalValueError(
'parameter', keys, value, 'mixing int range and str')
can_only_be = int
items.extend(values)
elif cls._REC_NAME_SUFFIX.match(item): # noqa: SIM106
try:
if '_' in item:
# Disable PEP-515 int coercion; go to except block:
raise ValueError()
int(item)
except ValueError:
if can_only_be == int:
if can_only_be is int:
raise IllegalValueError(
'parameter',
keys,
Expand All @@ -1034,6 +1042,10 @@ def coerce_parameter_list(cls, value, keys):
else:
raise IllegalValueError(
'parameter', keys, value, '%s: bad value' % item)

if can_only_be is str:
return items

try:
return [int(item) for item in items]
except ValueError:
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/parsec/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ def test_coerce_parameter_list():
('-15, -10, -5, -1..1', [-15, -10, -5, -1, 0, 1])]:
assert validator.coerce_parameter_list(value, ['whatever']) == result
# The bad
for value in ['foo/bar', 'p1, 1..10', '2..3, 4, p']:
for value in ['foo/bar', 'p1, 1..10', '2..3, 4, p', 'x:,']:
with pytest.raises(IllegalValueError):
validator.coerce_parameter_list(value, ['whatever'])

Expand Down

0 comments on commit 749b8ce

Please sign in to comment.