From 83ff862019092b6630114934ddb6a85bff270b46 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Sun, 15 Sep 2024 16:19:56 +0300 Subject: [PATCH 1/2] Handle default arguments in nested models --- src/tyro/_fields.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/tyro/_fields.py b/src/tyro/_fields.py index 931a0982..dcfc8fb3 100644 --- a/src/tyro/_fields.py +++ b/src/tyro/_fields.py @@ -1165,6 +1165,8 @@ def _get_pydantic_v2_field_default( parent_default_instance: DefaultInstance, ) -> Tuple[Any, bool]: """Helper for getting the default instance for a Pydantic field.""" + + import pydantic # Try grabbing default from parent instance. if ( @@ -1183,7 +1185,10 @@ def _get_pydantic_v2_field_default( ) if not field.is_required(): - return field.get_default(call_default_factory=True), False + try: + return field.get_default(call_default_factory=True), False + except pydantic.ValidationError: + pass # Otherwise, no default. return MISSING_NONPROP, False From e6e69930079ec623a7a2500ce09e03fb01c80f4b Mon Sep 17 00:00:00 2001 From: Dmitry Date: Mon, 16 Sep 2024 10:36:57 +0300 Subject: [PATCH 2/2] Run ruff formatting --- src/tyro/_fields.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tyro/_fields.py b/src/tyro/_fields.py index dcfc8fb3..0f43ea9c 100644 --- a/src/tyro/_fields.py +++ b/src/tyro/_fields.py @@ -1165,7 +1165,7 @@ def _get_pydantic_v2_field_default( parent_default_instance: DefaultInstance, ) -> Tuple[Any, bool]: """Helper for getting the default instance for a Pydantic field.""" - + import pydantic # Try grabbing default from parent instance.