-
-
Couldn't load subscription status.
- Fork 783
🐛 Fix support for Annotated fields with Pydantic 2.12+
#1603
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
base: main
Are you sure you want to change the base?
Conversation
This comment has been minimized.
This comment has been minimized.
This comment was marked as resolved.
This comment was marked as resolved.
|
solves #1602 |
Annotated fields with Pydantic 2.12+
| continue | ||
|
|
||
| original_field = getattr(v, "_original_assignment", Undefined) | ||
| # Get the original sqlmodel FieldInfo, pydantic >=v2.12 changes the model | ||
| if isinstance(original_field, FieldInfo): | ||
| field = original_field | ||
| else: | ||
| annotated_field = next( | ||
| ann | ||
| for c in new_cls.__mro__ | ||
| if (ann := get_annotations(c.__dict__).get(k)) # type: ignore[arg-type] | ||
| ) | ||
| annotated_field_meta = getattr( | ||
| annotated_field, "__metadata__", (()) | ||
| ) | ||
| field = next( | ||
| (f for f in annotated_field_meta if isinstance(f, FieldInfo)), | ||
| v, | ||
| ) # type: ignore[assignment] | ||
| field.annotation = v.annotation | ||
| # Guarantee the field has the correct type | ||
| col = get_column_from_field(field) |
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.
| continue | |
| original_field = getattr(v, "_original_assignment", Undefined) | |
| # Get the original sqlmodel FieldInfo, pydantic >=v2.12 changes the model | |
| if isinstance(original_field, FieldInfo): | |
| field = original_field | |
| else: | |
| annotated_field = next( | |
| ann | |
| for c in new_cls.__mro__ | |
| if (ann := get_annotations(c.__dict__).get(k)) # type: ignore[arg-type] | |
| ) | |
| annotated_field_meta = getattr( | |
| annotated_field, "__metadata__", (()) | |
| ) | |
| field = next( | |
| (f for f in annotated_field_meta if isinstance(f, FieldInfo)), | |
| v, | |
| ) # type: ignore[assignment] | |
| field.annotation = v.annotation | |
| # Guarantee the field has the correct type | |
| col = get_column_from_field(field) | |
| else: | |
| original_field = getattr(v, "_original_assignment", Undefined) | |
| if isinstance(original_field, FieldInfo): | |
| field = original_field | |
| else: | |
| field = find_field_info(new_cls, k, v) | |
| field.annotation = v.annotation | |
| col = get_column_from_field(field) | |
| setattr(new_cls, k, col) |
I think this part will be clearer with if..else instead of continue and with some logic moved to a separate function:
def find_field_info(cls: type, field_name: str, fallback: FieldInfo) -> FieldInfo:
for c in cls.__mro__:
annotated = get_annotations(c.__dict__).get(field_name) # type: ignore[arg-type]
if annotated:
for meta in getattr(annotated, "__metadata__", ()):
if isinstance(meta, FieldInfo):
return meta
return fallbackThere 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.
Yes, it's easier to read, I will apply it
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.
Just upload a change simplifying the logic based on your suggestion, maybe this way is more straight-forward
3f6bec5 to
eb3cfcc
Compare
|
Any idea on when a new release with this fix will be available? |
Not really, hopefully soon! |
That's alright, I'll follow your suggestion. Thanks for the quick reply! |
After
pydanticupdate to support Python 3.14, different declaration syntax were affected by the changed.I check and on
pydanticside there is not changes on the resultingFieldInfostructure, at least at first glance.In the tests I illustrated the 3 possible declaration syntax for annotations defined by
pydanticand supported so far bysqlmodel. Two of them do not work any more withpydantic>=2.12!