Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Reworkcreate_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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Viicos merged 3 commits intomainfromcreate-model-rework
Dec 3, 2024
Merged

Conversation

Viicos
Copy link
Member

@ViicosViicos commentedDec 3, 2024
edited
Loading

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 aField() 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 aField() 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.

Change Summary

Related issue number

Checklist

  • The pull request title is a good summary of the changes - it will be used in the changelog
  • Unit tests for the changes exist
  • Tests pass on CI
  • Documentation reflects the changes where applicable
  • My PR is ready to review,please add a comment including the phrase "please review" to assign reviewers

@github-actionsgithub-actionsbot added the relnotes-fixUsed for bugfixes. labelDec 3, 2024
@cloudflare-workers-and-pagesCloudflare Workers and Pages
Copy link

cloudflare-workers-and-pagesbot commentedDec 3, 2024
edited
Loading

Deploying pydantic-docs with  Cloudflare Pages  Cloudflare Pages

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

View logs

If the value is a two-tuple, the first element is the type andthe second element is the assigned value (either a default ora `Field()` function). This isn't breaking as this was alreadyallowed before.We now also allow a single element to be passed, which will bethe 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 (witha wrong warning class, but still) since 1.10, was not doing anything,so I don't expect anyone to be using it.
@codspeed-hqCodSpeed HQ
Copy link

codspeed-hqbot commentedDec 3, 2024
edited
Loading

CodSpeed Performance Report

Merging#11032 willnot alter performance

Comparingcreate-model-rework (a0c253b) withmain (c6905b6)

Summary

✅ 46 untouched benchmarks

@github-actionsGitHub Actions
Copy link
Contributor

github-actionsbot commentedDec 3, 2024
edited
Loading

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  pydantic
  main.py
Project Total 

This report was generated bypython-coverage-comment-action

Seems like a common error was to write tests in this file,because it was named `test_create_model.py`.
Copy link
Contributor

@sydney-runklesydney-runkle 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, 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!

@sydney-runklesydney-runkle added relnotes-changeUsed for changes to existing functionality which don't have a better categorization. and removed relnotes-fixUsed for bugfixes. labelsDec 3, 2024
@sydney-runkle
Copy link
Contributor

I've added therelnotes-change label, as we should highlight the official end of support for__slots__ in the release notes here.

As you mentioned, deprecated long ago, so I'm not too worried about breakages here.

@ViicosViicosenabled auto-merge (squash)December 3, 2024 17:16
Copy link
Contributor

@sydney-runklesydney-runkle left a 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.

@ViicosViicos merged commitc409bc7 intomainDec 3, 2024
56 checks passed
@ViicosViicos deleted the create-model-rework branchDecember 3, 2024 17:23
amritghimire added a commit to iterative/datachain that referenced this pull requestMar 28, 2025
With recent release in pydantic, the pickling is not working asexpected. This restricts the pydantic to 2.10.6 untill we fix thebreaking changes.Relevant links:-https://github.com/pydantic/pydantic/releases/tag/v2.11.0-pydantic/pydantic#11032-https://iterativeai.slack.com/archives/C04A9RWEZBN/p1743143098907759
amritghimire added a commit to iterative/datachain that referenced this pull requestMar 28, 2025
With recent release in pydantic, the pickling is not working asexpected. This restricts the pydantic to 2.10.6 untill we fix thebreaking changes.Relevant links:-https://github.com/pydantic/pydantic/releases/tag/v2.11.0-pydantic/pydantic#11032-https://iterativeai.slack.com/archives/C04A9RWEZBN/p1743143098907759
@commonism
Copy link
Contributor

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:

  • using model_config as field (easy, usingconfig instead)
  • using a property(…) as field (yet unknown)

as there is multiple issues linked to this change - any chance this gets revisited?

@Viicos
Copy link
MemberAuthor

Viicos commentedApr 11, 2025
edited
Loading

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 formodel_config vs__config__. This is a typicalhttps://xkcd.com/1172/ situation, so nothing will change for 2.11. For properties, this is unfortunate but it was clearly documented that the extra keyword arguments are fields.

@Viicos
Copy link
MemberAuthor

Viicos commentedApr 11, 2025
edited
Loading

At the very least, we could backport the fix for#11709 in the next patch release.

@commonism
Copy link
Contributor

commonism commentedApr 11, 2025
edited
Loading

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:

frompydanticimportcreate_model,ConfigDictdefmkx():defget_additionalProperties(x):returnx.model_extrareturnget_additionalProperties,None,NoneA=create_model('A',__config__=ConfigDict(extra="allow"),aio3_additionalProperties=(None,property(mkx()[0])),)a=A(x=1)assertnotisinstance(a.aio3_additionalProperties,property)

A possible fix - make the annotation declaration optional:

pydantic/pydantic/main.py

Lines 1742 to 1746 in2e7cddb

annotations[f_name]=f_def[0]
fields[f_name]=f_def[1]
else:
annotations[f_name]=f_def

iff_def[0]:annotations[f_name]=f_def[0]fields[f_name]=f_def[1]else:annotations[f_name]=f_def

commonism added a commit to commonism/aiopenapi3 that referenced this pull requestApr 12, 2025
commonism added a commit to commonism/aiopenapi3 that referenced this pull requestApr 12, 2025
@Viicos
Copy link
MemberAuthor

there is nothing wrong in the expectation create_model can do the same.

There is, as I mention and this can be seen in the 2.10 documentation:

image

We haven't decided on a solution yet, but it will probably not be using the**kwargs.#11709 is the issue tracking this, and the implementation will be backported to 2.11.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@sydney-runklesydney-runklesydney-runkle approved these changes

Assignees
No one assigned
Labels
relnotes-changeUsed for changes to existing functionality which don't have a better categorization.
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Allow field names starting with underscore increate_model if they are class vars support multiple Field in Annotated for create_model()
3 participants
@Viicos@sydney-runkle@commonism

[8]ページ先頭

©2009-2025 Movatter.jp