Skip to content

Commit

Permalink
Corner case: kwarg with numerical value passed as string
Browse files Browse the repository at this point in the history
  • Loading branch information
giadarol committed Sep 15, 2024
1 parent 238437e commit 383df52
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
4 changes: 4 additions & 0 deletions tests/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ def test_vars_and_element_access_modes(container_type):
assert env.vv['c'] == 2 * (2 * 4 + 3 * 4 + 6)
env.set('d', 6)
assert env.vv['d'] == 6
env.set('d', '7')
assert env.vv['d'] == 7

ee.set('a', 0.)
assert ee.vv['k.1'] == 3 * 0 + 6
Expand Down Expand Up @@ -247,6 +249,8 @@ def test_assemble_ring():

girder_f = girder.clone(name='f')
girder_d = girder.clone(name='d', mirror=True)
env.set('mq.f', k1='3') # Test string with value
assert env['mq.f'].k1 == 3.
env.set('mq.f', k1='kqf')
env.set('mq.d', k1='kqd')

Expand Down
5 changes: 4 additions & 1 deletion xtrack/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,10 @@ def _parse_kwargs(cls, kwargs, _eval):
elif (isinstance(kwargs[kk], str) and hasattr(cls, '_xofields')
and kk in cls._xofields and cls._xofields[kk].__name__ != 'String'):
ref_kwargs[kk] = _eval(kwargs[kk])
value_kwargs[kk] = ref_kwargs[kk]._value
if hasattr(ref_kwargs[kk], '_value'):
value_kwargs[kk] = ref_kwargs[kk]._value
else:
value_kwargs[kk] = ref_kwargs[kk]
else:
value_kwargs[kk] = kwargs[kk]

Expand Down

0 comments on commit 383df52

Please sign in to comment.