You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
TypingHintArgSpecGuesser breaks on type alias List #216
Sorry not to give further details but I had the choice between sending a quick issue or not mentioning it at all.
My script, that uses argh, stopped working when I updated to argh==0.31.0 from argh==0.26.2.
The error
argh.dispatch_command(populate_pkg_dir)
File "~/.pyenv/versions/p10/lib/python3.10/site-packages/argh/dispatching.py", line 470, in dispatch_command
set_default_command(parser, function, name_mapping_policy=name_mapping_policy)
File "~/.pyenv/versions/p10/lib/python3.10/site-packages/argh/assembling.py", line 420, in set_default_command
inferred_args: List[ParserAddArgumentSpec] = list(
File "~/.pyenv/versions/p10/lib/python3.10/site-packages/argh/assembling.py", line 183, in infer_argspecs_from_function
TypingHintArgSpecGuesser.typing_hint_to_arg_spec_params(
File "~/.pyenv/versions/p10/lib/python3.10/site-packages/argh/assembling.py", line 778, in typing_hint_to_arg_spec_params
item_type = cls._extract_item_type_from_list_type(first_subtype)
File "~.pyenv/versions/p10/lib/python3.10/site-packages/argh/assembling.py", line 799, in _extract_item_type_from_list_type
if args[0] in cls.BASIC_TYPES:
IndexError: tuple index out of range
My guess is that the len(args) == 0 case is not handled properly?
Without looking further, I'd suggest:
ifargsand (args[0] incls.BASIC_TYPES):
instead?
If I have time, I'll look at it further this week and send a PR.
changed the title [-]`argh==0.31.0` breaks (my) `argh.dispatch_command` call[/-][+]TypingHintArgSpecGuesser breaks on type alias List[/+]on Jan 16, 2024
Note that replacing Optional[List] with Optional[list] or just List works though.
File "~/tmp/scrap.py", line 9, in <module>
argh.dispatch_command(func)
File "~/.pyenv/versions/p10/lib/python3.10/site-packages/argh/dispatching.py", line 470, in dispatch_command
set_default_command(parser, function, name_mapping_policy=name_mapping_policy)
File "~/.pyenv/versions/p10/lib/python3.10/site-packages/argh/assembling.py", line 420, in set_default_command
inferred_args: List[ParserAddArgumentSpec] = list(
File "~/.pyenv/versions/p10/lib/python3.10/site-packages/argh/assembling.py", line 183, in infer_argspecs_from_function
TypingHintArgSpecGuesser.typing_hint_to_arg_spec_params(
File "~/.pyenv/versions/p10/lib/python3.10/site-packages/argh/assembling.py", line 778, in typing_hint_to_arg_spec_params
item_type = cls._extract_item_type_from_list_type(first_subtype)
File "~/.pyenv/versions/p10/lib/python3.10/site-packages/argh/assembling.py", line 799, in _extract_item_type_from_list_type
if args[0] in cls.BASIC_TYPES:
IndexError: tuple index out of range
Ahh, you're right. I fixed a more general case but it was not enough.
Will ship the fix with 0.31.2 later today.
BTW, we'll get rid of those aliases (List etc.) when we drop support for Python 3.8 (pretty soon), they are deprecated since Python 3.9, so I'd really try to use Optional[list] or even list | None whenever possible.
The good news is that, trying it out on my side, I find that, out of 109776 combinations tested, the only one that failed was our good old `Optional[List]'!
@neithere -- made a PR containing my code to increase the test coverage for this problem.
Uses (further edited) versions of my code I pasted above, plus a test function to apply it to argh.dispatch over a large (100K+) number of type combinations.
@thorwhalen oh wow, thanks, that's a lot going on there :) I'll need some time to read the code itself.
I've enabled CI for that PR and it's failing with some strange error messages (and lots of them!). I don't have time to dig deep into that at the moment, so I think that for now I'll just rely on the results of your research and assume that the only known problematic combination is fixed, and therefore the fix can be shipped. That's a great result already.
Later I'll need to think how to ensure optimal test coverage for the upcoming Argh 1.0 (it won't support type aliases like List, BTW). At the moment I'm leaning towards writing tests specifically for what Argh knows how to interpret, and so far it's a pretty limited set of types. It also won't go too deep inside of those because of CLI limitations (e.g. pythonic foo: list[str] is foo, [foo ...] in the CLI world, but what could foo: list[list[str]] mean in CLI? how would you express a dict in a set of CLI arguments?). Of course we also need to ensure that the library doesn't crash on whatever it does not support, so a generator of permutations might be useful indeed, and I guess that's exactly the problem that your PR addresses.
@thorwhalen oh wow, thanks, that's a lot going on there :) I'll need some time to read the code itself.
I've enabled CI for that PR and it's failing with some strange error messages (and lots of them!). I don't have time to dig deep into that at the moment, so I think that for now I'll just rely on the results of your research and assume that the only known problematic combination is fixed, and therefore the fix can be shipped. That's a great result already.
Later I'll need to think how to ensure optimal test coverage for the upcoming Argh 1.0 (it won't support type aliases like List, BTW). At the moment I'm leaning towards writing tests specifically for what Argh knows how to interpret, and so far it's a pretty limited set of types. It also won't go too deep inside of those because of CLI limitations (e.g. pythonic foo: list[str] is foo, [foo ...] in the CLI world, but what could foo: list[list[str]] mean in CLI? how would you express a dict in a set of CLI arguments?). Of course we also need to ensure that the library doesn't crash on whatever it does not support, so a generator of permutations might be useful indeed, and I guess that's exactly the problem that your PR addresses.
Yes. Just wanted to see what a test covering a lot more combinations would look like. Just a proposal. Not essential.
Activity
fix: issue neithere#216
thorwhalen commentedon Jan 15, 2024
I went ahead and made a PR for this: #217
[-]`argh==0.31.0` breaks (my) `argh.dispatch_command` call[/-][+]TypingHintArgSpecGuesser breaks on type alias List[/+]neithere commentedon Jan 16, 2024
Minimal example as discussed in the related PR:
def func(xs: list)— worksdef func(xs: List)— breaksfix: broken support for type alias List (fixes #216)
thorwhalen commentedon Jan 23, 2024
@neithere : This still breaks for me with
argh==0.31.1:Note that replacing
Optional[List]withOptional[list]or justListworks though.neithere commentedon Jan 23, 2024
Ahh, you're right. I fixed a more general case but it was not enough.
Will ship the fix with 0.31.2 later today.
BTW, we'll get rid of those aliases (
Listetc.) when we drop support for Python 3.8 (pretty soon), they are deprecated since Python 3.9, so I'd really try to useOptional[list]or evenlist | Nonewhenever possible.fix: broken support for `Optional[List]` (fixes #216)
thorwhalen commentedon Jan 23, 2024
@neithere: I see you're active, so let me help you out with this.
You might want to use this to make a more significant test:
thorwhalen commentedon Jan 23, 2024
Combine this with this and you'll get some monster coverage:
thorwhalen commentedon Jan 23, 2024
The good news is that, trying it out on my side, I find that, out of 109776 combinations tested, the only one that failed was our good old `Optional[List]'!
I'll do a PR to add these tests.
feat: test_that_issue_216_does_not_happen to increase test coverage of …
thorwhalen commentedon Jan 23, 2024
@neithere -- made a PR containing my code to increase the test coverage for this problem.
Uses (further edited) versions of my code I pasted above, plus a test function to apply it to
argh.dispatchover a large (100K+) number of type combinations.#219
neithere commentedon Jan 24, 2024
@thorwhalen oh wow, thanks, that's a lot going on there :) I'll need some time to read the code itself.
I've enabled CI for that PR and it's failing with some strange error messages (and lots of them!). I don't have time to dig deep into that at the moment, so I think that for now I'll just rely on the results of your research and assume that the only known problematic combination is fixed, and therefore the fix can be shipped. That's a great result already.
Later I'll need to think how to ensure optimal test coverage for the upcoming Argh 1.0 (it won't support type aliases like
List, BTW). At the moment I'm leaning towards writing tests specifically for what Argh knows how to interpret, and so far it's a pretty limited set of types. It also won't go too deep inside of those because of CLI limitations (e.g. pythonicfoo: list[str]isfoo, [foo ...]in the CLI world, but what couldfoo: list[list[str]]mean in CLI? how would you express adictin a set of CLI arguments?). Of course we also need to ensure that the library doesn't crash on whatever it does not support, so a generator of permutations might be useful indeed, and I guess that's exactly the problem that your PR addresses.neithere commentedon Jan 24, 2024
P.S.: FYI, I'm trying to comment on the latest developments in typing hints support in #107 :)
thorwhalen commentedon Jan 26, 2024
Yes. Just wanted to see what a test covering a lot more combinations would look like. Just a proposal. Not essential.