-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Use the get_attribute hook for dynamic classes #6371
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
Use the get_attribute hook for dynamic classes #6371
Conversation
Whenever we find __getattr__ or __getattribute__ on an instance, the getattribute hook is used. (It continues to be used whenever we find a method or variable on an instance) Closes python#6259, python#5910
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 PR! I have couple comments. Could you also please add tests? (You can look at the existing tests for plugin hooks to see how to add them.)
@ilevkivskyi Thanks for the review! I made the changes you requested. One of the test cases is that the attribute hook can produce an error. I invented my own error, "Field does not exist", but ideally I would test that you can fallback to the normal mypy error handling -- but I don't think you can access a MemberContext to call |
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 updates!
Sorry, there is one more thing, could you please also update the docstring for get_attribute_hook()
in mypy/plugin.py
?
Hm, ~~the old docstring was wrong too~~ (it specifies that the hook will be
called with the base class name where the field is defined, but it is
called with the derived class name). I assume the documented behavior is
the desirable one?
[edit: not sure if old docstring was wrong anymore, investigating.]
|
…dynamic class. - Also start updating the docstring in plugin.py, although not sure yet what proper behavior is
… implementation, not the class of the type of the field we are resolving - Also make the other test I wrote work correctly.
Yes, I think giving the name of the base class where |
Whenever we find
__getattr__
or__getattribute__
on an instance, the getattribute hook is called to give the hook a chance to modify the return type of one of those functions.Closes #6259, #5910
(Note: Currently there are no tests. I can add them if important.)