Skip to content

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

Merged
merged 9 commits into from
Feb 10, 2019

Conversation

lincolnq
Copy link
Contributor

@lincolnq lincolnq commented Feb 10, 2019

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.)

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
Copy link
Member

@ilevkivskyi ilevkivskyi left a 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.)

@lincolnq
Copy link
Contributor Author

@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 mx.msg.has_no_attr. Is the way I did it ok?

Copy link
Member

@ilevkivskyi ilevkivskyi left a 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?

@lincolnq
Copy link
Contributor Author

lincolnq commented Feb 10, 2019 via email

…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.
@ilevkivskyi
Copy link
Member

I assume the documented behavior is the desirable one?

Yes, I think giving the name of the base class where __getattr__() was defined would be consistent. So you probably need to update your code. (The fix for the old test I posted above.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants