-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Rework create_model
field definitions format
#11032
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
Conversation
Deploying pydantic-docs with
|
Latest commit: |
a0c253b
|
Status: | ✅ Deploy successful! |
Preview URL: | https://ba5c77de.pydantic-docs.pages.dev |
Branch Preview URL: | https://create-model-rework.pydantic-docs.pages.dev |
If the value is a two-tuple, the first element is the type and the second element is the assigned value (either a default or a `Field()` function). This isn't breaking as this was already allowed before. We now also allow a single element to be passed, which will be the type. This also isn't breaking because: - We previously allowed a single element to be passed if it was an annotated type. We then extracted the first metadata element and enforced it to be a `Field()` function. With these changes, we now just leave the annotated type as is, so an arbitrary amount of metadata can now be specified. - If the type wasn't annotated, we were raising an error. Private fields are now allowed. Redundant tests were merged/removed. Documentation was updated. The `__slots__` parameter was removed. It was deprecated (with a wrong warning class, but still) since 1.10, was not doing anything, so I don't expect anyone to be using it.
0c70c39
to
46ef644
Compare
CodSpeed Performance ReportMerging #11032 will not alter performanceComparing Summary
|
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
Seems like a common error was to write tests in this file, because it was named `test_create_model.py`.
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, solid improvement here.
Nice work with the docs updates. Also, pretty simple implementation change, which is nice as well.
A few follow up questions, but otherwise, looking great!
I've added the As you mentioned, deprecated long ago, so I'm not too worried about breakages 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.
Nice work, excited to have this be more flexible.
With recent release in pydantic, the pickling is not working as expected. This restricts the pydantic to 2.10.6 untill we fix the breaking changes. Relevant links: - https://github.com/pydantic/pydantic/releases/tag/v2.11.0 - pydantic/pydantic#11032 - https://iterativeai.slack.com/archives/C04A9RWEZBN/p1743143098907759
With recent release in pydantic, the pickling is not working as expected. This restricts the pydantic to 2.10.6 untill we fix the breaking changes. Relevant links: - https://github.com/pydantic/pydantic/releases/tag/v2.11.0 - pydantic/pydantic#11032 - https://iterativeai.slack.com/archives/C04A9RWEZBN/p1743143098907759
I'm look at this as I suspect this causes multiple regressions 2.10.6 -> 2.11.0 when using create_model known to me:
as there is multiple issues linked to this change - any chance this gets revisited? |
As mentioned in the linked issues, this wasn't documented/expected, in particular for |
At the very least, we could backport the fix for #11709 in the next patch release. |
Let's forget about model_config/config, inconvenient but won't cause any long lasting headaches. For computed fields and properties, as it works for models created by inheriting from BaseModel, there is nothing wrong in the expectation create_model can do the same. And it did the same for about two years. As an MRE for property issue: from pydantic import create_model, ConfigDict
def mkx():
def get_additionalProperties(x):
return x.model_extra
return get_additionalProperties, None, None
A = create_model(
'A',
__config__=ConfigDict(extra="allow"),
aio3_additionalProperties=(None, property(mkx()[0])),
)
a = A(x=1)
assert not isinstance(a.aio3_additionalProperties, property) A possible fix - make the annotation declaration optional: Lines 1742 to 1746 in 2e7cddb
if f_def[0]:
annotations[f_name] = f_def[0]
fields[f_name] = f_def[1]
else:
annotations[f_name] = f_def |
There is, as I mention and this can be seen in the 2.10 documentation: We haven't decided on a solution yet, but it will probably not be using the |
Fixes #11005, fixes #11015, closes #11006.
If the value is a two-tuple, the first element is the type and the second element is the assigned value (either a default or a
Field()
function). This isn't breaking as this was already allowed before.We now also allow a single element to be passed, which will be the type. This also isn't breaking because:
Field()
function. With these changes, we now just leave the annotated type as is, so an arbitrary amount of metadata can now be specified.Private fields are now allowed.
Redundant tests were merged/removed. Documentation was updated.
The
__slots__
parameter was removed. It was deprecated (with a wrong warning class, but still) since 1.10, was not doing anything, so I don't expect anyone to be using it.Change Summary
Related issue number
Checklist