From 1139a32b264316a9f92afb4727c0d252aa13b7a0 Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Mon, 25 Nov 2024 13:01:30 -0800 Subject: [PATCH 1/4] add encodings.ascii and encodings.latin_1 --- stdlib/@tests/stubtest_allowlists/common.txt | 2 -- stdlib/encodings/ascii.pyi | 21 ++++++++++++++++++++ stdlib/encodings/latin_1.pyi | 21 ++++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 stdlib/encodings/ascii.pyi create mode 100644 stdlib/encodings/latin_1.pyi diff --git a/stdlib/@tests/stubtest_allowlists/common.txt b/stdlib/@tests/stubtest_allowlists/common.txt index 5f8c42c706c6..b38cfedc5743 100644 --- a/stdlib/@tests/stubtest_allowlists/common.txt +++ b/stdlib/@tests/stubtest_allowlists/common.txt @@ -59,13 +59,11 @@ weakref.WeakValueDictionary.update # TODO: Modules that exist at runtime, but are missing from stubs # ========== encodings.aliases -encodings.ascii encodings.base64_codec encodings.bz2_codec encodings.charmap encodings.hex_codec encodings.idna -encodings.latin_1 encodings.punycode encodings.quopri_codec encodings.raw_unicode_escape diff --git a/stdlib/encodings/ascii.pyi b/stdlib/encodings/ascii.pyi new file mode 100644 index 000000000000..fa7bdf60c9fa --- /dev/null +++ b/stdlib/encodings/ascii.pyi @@ -0,0 +1,21 @@ +import codecs +from _typeshed import ReadableBuffer + +class Codec(codecs.Codec): + encode = codecs.ascii_encode # type: ignore[assignment] # pyright: ignore[reportAssignmentType] + decode = codecs.ascii_decode # type: ignore[assignment] # pyright: ignore[reportAssignmentType] + +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): ... + +class StreamConverter(StreamWriter, StreamReader): # type: ignore[misc] # incompatible methods in base classes + encode = codecs.ascii_decode # type: ignore[assignment] # pyright: ignore[reportAssignmentType] + decode = codecs.ascii_encode # type: ignore[assignment] # pyright: ignore[reportAssignmentType] + +def getregentry() -> codecs.CodecInfo: ... diff --git a/stdlib/encodings/latin_1.pyi b/stdlib/encodings/latin_1.pyi new file mode 100644 index 000000000000..bf531381de77 --- /dev/null +++ b/stdlib/encodings/latin_1.pyi @@ -0,0 +1,21 @@ +import codecs +from _typeshed import ReadableBuffer + +class Codec(codecs.Codec): + encode = codecs.latin_1_encode # type: ignore[assignment] # pyright: ignore[reportAssignmentType] + decode = codecs.latin_1_decode # type: ignore[assignment] # pyright: ignore[reportAssignmentType] + +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): ... + +class StreamConverter(StreamWriter, StreamReader): # type: ignore[misc] # incompatible methods in base classes + encode = codecs.latin_1_decode # type: ignore[assignment] # pyright: ignore[reportAssignmentType] + decode = codecs.latin_1_encode # type: ignore[assignment] # pyright: ignore[reportAssignmentType] + +def getregentry() -> codecs.CodecInfo: ... From 2f4384e31cc8c4120d7e3fa8e1d66656e3cb990c Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Mon, 25 Nov 2024 13:08:20 -0800 Subject: [PATCH 2/4] pyright --- stdlib/encodings/ascii.pyi | 4 ++-- stdlib/encodings/latin_1.pyi | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/stdlib/encodings/ascii.pyi b/stdlib/encodings/ascii.pyi index fa7bdf60c9fa..09d9c2e21705 100644 --- a/stdlib/encodings/ascii.pyi +++ b/stdlib/encodings/ascii.pyi @@ -15,7 +15,7 @@ class StreamWriter(Codec, codecs.StreamWriter): ... class StreamReader(Codec, codecs.StreamReader): ... class StreamConverter(StreamWriter, StreamReader): # type: ignore[misc] # incompatible methods in base classes - encode = codecs.ascii_decode # type: ignore[assignment] # pyright: ignore[reportAssignmentType] - decode = codecs.ascii_encode # type: ignore[assignment] # pyright: ignore[reportAssignmentType] + encode = codecs.ascii_decode # type: ignore[assignment] + decode = codecs.ascii_encode # type: ignore[assignment] def getregentry() -> codecs.CodecInfo: ... diff --git a/stdlib/encodings/latin_1.pyi b/stdlib/encodings/latin_1.pyi index bf531381de77..4e65d6ce040d 100644 --- a/stdlib/encodings/latin_1.pyi +++ b/stdlib/encodings/latin_1.pyi @@ -15,7 +15,7 @@ class StreamWriter(Codec, codecs.StreamWriter): ... class StreamReader(Codec, codecs.StreamReader): ... class StreamConverter(StreamWriter, StreamReader): # type: ignore[misc] # incompatible methods in base classes - encode = codecs.latin_1_decode # type: ignore[assignment] # pyright: ignore[reportAssignmentType] - decode = codecs.latin_1_encode # type: ignore[assignment] # pyright: ignore[reportAssignmentType] + encode = codecs.latin_1_decode # type: ignore[assignment] + decode = codecs.latin_1_encode # type: ignore[assignment] def getregentry() -> codecs.CodecInfo: ... From db12fbfea53b97e149a1567b54051f0351ff1f86 Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Tue, 26 Nov 2024 11:50:50 -0800 Subject: [PATCH 3/4] use override methods instead of directly assigning the function. --- stdlib/encodings/ascii.pyi | 17 +++++++++++++---- stdlib/encodings/latin_1.pyi | 17 +++++++++++++---- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/stdlib/encodings/ascii.pyi b/stdlib/encodings/ascii.pyi index 09d9c2e21705..77605a74173b 100644 --- a/stdlib/encodings/ascii.pyi +++ b/stdlib/encodings/ascii.pyi @@ -2,8 +2,12 @@ import codecs from _typeshed import ReadableBuffer class Codec(codecs.Codec): - encode = codecs.ascii_encode # type: ignore[assignment] # pyright: ignore[reportAssignmentType] - decode = codecs.ascii_decode # type: ignore[assignment] # pyright: ignore[reportAssignmentType] + # At runtime, this is codecs.ascii_encode + @staticmethod + def encode(str: str, errors: str | None = None, /) -> tuple[bytes, int]: ... # type: ignore[override] + # At runtime, this is codecs.ascii_decode + @staticmethod + def decode(data: ReadableBuffer, errors: str | None = None, /) -> tuple[str, int]: ... # type: ignore[override] class IncrementalEncoder(codecs.IncrementalEncoder): def encode(self, input: str, final: bool = False) -> bytes: ... @@ -14,8 +18,13 @@ class IncrementalDecoder(codecs.IncrementalDecoder): 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 - encode = codecs.ascii_decode # type: ignore[assignment] - decode = codecs.ascii_encode # type: ignore[assignment] + # 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: ... diff --git a/stdlib/encodings/latin_1.pyi b/stdlib/encodings/latin_1.pyi index 4e65d6ce040d..5b9773b867ca 100644 --- a/stdlib/encodings/latin_1.pyi +++ b/stdlib/encodings/latin_1.pyi @@ -2,8 +2,12 @@ import codecs from _typeshed import ReadableBuffer class Codec(codecs.Codec): - encode = codecs.latin_1_encode # type: ignore[assignment] # pyright: ignore[reportAssignmentType] - decode = codecs.latin_1_decode # type: ignore[assignment] # pyright: ignore[reportAssignmentType] + # At runtime, this is codecs.latin_1_encode + @staticmethod + def encode(str: str, errors: str | None = None, /) -> tuple[bytes, int]: ... # type: ignore[override] + # At runtime, this is codecs.latin_1_decode + @staticmethod + def decode(data: ReadableBuffer, errors: str | None = None, /) -> tuple[str, int]: ... # type: ignore[override] class IncrementalEncoder(codecs.IncrementalEncoder): def encode(self, input: str, final: bool = False) -> bytes: ... @@ -14,8 +18,13 @@ class IncrementalDecoder(codecs.IncrementalDecoder): 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 - encode = codecs.latin_1_decode # type: ignore[assignment] - decode = codecs.latin_1_encode # type: ignore[assignment] + # 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: ... From 4b47e75f82bad14a824b705682ea191c4e005f7f Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Tue, 26 Nov 2024 12:06:19 -0800 Subject: [PATCH 4/4] remove unneeded ignores --- stdlib/encodings/ascii.pyi | 4 ++-- stdlib/encodings/latin_1.pyi | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/stdlib/encodings/ascii.pyi b/stdlib/encodings/ascii.pyi index 77605a74173b..a85585af32ed 100644 --- a/stdlib/encodings/ascii.pyi +++ b/stdlib/encodings/ascii.pyi @@ -4,10 +4,10 @@ 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]: ... # type: ignore[override] + 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]: ... # type: ignore[override] + def decode(data: ReadableBuffer, errors: str | None = None, /) -> tuple[str, int]: ... class IncrementalEncoder(codecs.IncrementalEncoder): def encode(self, input: str, final: bool = False) -> bytes: ... diff --git a/stdlib/encodings/latin_1.pyi b/stdlib/encodings/latin_1.pyi index 5b9773b867ca..3b06773eac03 100644 --- a/stdlib/encodings/latin_1.pyi +++ b/stdlib/encodings/latin_1.pyi @@ -4,10 +4,10 @@ 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]: ... # type: ignore[override] + 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]: ... # type: ignore[override] + def decode(data: ReadableBuffer, errors: str | None = None, /) -> tuple[str, int]: ... class IncrementalEncoder(codecs.IncrementalEncoder): def encode(self, input: str, final: bool = False) -> bytes: ...