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

Skip to content

improve io #194

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 5 commits into from
May 11, 2016
Merged

improve io #194

merged 5 commits into from
May 11, 2016

Conversation

tharvik
Copy link
Contributor

@tharvik tharvik commented May 10, 2016

add some type checking to io and _io.

I've moved everything needed from _io to io as it was public anyway.

I've had to fix lxml as it was not using the same signature as the parent

class _ListDataStream(io.BufferedIOBase):
      def write(self, b: str) -> None: ...

I decided to add a @no_type_check, but it may not be the best way.

Also, there seems to be some small differences between some python version (__del__ for IOBase in python3.5, BlockingIOError which goes from io to builtin, ...), is there a way to avoid coping the nearly identical module everywhere?

@tharvik tharvik force-pushed the improve_io branch 3 times, most recently from 0b342d0 to a7f9e30 Compare May 10, 2016 10:50
@@ -80,6 +80,7 @@ class _ListDataStream(io.BufferedIOBase):
def __init__(self, lst) -> None: ...
def writable(self) -> bool: ...
def seekable(self) -> bool: ...
@no_type_check # not same signature as parent
def write(self, b: str) -> None: ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend using # type: ignore here instead of @no_type_check. The latter means that the annotations aren't even in mypy's language; the former just suppresses the complaint about the type mismatch.

Though maybe there's a different solution possible altogether? Having to suppress this smells a bit...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAICT from some experimentation, write() actually takes bytes too. But since this is an internal class used only by tostringlist() maybe it's better to get rid of it?

Copy link
Contributor Author

@tharvik tharvik May 11, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAICT from some experimentation, write() actually takes bytes too. But since this is an internal class used only by tostringlist() maybe it's better to get rid of it?

Removed then.

@gvanrossum
Copy link
Member

Also, there seems to be some small differences between some python version (del for IOBase in python3.5, BlockingIOError which goes from io to builtin, ...), is there a way to avoid coping the nearly identical module everywhere?

There certainly is -- use checks for sys.version_info. While mypy doesn't currently implement these, it will just assume any if you put in there may evaluate to True so it will always type check everything as if all definitions were present. As long as there aren't incompatibilities this shouldn't matter.

IIRC it's just a historical accident that the xml.etree stubs were repeated (maybe at the time we wanted them to be version-precise despite mypy not implementing version checks yet, but personally I don't think that's important).

@tharvik tharvik force-pushed the improve_io branch 4 times, most recently from 22d26b2 to c253a77 Compare May 11, 2016 11:30
@tharvik
Copy link
Contributor Author

tharvik commented May 11, 2016

Travis gives me

stdlib/3/io.pyi:25: error: Name 'BlockingIOError' already defined
stdlib/3/io.pyi:27: error: Name 'UnsupportedOperation' already defined

for

if sys.version_info >= (3, 3): 
    BlockingIOError = BlockingIOError
    class UnsupportedOperation(OSError, ValueError): ... 
else:
    class BlockingIOError(IOError):
        characters_written = ...  # type: int
    class UnsupportedOperation(IOError, ValueError): ... 

I guess it is a mypy issue, because any path would only declare these once, should I report it? For now, even if it is somewhat wrong, we can keep the >= 3.3 version, and fix it when mypy handle conditional better (done in 0336639).

SEEK_SET = ... # type: int
SEEK_CUR = ... # type: int
SEEK_END = ... # type: int
if sys.version_info >= (3, 1):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mypy doesn't support Python 3.0 or 3.1, so you can put this block in unconditionally.

@gvanrossum
Copy link
Member

Yeah, there are several mypy issue still open related to conditional definitions.

Anyway, please get rid of the 3.1 version checks; the rest looks good so far.

@tharvik
Copy link
Contributor Author

tharvik commented May 11, 2016

Done. It's somewhat more readable now.

@gvanrossum
Copy link
Member

There are so many changes here, this feel risky, but I'm going to risk it. We'll see if someone reports problems.

@gvanrossum gvanrossum merged commit 9fdac6e into python:master May 11, 2016
@gvanrossum
Copy link
Member

Sadly this breaks some mypy tests: https://travis-ci.org/python/mypy/jobs/129495880

FAILURE  #180 check stdlibsamples (3.2)
test/test_pprint.py:1: note: In module imported here:
pprint.py: note: In member "pformat" of class "PrettyPrinter":
pprint.py:142: error: Argument 2 to "_format" of "PrettyPrinter" has incompatible type "StringIO"; expected "TextIO"
test/test_base64.py:6: note: In module imported here:
subprocess.py: note: In member "__init__" of class "Popen":
subprocess.py:741: error: Unexpected keyword argument "write_through" for "TextIOWrapper"
subprocess.py:741: error: Argument 1 to "TextIOWrapper" has incompatible type IO[Any]; expected "BufferedIOBase"
subprocess.py:741: error: Incompatible types in assignment (expression has type "TextIOWrapper", variable has type IO[Any])
subprocess.py:745: error: Argument 1 to "TextIOWrapper" has incompatible type IO[Any]; expected "BufferedIOBase"
subprocess.py:745: error: Incompatible types in assignment (expression has type "TextIOWrapper", variable has type IO[Any])
subprocess.py:749: error: Argument 1 to "TextIOWrapper" has incompatible type IO[Any]; expected "BufferedIOBase"
subprocess.py:749: error: Incompatible types in assignment (expression has type "TextIOWrapper", variable has type IO[Any])
test/test_base64.py: note: In member "test_encode" of class "LegacyBase64TestCase":
test/test_base64.py:50: error: Argument 1 to "encode" has incompatible type "BytesIO"; expected IO[bytes]
test/test_base64.py:50: error: Argument 2 to "encode" has incompatible type "BytesIO"; expected IO[bytes]
test/test_base64.py: note: In member "test_decode" of class "LegacyBase64TestCase":
test/test_base64.py:60: error: Argument 1 to "decode" has incompatible type "BytesIO"; expected IO[bytes]
test/test_base64.py:60: error: Argument 2 to "decode" has incompatible type "BytesIO"; expected IO[bytes]

Please see what you can do about this...

@tharvik tharvik deleted the improve_io branch May 14, 2016 10:25
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