-
-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Switch to typing-inspection
#556
Conversation
d93b372
to
f1c9d3c
Compare
return str(obj) | ||
|
||
if not isinstance(obj, (typing_base, WithArgsTypes, type)): | ||
origin = get_origin(obj) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is still a big mess, in the future I'd like to add a string repr function in typing-inspection
annotation = root_annotation | ||
except TypeError: | ||
pass | ||
if annotation is not None and _lenient_issubclass(annotation, RootModel) and annotation is not RootModel: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_lenient_issubclass
takes care of the TypeError
.
Also, checking for annotation is not RootModel
and relying on model_fields['root'].annotation
is more robust
@@ -2365,22 +2364,23 @@ def _annotation_contains_types( | |||
|
|||
|
|||
def _strip_annotated(annotation: Any) -> Any: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Python takes care of flattening Annotated forms already.
if sys.version_info < (3, 10): | ||
_WithArgsTypes = tuple() | ||
else: | ||
_WithArgsTypes = (_GenericAlias, types.GenericAlias, types.UnionType) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is taken from Pydantic 2.9. The < (3, 10)
branch was actually wrong (it was missing _GenericAlias
), and pydantic-settings relied on this inconsistency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually this was reported here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm keeping the inconsistency here because the metavar format function is really messy and I can't reason properly when looking into it. It needs to be refactored and as mentioned in another comment this will probably be done in typing-inspection
.
f1c9d3c
to
ceb3253
Compare
ceb3253
to
5fadd43
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @Viicos, Looks Great 👍
Also fix some logic to be a bit more robust.
I'll note that the code base rely heavily on inspection of typing objects, but this fragile for several reasons (one example is when determining if a union is complex: this doesn't take into account PEP 695 type aliases).
Ideally most of the logic should rely on the core schema instead, but this isn't trivial as well.
Closes #553.