Thanks to visit codestin.com
Credit goes to github.com

Skip to content

a better definition for io.IOBase #1160

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 3 commits into from
Apr 17, 2017
Merged

a better definition for io.IOBase #1160

merged 3 commits into from
Apr 17, 2017

Conversation

matthiaskramm
Copy link
Contributor

This is aiming to make the following code pass type checking:

shutil.copyfileobj(gzip.GzipFile("/tmp/test.txt.gz", "r"),
                   gzip.GzipFile("/tmp/test.txt.gz", "w"))

@matthiaskramm matthiaskramm merged commit 7e58389 into master Apr 17, 2017
@matthiaskramm matthiaskramm deleted the io branch April 17, 2017 16:43
@gvanrossum
Copy link
Member

But it doesn't seem to work. When I check the above example with mypy I still get errors:
tmp.py contains:

import gzip, shutil
shutil.copyfileobj(gzip.GzipFile("/tmp/test.txt.gz", "r"),
                   gzip.GzipFile("/tmp/test.txt.gz", "w"))

and mypy -2 __tmp__.py (using the latest master) gives

__tmp__.py:2: error: Cannot instantiate abstract class 'GzipFile' with abstract attributes '__enter__', '__exit__', ... and 'writelines' (6 methods suppressed)
__tmp__.py:3: error: Cannot instantiate abstract class 'GzipFile' with abstract attributes '__enter__', '__exit__', ... and 'writelines' (6 methods suppressed)

That's a different error than before but still an error. :-(

@gvanrossum
Copy link
Member

(It's actually worse because you get this error on any instantiation of GzipFile.)

gvanrossum pushed a commit that referenced this pull request Apr 18, 2017
This solves the issue with instantiating gzip.GzipFile() mentioned at
#1160 (comment)
gvanrossum added a commit that referenced this pull request Apr 18, 2017
This solves the issue with instantiating gzip.GzipFile() mentioned at
#1160 (comment)
@matthiaskramm
Copy link
Contributor Author

Oops, missed this yesterday. Right, mypy's version of typing.pyi (a.k.a. the typeshed version) declares most methods in IO abstract. Maybe that's correct in cases where you inherit from IO in your .py, but in .pyi, it seems to cause trouble. Hopefully, with PEP 544, explicit subclassing of IO won't be needed anymore, and then maybe we can just make the methods in typing.pyi concrete.

In any case, sorry for breaking this! I was testing with pytype's typing.pyi (https://github.com/google/pytype/blob/master/pytype/pytd/builtins/typing.pytd) which looks slightly different.

@gvanrossum
Copy link
Member

Well, the IO class in typing.py (i.e. at runtime) really is abstract, but the various classes defined in io and _io (at runtime) are concrete, and I think typeshed should follow -- IO in typing.pyi should be abstract but the classes in io.pyi and _io.pyi should be concrete. Which is what my PR already did. I just don't think there's a future action item.

@JelleZijlstra JelleZijlstra mentioned this pull request Jul 11, 2017
25 tasks
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