Skip to content
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

get_attribute_hook for unknown attributes #5910

Closed
mkurnikov opened this issue Nov 17, 2018 · 2 comments
Closed

get_attribute_hook for unknown attributes #5910

mkurnikov opened this issue Nov 17, 2018 · 2 comments
Labels
bug mypy got something wrong priority-1-normal topic-plugins The plugin API and ideas for new plugins

Comments

@mkurnikov
Copy link
Contributor

How do I intercept type inference of the settings.ROOT_DIR here?

from django.conf import settings
settings.ROOT_DIR

I wasn't able to get it working neither with get_attribute_hook, nor with others. settings is specified as

# in django/conf/__init__.pyi
class LazySettings(object):
    pass
settings = LazySettings()

From the implementation, it looks like hooks's only called if attribute is present on the object (is a Var)
https://github.com/python/mypy/blob/master/mypy/checkmember.py#L280

Another solution for this problem could be to augment the AST with an additional settings dynamically somewhere in the plugin, but for that I need an initialization hook.

@ilevkivskyi
Copy link
Member

You can stuff various dummy names in the class symbol table using get_class_decorator_hook(), get_metaclass_hook() and get_base_class_hook() (plus something in stubs). But I think it probably makes sense to call the hook in more cases there (e.g. if it comes from __getattr__() etc.)

@ilevkivskyi ilevkivskyi added bug mypy got something wrong priority-1-normal topic-plugins The plugin API and ideas for new plugins labels Nov 17, 2018
lincolnq added a commit to lincolnq/mypy that referenced this issue Feb 10, 2019
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
ilevkivskyi pushed a commit that referenced this issue 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
@ilevkivskyi
Copy link
Member

Fixed by #6371

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong priority-1-normal topic-plugins The plugin API and ideas for new plugins
Projects
None yet
Development

No branches or pull requests

2 participants