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

add encodings.ascii and encodings.latin_1 #13113

Merged
merged 6 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions stdlib/@tests/stubtest_allowlists/common.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ weakref.WeakValueDictionary.update
# ==========
# TODO: Modules that exist at runtime, but are missing from stubs
# ==========
encodings.ascii
encodings.latin_1
turtledemo
turtledemo\..+
xml.sax.expatreader
Expand Down
30 changes: 30 additions & 0 deletions stdlib/encodings/ascii.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import codecs
from _typeshed import ReadableBuffer

class Codec(codecs.Codec):
# At runtime, this is codecs.ascii_encode
@staticmethod
def encode(str: str, errors: str | None = None, /) -> tuple[bytes, int]: ...
# At runtime, this is codecs.ascii_decode
@staticmethod
def decode(data: ReadableBuffer, errors: str | None = None, /) -> tuple[str, int]: ...

class IncrementalEncoder(codecs.IncrementalEncoder):
def encode(self, input: str, final: bool = False) -> bytes: ...

class IncrementalDecoder(codecs.IncrementalDecoder):
def decode(self, input: ReadableBuffer, final: bool = False) -> str: ...

class StreamWriter(Codec, codecs.StreamWriter): ...
class StreamReader(Codec, codecs.StreamReader): ...

# Note: encode being a decode function and decode being an encode function is accurate to runtime.
Copy link
Member

Choose a reason for hiding this comment

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

Indeed, is that a bug in the runtime?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure. StreamConverter only exists on ascii and latin_1 encodings. Both do this, and there's nothing in the stdlib that makes use of them, so I don't know how they're intended to be used. I wondered the same thing, but I can't rule out that this is intended.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They've also been this way for almost 25 years now.

It's possible that no one has ever used these classes for anything.

class StreamConverter(StreamWriter, StreamReader): # type: ignore[misc] # incompatible methods in base classes
# At runtime, this is codecs.ascii_decode
@staticmethod
def encode(data: ReadableBuffer, errors: str | None = None, /) -> tuple[str, int]: ... # type: ignore[override]
# At runtime, this is codecs.ascii_encode
@staticmethod
def decode(str: str, errors: str | None = None, /) -> tuple[bytes, int]: ... # type: ignore[override]

def getregentry() -> codecs.CodecInfo: ...
30 changes: 30 additions & 0 deletions stdlib/encodings/latin_1.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import codecs
from _typeshed import ReadableBuffer

class Codec(codecs.Codec):
# At runtime, this is codecs.latin_1_encode
@staticmethod
def encode(str: str, errors: str | None = None, /) -> tuple[bytes, int]: ...
# At runtime, this is codecs.latin_1_decode
@staticmethod
def decode(data: ReadableBuffer, errors: str | None = None, /) -> tuple[str, int]: ...

class IncrementalEncoder(codecs.IncrementalEncoder):
def encode(self, input: str, final: bool = False) -> bytes: ...

class IncrementalDecoder(codecs.IncrementalDecoder):
def decode(self, input: ReadableBuffer, final: bool = False) -> str: ...

class StreamWriter(Codec, codecs.StreamWriter): ...
class StreamReader(Codec, codecs.StreamReader): ...

# Note: encode being a decode function and decode being an encode function is accurate to runtime.
class StreamConverter(StreamWriter, StreamReader): # type: ignore[misc] # incompatible methods in base classes
# At runtime, this is codecs.latin_1_decode
@staticmethod
def encode(data: ReadableBuffer, errors: str | None = None, /) -> tuple[str, int]: ... # type: ignore[override]
# At runtime, this is codecs.latin_1_encode
@staticmethod
def decode(str: str, errors: str | None = None, /) -> tuple[bytes, int]: ... # type: ignore[override]

def getregentry() -> codecs.CodecInfo: ...