diff --git a/qutebrowser/commands/argparser.py b/qutebrowser/commands/argparser.py index edb6de306..fe21a5373 100644 --- a/qutebrowser/commands/argparser.py +++ b/qutebrowser/commands/argparser.py @@ -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) diff --git a/tests/unit/commands/test_argparser.py b/tests/unit/commands/test_argparser.py index 3eb8a6d33..dc39f631b 100644 --- a/tests/unit/commands/test_argparser.py +++ b/tests/unit/commands/test_argparser.py @@ -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):