-
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.
Start struct handling rewrite (#191)
* Custom constructors + container type resolution cleanup, docs * Simpler rule API * Global namespace cleanup * fix for older Python versions * Ignore docs for pyright * Bump typing-extensions for typing.ReadOnly * Docs sync * Fix 3.7 typing-extension dep * Fix issubclass error for new pydantic * Add pydantic + generics test * Start refactor * More refactor * Passing tests * pyright * ruff * exports * fix frozendict for Python 3.8 * dict fix * mypy * coverage
- Loading branch information
Showing
41 changed files
with
1,516 additions
and
1,487 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
92 changes: 92 additions & 0 deletions
92
docs/source/examples/04_additional/12_custom_constructors_registry.rst
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,92 @@ | ||
.. Comment: this file is automatically generated by `update_example_docs.py`. | ||
It should not be modified manually. | ||
Custom Constructors (Registry) | ||
========================================== | ||
|
||
For additional flexibility, :module:`tyro.constructors` exposes | ||
tyro's API for defining behavior for different types. This is the same | ||
API that tyro relies on for the built-in types. | ||
|
||
|
||
.. code-block:: python | ||
:linenos: | ||
import json | ||
from typing import Any | ||
import tyro | ||
custom_registry = tyro.constructors.PrimitiveConstructorRegistry() | ||
@custom_registry.define_rule | ||
def _( | ||
type_info: tyro.constructors.PrimitiveTypeInfo, | ||
) -> tyro.constructors.PrimitiveConstructorSpec | None: | ||
# We return `None` if the rule does not apply. | ||
if type_info.type != dict[str, Any]: | ||
return None | ||
# If the rule applies, we return the constructor spec. | ||
return tyro.constructors.PrimitiveConstructorSpec( | ||
nargs=1, | ||
metavar="JSON", | ||
instance_from_str=lambda args: json.loads(args[0]), | ||
is_instance=lambda instance: isinstance(instance, dict), | ||
str_from_instance=lambda instance: [json.dumps(instance)], | ||
) | ||
def main( | ||
dict1: dict[str, Any], | ||
dict2: dict[str, Any] = {"default": None}, | ||
) -> None: | ||
print(f"{dict1=}") | ||
print(f"{dict2=}") | ||
if __name__ == "__main__": | ||
with custom_registry: | ||
tyro.cli(main) | ||
------------ | ||
|
||
.. raw:: html | ||
|
||
<kbd>python 04_additional/12_custom_constructors_registry.py --help</kbd> | ||
|
||
.. program-output:: python ../../examples/04_additional/12_custom_constructors_registry.py --help | ||
|
||
------------ | ||
|
||
.. raw:: html | ||
|
||
<kbd>python 04_additional/12_custom_constructors_registry.py --dict1.json '{"hello": "world"}'</kbd> | ||
|
||
.. program-output:: python ../../examples/04_additional/12_custom_constructors_registry.py --dict1.json '{"hello": "world"}' | ||
|
||
------------ | ||
|
||
.. raw:: html | ||
|
||
<kbd>python 04_additional/12_custom_constructors_registry.py --dict1.json '{"hello": "world"}'</kbd> | ||
|
||
.. program-output:: python ../../examples/04_additional/12_custom_constructors_registry.py --dict1.json '{"hello": "world"}' | ||
|
||
------------ | ||
|
||
.. raw:: html | ||
|
||
<kbd>python 04_additional/12_custom_constructors_registry.py --dict1.json '{"hello": "world"}' --dict2.json '{"hello": "world"}'</kbd> | ||
|
||
.. program-output:: python ../../examples/04_additional/12_custom_constructors_registry.py --dict1.json '{"hello": "world"}' --dict2.json '{"hello": "world"}' | ||
|
||
------------ | ||
|
||
.. raw:: html | ||
|
||
<kbd>python 04_additional/12_custom_constructors_registry.py --dict1.json '{"hello": "world"}' --dict2.json '{"hello": "world"}'</kbd> | ||
|
||
.. program-output:: python ../../examples/04_additional/12_custom_constructors_registry.py --dict1.json '{"hello": "world"}' --dict2.json '{"hello": "world"}' |
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
Oops, something went wrong.