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

plugin causes open() signature to change #16

Closed
arnimarj opened this issue Jan 24, 2020 · 6 comments · Fixed by #36
Closed

plugin causes open() signature to change #16

arnimarj opened this issue Jan 24, 2020 · 6 comments · Fixed by #36

Comments

@arnimarj
Copy link
Contributor

Hi,

I ran across an interesting issue. The signature of open() unexpectedly became typing.IO[Any] instead of typing.TextIO when the plugin is enabled.

example.py:

reveal_type(open('str'))

plugin.ini:

[mypy]
plugins = mypy_zope:plugin

Here is small test:

$ more example.py 
reveal_type(open('str'))
$ more plugin.ini 
[mypy]
plugins = mypy_zope:plugin
$ mypy --config plugin.ini example.py 
example.py:1: note: Revealed type is 'typing.IO[Any]'
$ mypy example.py 
example.py:1: note: Revealed type is 'typing.TextIO'

I created a test-case here:

master...arnimarj:bug/open-signature-changes

@kedder
Copy link
Member

kedder commented Jan 25, 2020

Interesting. This is triggered by overriding get_function_hook function in a plugin. typing.IO[Any] is what is being passed to it the hook in function_ctx.default_return_type. If hook returns this default type, mypy takes it as granted. If hook is not invoked, mypy infers type as typing.TextIO somehow.

This looks like a mypy bug to me. I'll submit the report upstream.

@kedder
Copy link
Member

kedder commented Jan 25, 2020

Apparently mypy doesn't consider this as mypy issue. This bug affects a feature that allows to guess type of a field in a schema, such as

    textline = zope.schema.TextLine(
        title='Title',
        description='Optional text line',
        required=False)

@arnimarj is there any practical issues with the type mismatch? Do you depend on this feature?

@arnimarj
Copy link
Contributor Author

I ran into the issue when passing down the return value of open down to a function which requires BinaryIO. This is the first time encountering this after having annotated in the order on 10k lines, so maybe not a big deal.

So in practice not a big deal, but if we start using more plugins that may change.

@kedder
Copy link
Member

kedder commented Jan 25, 2020

I see. I'm going to try suggestion from python/mypy#8331, maybe we can do something about it.

@arnimarj
Copy link
Contributor Author

arnimarj commented Nov 1, 2020

There is a open PR (python/mypy#9275) which removes the open plugin. Just tried the test-case above on that branch, and got this:

$ mypy --config plugin.ini example.py 
example.py:1: note: Revealed type is 'io.TextIOWrapper'

$ mypy example.py 
example.py:1: note: Revealed type is 'typing.TextIO'

@arnimarj
Copy link
Contributor Author

I just tested this on the latest mypy (0.800), and this is no longer an issue.

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 a pull request may close this issue.

2 participants