-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Python 3.12 + PEP 695 (
type
statement, new type parameter s…
…yntax) (#135) * Support Python 3.12, add new-style generics example + test * Add support for for `type` statement * Use Python 3.12 for mypy * `tuple` => `Tuple` in test * ruff * Specify Python 3.12 for docs * Python 3.12 for coverage * Suppress mypy errors * Ignore leading comments in docs update script * Update type param example
- Loading branch information
Showing
26 changed files
with
317 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
.. Comment: this file is automatically generated by `update_example_docs.py`. | ||
It should not be modified manually. | ||
Generic Types (Python 3.12+ syntax) | ||
========================================== | ||
|
||
|
||
Example of parsing for generic dataclasses using syntax introduced in Python | ||
3.12. Note: this is not compatible with ``from __future__ import annotations``. | ||
|
||
|
||
|
||
.. code-block:: python | ||
:linenos: | ||
import dataclasses | ||
import tyro | ||
@dataclasses.dataclass(frozen=True) | ||
class Point3[ScalarType: (int, float)]: | ||
x: ScalarType | ||
y: ScalarType | ||
z: ScalarType | ||
frame_id: str | ||
@dataclasses.dataclass(frozen=True) | ||
class Triangle: | ||
a: Point3[float] | ||
b: Point3[float] | ||
c: Point3[float] | ||
@dataclasses.dataclass(frozen=True) | ||
class Args[ShapeType]: | ||
shape: ShapeType | ||
if __name__ == "__main__": | ||
args = tyro.cli(Args[Triangle]) | ||
print(args) | ||
------------ | ||
|
||
.. raw:: html | ||
|
||
<kbd>python 04_additional/06_generics_py312.py --help</kbd> | ||
|
||
.. program-output:: python ../../examples/04_additional/06_generics_py312.py --help |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
.. Comment: this file is automatically generated by `update_example_docs.py`. | ||
It should not be modified manually. | ||
Type aliases (Python 3.12+) | ||
========================================== | ||
|
||
|
||
In Python 3.12, the ``type`` statement is introduced to create type aliases. | ||
|
||
|
||
|
||
.. code-block:: python | ||
:linenos: | ||
import dataclasses | ||
import tyro | ||
# Lazily-evaluated type alias. | ||
type Field1Type = Inner | ||
@dataclasses.dataclass | ||
class Inner: | ||
a: int | ||
b: str | ||
@dataclasses.dataclass | ||
class Args: | ||
"""Description. | ||
This should show up in the helptext!""" | ||
field1: Field1Type | ||
"""A field.""" | ||
field2: int = 3 | ||
"""A numeric field, with a default value.""" | ||
if __name__ == "__main__": | ||
args = tyro.cli(Args) | ||
print(args) | ||
------------ | ||
|
||
.. raw:: html | ||
|
||
<kbd>python 04_additional/13_type_statement.py --help</kbd> | ||
|
||
.. program-output:: python ../../examples/04_additional/13_type_statement.py --help |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# mypy: ignore-errors | ||
# | ||
# PEP 695 isn't yet supported in mypy. (April 4, 2024) | ||
"""Generic Types (Python 3.12+ syntax) | ||
Example of parsing for generic dataclasses using syntax introduced in Python | ||
3.12. Note: this is not compatible with `from __future__ import annotations`. | ||
Usage: | ||
`python ./05_generics.py --help` | ||
""" | ||
|
||
import dataclasses | ||
|
||
import tyro | ||
|
||
|
||
@dataclasses.dataclass(frozen=True) | ||
class Point3[ScalarType: (int, float)]: | ||
x: ScalarType | ||
y: ScalarType | ||
z: ScalarType | ||
frame_id: str | ||
|
||
|
||
@dataclasses.dataclass(frozen=True) | ||
class Triangle: | ||
a: Point3[float] | ||
b: Point3[float] | ||
c: Point3[float] | ||
|
||
|
||
@dataclasses.dataclass(frozen=True) | ||
class Args[ShapeType]: | ||
shape: ShapeType | ||
|
||
|
||
if __name__ == "__main__": | ||
args = tyro.cli(Args[Triangle]) | ||
print(args) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# mypy: ignore-errors | ||
# | ||
# PEP 695 isn't yet supported in mypy. (April 4, 2024) | ||
"""Type aliases (Python 3.12+) | ||
In Python 3.12, the `type` statement is introduced to create type aliases. | ||
Usage: | ||
`python ./13_type_statement.py --help` | ||
""" | ||
|
||
import dataclasses | ||
|
||
import tyro | ||
|
||
# Lazily-evaluated type alias. | ||
type Field1Type = Inner | ||
|
||
|
||
@dataclasses.dataclass | ||
class Inner: | ||
a: int | ||
b: str | ||
|
||
|
||
@dataclasses.dataclass | ||
class Args: | ||
"""Description. | ||
This should show up in the helptext!""" | ||
|
||
field1: Field1Type | ||
"""A field.""" | ||
|
||
field2: int = 3 | ||
"""A numeric field, with a default value.""" | ||
|
||
|
||
if __name__ == "__main__": | ||
args = tyro.cli(Args) | ||
print(args) |
Oops, something went wrong.