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
Fix PEP487 __set_name__
protocol in BaseModel
#4407
Conversation
__set_name__
protocol in BaseModel__set_name__
protocol in BaseModel
please review |
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.
otherwise, LGTM.
Please update.
|
||
class A(BaseModel): | ||
@class_deco | ||
def _some_func(self): |
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 also call _some_func
to check it's a proper bound method.
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.
working on this one still, don't merge yet :)
Looks good, thanks so much. |
This reverts commit d501c39.
This reverts commit d501c39.
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>.md
file 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__
)