brentyi / tyro

CLI interfaces & config objects, from types
https://brentyi.github.io/tyro
MIT License
467 stars 23 forks source link

single subcommand causes UnsupportedTypeAnnotationError #145

Closed bzip2 closed 2 months ago

bzip2 commented 3 months ago

Hi. Nice work. This is minor but if you only have one subcommand,

import tyro
def commit(branch: str) -> None:
    print(f"commit {branch=}")
def checkout(branch: str) -> None:
    print(f"checkout {branch=}")

tyro.extras.subcommand_cli_from_dict(
    #{"commit": commit, "checkout" : checkout}
    {"commit": commit} # <<< 
)

The error message is cryptic:

  File "test_tyro.py", line 8, in <module>
    tyro.extras.subcommand_cli_from_dict(
  File "python3.12/site-packages/tyro/extras/_subcommand_cli_from_dict.py", line 90, in subcommand_cli_from_dict
    return cli(
           ^^^^
  File "python3.12/site-packages/tyro/_cli.py", line 184, in cli
    output = _cli_impl(
             ^^^^^^^^^^
  File "python3.12/site-packages/tyro/_cli.py", line 370, in _cli_impl
    parser_spec = _parsers.ParserSpecification.from_callable_or_type(
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "python3.12/site-packages/tyro/_parsers.py", line 82, in from_callable_or_type
    f, type_from_typevar, field_list = _fields.field_list_from_callable(
                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "python3.12/site-packages/tyro/_fields.py", line 309, in field_list_from_callable
    raise _instantiators.UnsupportedTypeAnnotationError(field_list.message)
tyro._instantiators.UnsupportedTypeAnnotationError: Expected fully type-annotated callable, but <function Singleton.__new__ at 0x794d33813ce0> with arguments ('args', 'kwds') has no annotation for 'args'.

Thanks.

brentyi commented 2 months ago

Hi @bzip2, thanks so much for the report + concise reproduction instructions!

I just pushed a handful of changes that will improve error messages in situations including this one. They'll be available in the next release. 🙂

brentyi commented 2 months ago

This should be fixed in 0.8.5!