Skip to content

Commit

Permalink
Test fixes, examples cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
brentyi committed Oct 19, 2022
1 parent fbefca6 commit de7c57b
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 15 deletions.
2 changes: 1 addition & 1 deletion docs/source/examples/01_basics/03_containers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ container types; such as ``typing.List[T]`` or ``typing.Tuple[T1, T2]``. In Pyth
import dataclasses
import enum
import pathlib
from typing import Optional, Tuple
from typing import Tuple
import tyro
Expand Down
2 changes: 0 additions & 2 deletions docs/source/examples/01_basics/04_enums.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ We can generate argument parsers from more advanced type annotations, like enums
import dataclasses
import enum
import pathlib
from typing import Optional, Tuple
import tyro
Expand Down
2 changes: 1 addition & 1 deletion docs/source/examples/01_basics/06_literals.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Choices
import dataclasses
import enum
from typing import Literal, Optional, Tuple, Union
from typing import Literal
import tyro
Expand Down
2 changes: 1 addition & 1 deletion examples/01_basics/03_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import dataclasses
import enum
import pathlib
from typing import Optional, Tuple
from typing import Tuple

import tyro

Expand Down
2 changes: 0 additions & 2 deletions examples/01_basics/04_enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

import dataclasses
import enum
import pathlib
from typing import Optional, Tuple

import tyro

Expand Down
2 changes: 1 addition & 1 deletion examples/01_basics/06_literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import dataclasses
import enum
from typing import Literal, Optional, Tuple, Union
from typing import Literal

import tyro

Expand Down
8 changes: 3 additions & 5 deletions tests/test_dcargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,14 +491,12 @@ def main(x: Callable[[int], int] = lambda x: x * 2) -> Callable[[int], int]:


def test_fixed_dataclass_type():
@dataclasses.dataclass
class Dummy:
pass
dummy = lambda: 5 # noqa

def main(x: Callable = Dummy) -> Callable:
def main(x: Callable = dummy) -> Callable:
return x

assert tyro.cli(main, args=[]) is Dummy
assert tyro.cli(main, args=[]) is dummy
with pytest.raises(SystemExit):
tyro.cli(main, args=["--x", "something"])

Expand Down
10 changes: 8 additions & 2 deletions tyro/_calling.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,15 @@ def get_value_from_arg(prefixed_field_name: str) -> Any:
field.name if field.name_override is None else field.name_override
] = value

unwrapped_f = _resolver.unwrap_origin_strip_extras(f)
unwrapped_f = list if unwrapped_f is Sequence else unwrapped_f # type: ignore
# Note: we unwrap types both before and after narrowing. This is because narrowing
# sometimes produces types like `Tuple[T1, T2, ...]`, where we actually want just
# `tuple`.
unwrapped_f = f
unwrapped_f = _resolver.unwrap_origin_strip_extras(unwrapped_f)
unwrapped_f = _resolver.narrow_type(unwrapped_f, default_instance)
unwrapped_f = _resolver.unwrap_origin_strip_extras(unwrapped_f)
unwrapped_f = list if unwrapped_f is Sequence else unwrapped_f # type: ignore

if unwrapped_f in (tuple, list, set):
if len(args) == 0:
# When tuples are used as nested structures (eg Tuple[SomeDataclass]), we
Expand Down
14 changes: 14 additions & 0 deletions tyro/_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import collections.abc
import copy
import dataclasses
import pathlib
import sys
from typing import (
Any,
Expand Down Expand Up @@ -118,8 +119,21 @@ def narrow_type(typ: TypeT, default_instance: Any) -> TypeT:
should parse as Cat.
Note that Union types are intentionally excluded here."""

# Don't apply narrowing for pathlib.PosixPath, pathlib.WindowsPath, etc.
# This is mostly an aesthetic decision, and is needed because pathlib.Path() hacks
# __new__ to dynamically choose between path types.
if typ is pathlib.Path:
return typ

try:
potential_subclass = type(default_instance)

if potential_subclass is type:
# Don't narrow to `type`. This happens when the default instance is a class;
# it doesn't really make sense to parse this case.
return typ

superclass = unwrap_annotated(typ)[0]
if superclass is Any or issubclass(potential_subclass, superclass): # type: ignore
if get_origin(typ) is Annotated:
Expand Down

0 comments on commit de7c57b

Please sign in to comment.