Skip to content

Commit

Permalink
Note Python 3.10 edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
brentyi committed Feb 19, 2025
1 parent 4aff1a8 commit 9a434d4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 34 deletions.
25 changes: 15 additions & 10 deletions tests/test_positional_min_py38.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import sys
from typing import List, Optional, Tuple

import pytest
Expand Down Expand Up @@ -192,17 +193,21 @@ def main(field1: TupleCustomConstructor, /, field2: int = 3) -> Tuple[str, ...]:
]


def test_tuple_custom_constructors_positional_default_none() -> None:
def main(
field1: TupleCustomConstructor2 | None = None, /, field2: int = 3
) -> Tuple[str, ...] | None:
del field2
return field1
if sys.version_info >= (3, 11):

assert tyro.cli(main, args=["a", "b"]) == ("a", "b")
assert tyro.cli(main, args=["a"]) == ("a",)
assert tyro.cli(main, args=[]) is None
assert "A TUPLE METAVAR" in get_helptext_with_checks(main)
def test_tuple_custom_constructors_positional_default_none() -> None:
# Waiting for typing_extensions with this fixed:
# https://github.com/python/typing_extensions/issues/310
def main(
field1: TupleCustomConstructor2 | None = None, /, field2: int = 3
) -> Tuple[str, ...] | None:
del field2
return field1

assert tyro.cli(main, args=["a", "b"]) == ("a", "b")
assert tyro.cli(main, args=["a"]) == ("a",)
assert tyro.cli(main, args=[]) is None
assert "A TUPLE METAVAR" in get_helptext_with_checks(main)


def test_tuple_custom_constructors_positional_default_five() -> None:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import json
from typing import Annotated, Any, Dict, List, Literal, get_args
from typing import Annotated, Any, Dict, List, Literal, Tuple, get_args

import numpy as np
import pytest
Expand Down Expand Up @@ -151,7 +151,7 @@ def main(


TupleCustomConstructor = Annotated[
tuple[str, ...],
Tuple[str, ...],
tyro.constructors.PrimitiveConstructorSpec(
nargs="*",
metavar="A TUPLE METAVAR",
Expand Down
53 changes: 31 additions & 22 deletions tests/test_py311_generated/test_positional_min_py38_generated.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from __future__ import annotations

import sys
from typing import Annotated, List, Optional, Tuple

import pytest
Expand Down Expand Up @@ -32,11 +35,12 @@ def main(
assert tyro.cli(main, args="--x 1 --y 2 --z 3".split(" ")) == (1, 2, 3)


def test_nested_positional():
class A:
def __init__(self, a: int, hello_world: int, /, c: int):
self.hello_world = hello_world
class A:
def __init__(self, a: int, hello_world: int, /, c: int):
self.hello_world = hello_world


def test_nested_positional():
def nest1(a: int, b: int, thing: A, /, c: int) -> A:
return thing

Expand All @@ -46,11 +50,12 @@ def nest1(a: int, b: int, thing: A, /, c: int) -> A:
tyro.cli(nest1, args="0 1 2 3 4 4 --c 4".split(" "))


def test_nested_positional_alt():
class B:
def __init__(self, a: int, b: int, /, c: int):
pass
class B:
def __init__(self, a: int, b: int, /, c: int):
pass


def test_nested_positional_alt():
def nest2(a: int, b: int, /, thing: B, c: int):
return thing

Expand Down Expand Up @@ -151,7 +156,7 @@ def main(


TupleCustomConstructor = Annotated[
tuple[str, ...],
Tuple[str, ...],
tyro.constructors.PrimitiveConstructorSpec(
nargs="*",
metavar="A TUPLE METAVAR",
Expand All @@ -164,7 +169,7 @@ def main(


def test_tuple_custom_constructors_positional() -> None:
def main(field1: TupleCustomConstructor, /, field2: int = 3) -> tuple[str, ...]:
def main(field1: TupleCustomConstructor, /, field2: int = 3) -> Tuple[str, ...]:
del field2
return field1

Expand All @@ -175,7 +180,7 @@ def main(field1: TupleCustomConstructor, /, field2: int = 3) -> tuple[str, ...]:


TupleCustomConstructor2 = Annotated[
tuple[str, ...],
Tuple[str, ...],
tyro.constructors.PrimitiveConstructorSpec(
nargs="*",
metavar="A TUPLE METAVAR",
Expand All @@ -187,23 +192,27 @@ def main(field1: TupleCustomConstructor, /, field2: int = 3) -> tuple[str, ...]:
]


def test_tuple_custom_constructors_positional_default_none() -> None:
def main(
field1: TupleCustomConstructor2 | None = None, /, field2: int = 3
) -> tuple[str, ...] | None:
del field2
return field1
if sys.version_info >= (3, 11):

assert tyro.cli(main, args=["a", "b"]) == ("a", "b")
assert tyro.cli(main, args=["a"]) == ("a",)
assert tyro.cli(main, args=[]) is None
assert "A TUPLE METAVAR" in get_helptext_with_checks(main)
def test_tuple_custom_constructors_positional_default_none() -> None:
# Waiting for typing with this fixed:
# https://github.com/python/typing/issues/310
def main(
field1: TupleCustomConstructor2 | None = None, /, field2: int = 3
) -> Tuple[str, ...] | None:
del field2
return field1

assert tyro.cli(main, args=["a", "b"]) == ("a", "b")
assert tyro.cli(main, args=["a"]) == ("a",)
assert tyro.cli(main, args=[]) is None
assert "A TUPLE METAVAR" in get_helptext_with_checks(main)


def test_tuple_custom_constructors_positional_default_five() -> None:
def main(
field1: TupleCustomConstructor2 | int = 5, /, field2: int = 3
) -> tuple[str, ...] | int:
) -> Tuple[str, ...] | int:
del field2
return field1

Expand Down

0 comments on commit 9a434d4

Please # to comment.