Skip to content

Bug? Inconsistent type inference between open() and Path().open() #6666

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

Closed
BuongiornoTexas opened this issue Apr 12, 2019 · 4 comments
Closed

Comments

@BuongiornoTexas
Copy link

After spending quite a while one this, I'm not sure if this is an intended mode of operation, a mypy issue or a typeshed issue. If I have this in the wrong place or if it is not an issue, please close and I will re-raise in typeshed if appropriate.

I have run into an issue with inconsistent type-inferences for open() and pathlib.Path().open().

#!/usr/bin/env python3

from pathlib import Path
fp = open('README.rst', 'rt')
print(type(fp))
fp1 = Path('README.rst').open('rt')
print(type(fp1))

yields:

<class '_io.TextIOWrapper'>
<class '_io.TextIOWrapper'>

However, running mypy reveal_type on fp and fp1 yields:

Revealed type is 'typing.TextIO'
Revealed type is 'typing.IO[Any]'

I would expect both of these to reveal typing.TextIO. I found this issue when using a routine that switched TextIO output between sys.stdout (TextIO) and a file opened with Path().open() (IO).

This is on python 3.7.2 and mypy 0.700 with arguments: --follow-imports=silent, --show-column-numbers, --disallow-untyped-defs.

@gvanrossum
Copy link
Member

Right now we have a plugin that special-cases builtins.open() but not Path.open(). This should be solved in typeshed by making the definition of Path.open() overloaded on various Literal types. So I am closing this as "not a mypy bug" but I recommend that you submit a PR to typeshed to add this.

@BuongiornoTexas
Copy link
Author

Thanks. Will do.

I figured with an issue number of 6666, this was always going to be short lived :-).

@gvanrossum
Copy link
Member

Well, there's always https://en.wikipedia.org/wiki/Chinese_numerology#Six

@ilevkivskyi
Copy link
Member

Superseded by python/typeshed#2911

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

3 participants