Fix PEP487 __set_name__ protocol in BaseModel#4407
Merged
samuelcolvin merged 7 commits intopydantic:mainfrom Aug 21, 2022
Merged
Fix PEP487 __set_name__ protocol in BaseModel#4407samuelcolvin merged 7 commits intopydantic:mainfrom
__set_name__ protocol in BaseModel#4407samuelcolvin merged 7 commits intopydantic:mainfrom
Conversation
__set_name__ protocol in BaseModel__set_name__ protocol in BaseModel
Contributor
Author
|
please review |
Member
samuelcolvin
left a comment
There was a problem hiding this comment.
otherwise, LGTM.
Please update.
|
|
||
| class A(BaseModel): | ||
| @class_deco | ||
| def _some_func(self): |
Member
There was a problem hiding this comment.
can you also call _some_func to check it's a proper bound method.
Contributor
Author
There was a problem hiding this comment.
working on this one still, don't merge yet :)
Member
|
Looks good, thanks so much. |
5 tasks
samuelcolvin
added a commit
that referenced
this pull request
Aug 21, 2022
This reverts commit d501c39.
samuelcolvin
added a commit
that referenced
this pull request
Aug 22, 2022
This reverts commit d501c39.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Change Summary
This is a small change that brings BaseModel back into compliance with the the
__set_name__protocol defined in [PEP 487, and described in the creating the class object portion of the python documentation, which states:In the case of
BaseModel, some names in the original namespace (such as instances ofModelPrivateAttr) are stripped before callingtype.__new__with thenew_namespace. As such, objects implementing this__set_name__protocol are skipped.Rather than calling
type.__new__with the original namespace, this PR implements the protocol directly by calling__set_name__on any objects in the namespace that provide the method.Checklist
changes/<pull request or issue id>-<github username>.mdfile added describing change(see changes/README.md for details)
Context
For context, I've been using sqlmodel recently and would like to to implement this pattern
to bind object-specific listeners to various SqlAlchemy ORM events... however, this requires that the decorator know the class in which it is being called (which, among other things, seems to have been a primary motivator of
__set_name__)