Skip to content
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

Merged
merged 1 commit into from
Feb 28, 2025
Merged

Switch to typing-inspection #556

merged 1 commit into from
Feb 28, 2025

Conversation

Viicos
Copy link
Member

@Viicos Viicos commented Feb 26, 2025

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.

return str(obj)

if not isinstance(obj, (typing_base, WithArgsTypes, type)):
origin = get_origin(obj)
Copy link
Member Author

@Viicos Viicos Feb 26, 2025

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:
Copy link
Member Author

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:
Copy link
Member Author

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.

Comment on lines +45 to +48
if sys.version_info < (3, 10):
_WithArgsTypes = tuple()
else:
_WithArgsTypes = (_GenericAlias, types.GenericAlias, types.UnionType)
Copy link
Member Author

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.

Copy link
Member Author

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.

Copy link
Member Author

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.

This was referenced Feb 26, 2025
@Viicos Viicos requested a review from hramezani February 28, 2025 14:04
Copy link
Member

@hramezani hramezani left a 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 👍

@Viicos Viicos merged commit e5fb741 into main Feb 28, 2025
18 checks passed
@Viicos Viicos deleted the typing-inspection branch February 28, 2025 15:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants