Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix generic aliases generated by Python 3.12 type statements #179

Merged
merged 5 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/tyro/_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ def make(
*,
markers: Tuple[_markers.Marker, ...] = (),
):
# Resolve generic aliases.
type_or_callable = _resolver.apply_type_from_typevar(type_or_callable, {})

# Try to extract argconf overrides from type.
_, argconfs = _resolver.unwrap_annotated_and_aliases(
type_or_callable, _confstruct._ArgConfiguration
Expand Down
15 changes: 13 additions & 2 deletions src/tyro/_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,17 @@ def unwrap_annotated_and_aliases(
def apply_type_from_typevar(
typ: TypeOrCallable, type_from_typevar: Dict[TypeVar, TypeForm[Any]]
) -> TypeOrCallable:
GenericAlias = getattr(types, "GenericAlias", None)
if (
GenericAlias is not None
and isinstance(typ, GenericAlias)
and len(getattr(typ, "__type_params__", ())) > 0
):
type_from_typevar = type_from_typevar.copy()
for k, v in zip(typ.__type_params__, typ.__args__): # type: ignore
type_from_typevar[k] = v # type: ignore
typ = typ.__value__ # type: ignore

if typ in type_from_typevar:
return type_from_typevar[typ] # type: ignore

Expand All @@ -405,7 +416,7 @@ def apply_type_from_typevar(
args = tuple(args[0]) + args[1:]

# Convert Python 3.9 and 3.10 types to their typing library equivalents, which
# support `.copy_with()`.
# support `.copy_with()`. This is not really the right place for this logic...
if sys.version_info[:2] >= (3, 9):
shim_table = {
# PEP 585. Requires Python 3.9.
Expand Down Expand Up @@ -434,7 +445,7 @@ def apply_type_from_typevar(
assert hasattr(origin, "__class_getitem__")
return origin.__class_getitem__(new_args) # type: ignore

return typ
return typ # type: ignore


@_unsafe_cache.unsafe_cache(maxsize=1024)
Expand Down
13 changes: 13 additions & 0 deletions tests/test_new_style_annotations_min_py312.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,16 @@ class Container:
a: AnnotatedBasic

assert tyro.cli(Container, args="--basic 1".split(" ")) == Container(1)


type TT[T] = Annotated[T, tyro.conf.arg(name="", constructor=lambda: True)]


def test_pep695_generic_alias() -> None:
"""Adapted from: https://github.com/brentyi/tyro/issues/177"""

@dataclass(frozen=True)
class Config:
arg: TT[bool]

assert tyro.cli(Config, args=[]) == Config(arg=True)
Loading