-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
improve codecs stubs #2114
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
improve codecs stubs #2114
Conversation
Started out as progress towards python#1476, but I ended up fixing a few more things: - fixed the signature of _encode_type, which actually returns a pair, not a string - made some attributes into properties in order to prevent the descriptor protocol from turning them into methods - found a bug in CPython in the process (python/cpython#6779) I used the following test file to make sure these classes are now instantiable: ```python import codecs import io from typing import IO bio = io.BytesIO() cod = codecs.lookup('utf-8') codecs.StreamReaderWriter(bio, codecs.StreamReader, codecs.StreamWriter) codecs.StreamRecoder(bio, cod.encode, cod.decode, codecs.StreamReader, codecs.StreamWriter) ```
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would understand if you don't want to keep tweaking this PR so I'm approving it at the same time as suggesting some extra work. Up to you!
@@ -33,8 +34,8 @@ _encoded = bytes | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's really too bad that this file uses all-lowercase names to indicate various type aliases. But maybe now's not the right time to fix that.
@@ -2,6 +2,7 @@ | |||
# https://docs.python.org/2/library/codecs.html and https://docs.python.org/3/library/codecs.html |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly the attribution comment does not belong here, nor does the value judgment.
@@ -217,7 +227,49 @@ class StreamReaderWriter(TextIO): | |||
def seek(self, offset: int, whence: int = ...) -> int: ... | |||
def __enter__(self: _T) -> _T: ... | |||
def __exit__(self, typ: Optional[Type[BaseException]], exc: Optional[BaseException], tb: Optional[types.TracebackType]) -> bool: ... | |||
def __getattr__(self, name: str) -> Any: ... | |||
|
|||
# These methods don't actually exist directly, but they are needed to satisfy the TextIO |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there no way to introduce these through inheritance?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They're abstract on the base classes, so we need to have them here to make these classes non-abstract. Maybe there's some class in io
that we could inherit from, but that wouldn't be accurate either since this class doesn't inherit from those classes at runtime.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, let's keep this (verbose) approach then.
Started out as progress towards python#1476, but I ended up fixing a few more things: - fixed the signature of _encode_type, which actually returns a pair, not a string - made some attributes into properties in order to prevent the descriptor protocol from turning them into methods - found a bug in CPython in the process (python/cpython#6779) I used the following test file to make sure these classes are now instantiable: ```python import codecs import io from typing import IO bio = io.BytesIO() cod = codecs.lookup('utf-8') codecs.StreamReaderWriter(bio, codecs.StreamReader, codecs.StreamWriter) codecs.StreamRecoder(bio, cod.encode, cod.decode, codecs.StreamReader, codecs.StreamWriter) ```
Started out as progress towards #1476, but I ended up fixing a few more things:
I used the following test file to make sure these classes are now instantiable: