8000 Allow config and bases to be specified together in `create_model()` by Viicos · Pull Request #11714 · pydantic/pydantic · GitHub
[go: up one dir, main page]

Skip to content

Allow config and bases to be specified together in create_model() #11714

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

Merged
merged 1 commit into from
Apr 9, 2025

Conversation

Viicos
Copy link
Member
@Viicos Viicos commented Apr 8, 2025

Change Summary

Fixes #2137.

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-actions github-actions bot added the relnotes-fix Used for bugfixes. label Apr 8, 2025
Copy link
Contributor
hyperlint-ai bot commented Apr 8, 2025

PR Change Summary

Allowed configuration and base classes to be specified together in the create_model() function, addressing a previously raised issue.

  • Enabled the use of both __config__ and __base__ in create_model()
  • Removed outdated error handling example related to create_model configuration
  • Updated documentation to reflect the new functionality

Modified Files

  • docs/errors/usage_errors.md

How can I customize these reviews?

Check out the Hyperlint AI Reviewer docs for more information on how to customize the review.

If you just want to ignore it on this PR, you can add the hyperlint-ignore label to the PR. Future changes won't trigger a Hyperlint review.

Note specifically for link checks, we only check the first 30 links in a file and we cache the results for several hours (for instance, if you just added a page, you might experience this). Our recommendation is to add hyperlint-ignore to the PR to ignore the link check for this PR.

Copy link
cloudflare-workers-and-pages bot commented Apr 8, 2025

Deploying pydantic-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 7a702a7
Status: ✅  Deploy successful!
Preview URL: https://5cf8e1f1.pydantic-docs.pages.dev
Branch Preview URL: https://create-model-config-base.pydantic-docs.pages.dev

View logs

Copy link
codspeed-hq bot commented Apr 8, 2025

CodSpeed Performance Report

Merging #11714 will not alter performance

Comparing create-model-config-base (7a702a7) with main (04fd639)

Summary

✅ 46 untouched benchmarks

@Viicos Viicos force-pushed the create-model-config-base branch from 46d0e9e to 7a702a7 Compare April 8, 2025 10:32
@Viicos Viicos added relnotes-change Used for changes to existing functionality which don't have a better categorization. and removed relnotes-fix Used for bugfixes. labels Apr 8, 2025
Copy link
Contributor
github-actions bot commented Apr 8, 2025

Coverage report

Click to see where and how coverage changed

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

This report was generated by python-coverage-comment-action

Copy link
Contributor
@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume that the original limitation was added because inheritance from a base class will set config. How does that config merge with the explicitly set config? Is that where confusion may occur? (e.g. can this do weird things like make the parent model fields all strict?)

8000

@Viicos
Copy link
Member Author
Viicos commented Apr 8, 2025

How does that config merge with the explicitly set config?

It will be equivalent to the following, as shown in the added test:

class Base(BaseModel):
        a: str

        model_config = {'str_to_lower': True}

class Model(Base):
    model_config = {'str_max_length': 3}

Which will result in Model having both configuration values. create_model() is in fact completely equivalent to normal subclassing, just that it is meant to be dynamic.

(e.g. can this do weird things like make the parent model fields all strict?)

as in "normal" subclassing, this shouldn't be the case.

Copy link
Contributor
@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok, that justification makes sense to me. LGTM with just one question.

@Viicos Viicos merged commit 8f6508a into main Apr 9, 2025
64 checks passed
@Viicos Viicos deleted the create-model-config-base branch April 9, 2025 09:35
@Viicos Viicos added the backport-2.11 Needs backport to 2.11 label Apr 29, 2025
Viicos added a commit that referenced this pull request Apr 29, 2025
The limitation was added back in the early days of Pydantic, when `create_model()` was implemented. It is no longer necessary.

Backport of: #11714
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-2.11 Needs backport to 2.11 relnotes-change Used for changes to existing functionality which don't have a better categorization.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide a Base and a Config in create_model
2 participants
0