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

Type of open() changes if plugin overrides get_function_hook #8331

Closed
kedder opened this issue Jan 25, 2020 · 3 comments
Closed

Type of open() changes if plugin overrides get_function_hook #8331

kedder opened this issue Jan 25, 2020 · 3 comments

Comments

@kedder
Copy link

kedder commented Jan 25, 2020

When plugin overrides get_function_hook, the type of open function changes from typing.TextIO to typing.IO[Any].

Consider this trivial plugin (type annotations stripped for brevity):

class TrivialPlugin(Plugin):
    def get_function_hook(self, fullname):
        def analyze(function_ctx):
            return function_ctx.default_return_type
        return analyze

With this plugin enabled, reveal_type(open('str')) produces message: note: Revealed type is 'typing.IO[Any]'. Without plugin the type is typing.TextIO.

The issue was reported to mypy-zope pligin: Shoobx/mypy-zope#16.

@kedder kedder changed the title Type of open changes if plugin overrides get_function_hook Type of open() changes if plugin overrides get_function_hook Jan 25, 2020
@ilevkivskyi
Copy link
Member

ilevkivskyi commented Jan 25, 2020

This is part of how plugins work, only one plugin hook is called if there are multiple registered plugins. This is why we have this two-step logic: get_xxx_hook() should only return a non-None value if you are sure you should take care of this function, not other plugins, see https://mypy.readthedocs.io/en/stable/extending_mypy.html#high-level-overview

@kedder
Copy link
Author

kedder commented Jan 25, 2020

Hm, I see. The fullname provided to get_xxx_hook is not always enough to make a decision though. It would be nice to have a way to indicate that plugin doesn't want to change the type based on inspection of the context. I was under impression that function_ctx.default_return_type is such a mechanism, but apparently it is not.

@ilevkivskyi
Copy link
Member

There is some (very) limited API that you can use to make a bit deeper inspection than just fullname. For example you can use Plugin().lookup_fully_qualified() to retrieve the function node and get something from it to make the decision.

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

No branches or pull requests

2 participants