-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Fix a crash when cleaning the namespace in ModelMetaclass
#10242
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
This happens when `MotelMetaclass` is used as a metaclass for a class other than `BaseModel`.
Deploying pydantic-docs with
|
| Latest commit: |
79ffa7c
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://d1d89a9d.pydantic-docs.pages.dev |
| Branch Preview URL: | https://metaclass-fix.pydantic-docs.pages.dev |
CodSpeed Performance ReportMerging #10242 will not alter performanceComparing Summary
|
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.
Can you add a test, even if it's weird and specific.
| @@ -243,7 +243,8 @@ def wrapped_model_post_init(self: BaseModel, context: Any, /) -> None: | |||
| else: | |||
| # These are instance variables, but have been assigned to `NoInitField` to trick the type checker. | |||
| for instance_slot in '__pydantic_fields_set__', '__pydantic_extra__', '__pydantic_private__': | |||
| del namespace[instance_slot] | |||
| if instance_slot in namespace: # Safe check in case the metaclass is used other than in `BaseModel`: | |||
| del namespace[instance_slot] | |||
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.
Surely, nameapace.pop(instance_slot, None) is better - only one lookup of the dict.
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 for the quick fix here!
In the future, let's break PRs like #10110 up into two - simple annotation order switches to adhere to type checker rules, and separately, actual code changes.
Well in this case the two were required to work together. |
|
Ah I see, fair enough. And the diff was small enough on the PR that things weren't too difficult to track down. |
This happens when
MotelMetaclassis used as a metaclass for a class other thanBaseModel.Fixes #10239
Change Summary
Related issue number
Checklist