Make sure typing.Union[str, int] gets handled
str always needs to be the last element checked as otherwise it'd always win.
This commit is contained in:
parent
a0d0b6464f
commit
6ed9b6b13f
@ -161,9 +161,16 @@ def multitype_conv(param, types, str_choices=None):
|
||||
types: The allowed types ("overloads")
|
||||
str_choices: The allowed choices if the type ends up being a string
|
||||
"""
|
||||
types = list(set(types))
|
||||
if str in types:
|
||||
# Make sure str is always the last type in the list, so e.g. '23' gets
|
||||
# returned as 23 if we have typing.Union[str, int]
|
||||
types.remove(str)
|
||||
types.append(str)
|
||||
|
||||
def _convert(value):
|
||||
"""Convert a value according to an iterable of possible arg types."""
|
||||
for typ in set(types):
|
||||
for typ in types:
|
||||
try:
|
||||
converter = type_conv(param, typ, str_choices)
|
||||
return converter(value)
|
||||
|
@ -92,6 +92,7 @@ class TestArgumentParser:
|
||||
([int], '2.5', None),
|
||||
([int], 'foo', None),
|
||||
([int, str], 'foo', 'foo'),
|
||||
([str, int], '23', 23),
|
||||
])
|
||||
@pytest.mark.parametrize('multi', [True, False])
|
||||
def test_type_conv(types, value, expected, multi):
|
||||
|
Loading…
Reference in New Issue
Block a user