-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
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
Deprecate and schedule removal of collections.abc.ByteString and typing.ByteString #91896
Comments
The general rule is that we can never add methods to ABCs once they are published. The purpose of an ABC is promise that a minimal set of methods are available. If Perhaps because this is an empty ABC that is almost entirely unused, this might be okay. On the other hand, because it is an empty ABC that is almost entirely unused, there is almost nothing to be gained by adding it. AFAICT no one has ever asked for or needed this since the ABC was added 15 years ago. That suggests that there is no problem to be solved here. |
Right, the ABC is hardly useful with no methods. I searched on
Well, I'm asking for it now. The use case is type annotating code that accepts both |
If you were to do it correctly, the procedure would be to subclass ByteString and add the new methods in a subclass. Loosely, this is similar to why we had to add IterableUserDict in Python 2 rather than modifying the existing UserDict. To annotate code that accepts both bytes or bytearray wouldn't the correct way be to write: One other thought: the name ByteString wasn't very good to begin as is suggests |
I forgot to mention that the docs promise something narrower that the bytes/bytearray API. It says, "ABCs for read-only and mutable sequences". There is no promise of the extra methods found in bytes or bytearray. Existing code reasonably by subclassing from ByteString and not providing or expecting any of the stringlike methods. All around, I think this proposal is a contract violation and that a new ABC should be created. A scan on grep.app is insufficient to show this won't be a breaking change (most of the world's Python code isn't publicly visible). |
That documentation is for Sequence, MutableSequence, and ByteString together (https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence). "Read-only and mutable sequences" is a good description for the first two, but the documentation really doesn't tell me what ByteString is good for. ByteString is also documented at https://docs.python.org/3/library/typing.html#typing.ByteString, but that documentation has a couple of problems:
That's a reasonable point. If we can't use the existing ByteString ABC for But if we keep ByteString as is, with no methods, I have no idea what it's useful for. We occasionally get people trying to use the ABC in type annotations, so the current state causes confusion. Perhaps we could deprecate ByteString, or explicitly document its limited use. |
I vote for deprecation because the name is bad (implying |
The name is good to me. It implies the bytes-like object with The terms "buffer", "bytes-like", "bytestring" and "bytes string" are used loosely in the documentation, but there are several meanings of bytes-likeness:
Unfortunately there are no strongly defined terms and corresponding abstract classes, protocols or types in the code to express the requirements precisely. |
Hi! I'm one of the contributors to the pygame project. I'm not sure whether this is the best best place to be asking, but it is relevant to the usage of So we have a function implemented with the python C API, and it uses I was looking for a suitable ABC to typehint this, and the closest thing I could find that already exists is I suppose a set of ABCs for the buffer protocol (if this were to be added) would be the closest replacement to |
The problem with buffers is that it doesn't have a visible Python API, so the ABC would be really weird, being entirely empty. See previous discussion at python/typing#593 and then PEP 688. |
Getting a DeprecationWarning on issubclass proved to be difficult, because it could affect unrelated looking things like `isinstance(bytes, Sequence)`
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com> Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
* main: (61 commits) pythongh-64595: Argument Clinic: Touch source file if any output file changed (python#104152) pythongh-64631: Test exception messages in cloned Argument Clinic funcs (python#104167) pythongh-68395: Avoid naming conflicts by mangling variable names in Argument Clinic (python#104065) pythongh-64658: Expand Argument Clinic return converter docs (python#104175) pythonGH-103092: port `_asyncio` freelist to module state (python#104196) pythongh-104051: fix crash in test_xxtestfuzz with -We (python#104052) pythongh-104190: fix ubsan crash (python#104191) pythongh-104106: Add gcc fallback of mkfifoat/mknodat for macOS (pythongh-104129) pythonGH-104142: Fix _Py_RefcntAdd to respect immortality (pythonGH-104143) pythongh-104112: link from cached_property docs to method-caching FAQ (python#104113) pythongh-68968: Correcting message display issue with assertEqual (python#103937) pythonGH-103899: Provide a hint when accidentally calling a module (pythonGH-103900) pythongh-103963: fix 'make regen-opcode' in out-of-tree builds (python#104177) pythongh-102500: Add PEP 688 and 698 to the 3.12 release highlights (python#104174) pythonGH-81079: Add case_sensitive argument to `pathlib.Path.glob()` (pythonGH-102710) pythongh-91896: Deprecate collections.abc.ByteString (python#102096) pythongh-99593: Add tests for Unicode C API (part 2) (python#99868) pythongh-102500: Document PEP 688 (python#102571) pythongh-102500: Implement PEP 688 (python#102521) pythongh-96534: socketmodule: support FreeBSD divert(4) socket (python#96536) ...
This has been deprecated for 3.12 |
This is an ABC that never really made much sense and was deprecated in python/cpython#91896
The bytes shorthand was removed in PEP 688: https://peps.python.org/pep-0688/#no-special-meaning-for-bytes I also remove the reference to `collections.abc.ByteString`, since that object is deprecated (#91896) and has different semantics (#102092)
The bytes shorthand was removed in PEP 688: https://peps.python.org/pep-0688/#no-special-meaning-for-bytes I also remove the reference to `collections.abc.ByteString`, since that object is deprecated (python#91896) and has different semantics (python#102092)
gh-102500: Remove mention of bytes shorthand (#104281) The bytes shorthand was removed in PEP 688: https://peps.python.org/pep-0688/#no-special-meaning-for-bytes The reference to collections.abc.ByteString is also removed, since that object is deprecated (#91896) and has different semantics (#102092) Although PEP 688 is new in Python 3.12, type checkers are expected to implement the new semantics for type annotations even if users are using an older version of Python, so this docs PR is backported to Python 3.11. Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
The bytes shorthand was removed in PEP 688: https://peps.python.org/pep-0688/#no-special-meaning-for-bytes I also remove the reference to `collections.abc.ByteString`, since that object is deprecated (python#91896) and has different semantics (python#102092)
This is an ABC that never really made much sense and was deprecated in python/cpython#91896
typing.ByteString's behaviour was poorly specified. It is currently scheduled for removal in Python 3.14. See also python/cpython#91896
…04422) Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
* main: pythongh-91896: Fixup some docs issues following ByteString deprecation (python#104422) pythonGH-104371: check return value of calling `mv.release` (python#104417) pythongh-104415: Fix refleak tests for `typing.ByteString` deprecation (python#104416) pythonGH-86275: Implementation of hypothesis stubs for property-based tests, with zoneinfo tests (python#22863) pythonGH-103082: Filter LINE events in VM, to simplify tool implementation. (pythonGH-104387) pythongh-93649: Split gc- and allocation tests from _testcapimodule.c (pythonGH-104403) pythongh-104389: Add 'unused' keyword to Argument Clinic C converters (python#104390) pythongh-101819: Prepare _io._IOBase for module state (python#104386) pythongh-104413: Fix refleak when super attribute throws AttributeError (python#104414) Fix refleak in `super_descr_get` (python#104408) pythongh-87526: Remove dead initialization from _zoneinfo parse_abbr() (python#24700) pythongh-91896: Improve visibility of `ByteString` deprecation warnings (python#104294) pythongh-104371: Fix calls to `__release_buffer__` while an exception is active (python#104378) pythongh-104377: fix cell in comprehension that is free in outer scope (python#104394) pythongh-104392: Remove _paramspec_tvars from typing (python#104393) pythongh-104396: uuid.py to skip platform check for emscripten and wasi (pythongh-104397) pythongh-99108: Refresh HACL* from upstream (python#104401) pythongh-104301: Allow leading whitespace in disambiguated pdb statements (python#104342)
* main: (29 commits) pythongh-101819: Fix _io clinic input for unused base class method stubs (python#104418) pythongh-101819: Isolate `_io` (python#101948) Bump mypy from 1.2.0 to 1.3.0 in /Tools/clinic (python#104501) pythongh-104494: Update certain Tkinter pack/place tests for Tk 8.7 errors (python#104495) pythongh-104050: Run mypy on `clinic.py` in CI (python#104421) pythongh-104490: Consistently define phony make targets (python#104491) pythongh-67056: document that registering/unregistering an atexit func from within an atexit func is undefined (python#104473) pythongh-104487: PYTHON_FOR_REGEN must be minimum Python 3.10 (python#104488) pythongh-101282: move BOLT config after PGO (pythongh-104493) pythongh-104469 Convert _testcapi/float.c to use AC (pythongh-104470) pythongh-104456: Fix ref leak in _ctypes.COMError (python#104457) pythongh-98539: Make _SSLTransportProtocol.abort() safe to call when closed (python#104474) pythongh-104337: Clarify random.gammavariate doc entry (python#104410) Minor improvements to typing docs (python#104465) pythongh-87092: avoid gcc warning on uninitialized struct field in assemble.c (python#104460) pythonGH-71383: IDLE - Document testing subsets of modules (python#104463) pythongh-104454: Fix refleak in AttributeError_reduce (python#104455) pythongh-75710: IDLE - add docstrings and comments to editor module (python#104446) pythongh-91896: Revert some very noisy DeprecationWarnings for `ByteString` (python#104424) Add a mention of PYTHONBREAKPOINT to breakpoint() docs (python#104430) ...
* main: (204 commits) pythongh-101819: Fix _io clinic input for unused base class method stubs (python#104418) pythongh-101819: Isolate `_io` (python#101948) Bump mypy from 1.2.0 to 1.3.0 in /Tools/clinic (python#104501) pythongh-104494: Update certain Tkinter pack/place tests for Tk 8.7 errors (python#104495) pythongh-104050: Run mypy on `clinic.py` in CI (python#104421) pythongh-104490: Consistently define phony make targets (python#104491) pythongh-67056: document that registering/unregistering an atexit func from within an atexit func is undefined (python#104473) pythongh-104487: PYTHON_FOR_REGEN must be minimum Python 3.10 (python#104488) pythongh-101282: move BOLT config after PGO (pythongh-104493) pythongh-104469 Convert _testcapi/float.c to use AC (pythongh-104470) pythongh-104456: Fix ref leak in _ctypes.COMError (python#104457) pythongh-98539: Make _SSLTransportProtocol.abort() safe to call when closed (python#104474) pythongh-104337: Clarify random.gammavariate doc entry (python#104410) Minor improvements to typing docs (python#104465) pythongh-87092: avoid gcc warning on uninitialized struct field in assemble.c (python#104460) pythonGH-71383: IDLE - Document testing subsets of modules (python#104463) pythongh-104454: Fix refleak in AttributeError_reduce (python#104455) pythongh-75710: IDLE - add docstrings and comments to editor module (python#104446) pythongh-91896: Revert some very noisy DeprecationWarnings for `ByteString` (python#104424) Add a mention of PYTHONBREAKPOINT to breakpoint() docs (python#104430) ...
Tested with mypy 1.5.1 on Python 3.11. This changes `ByteString` into `Union[bytes, bytearray, memoryview]`. See python/cpython#91896. Python 3.11 documentation on `typing.ByteString`: > Deprecated since version 3.9, will be removed in version 3.14: Prefer > typing_extensions.Buffer, or a union like > `bytes | bytearray | memoryview`. Python 3.8 documentation on `typing.ByteString` [2]: > This type represents the types `bytes`, `bytearray`, and `memoryview` > of byte sequences. > > As a shorthand for this type, `bytes` can be used to annotate > arguments of any of the types mentioned above. [1]: https://docs.python.org/3.11/library/typing.html#typing.ByteString [2]: https://docs.python.org/3.8/library/typing.html#typing.ByteString
Tested with mypy 1.5.1 on Python 3.11. This changes `ByteString` into `Union[bytes, bytearray, memoryview]`. See python/cpython#91896. Python 3.11 documentation on `typing.ByteString`: > Deprecated since version 3.9, will be removed in version 3.14: Prefer > typing_extensions.Buffer, or a union like > `bytes | bytearray | memoryview`. Python 3.8 documentation on `typing.ByteString` [2]: > This type represents the types `bytes`, `bytearray`, and `memoryview` > of byte sequences. > > As a shorthand for this type, `bytes` can be used to annotate > arguments of any of the types mentioned above. While at it, also add the return types for special methods which are still optional in mypy strict mode (Ruff's ANN024 rule [3]). [1]: https://docs.python.org/3.11/library/typing.html#typing.ByteString [2]: https://docs.python.org/3.8/library/typing.html#typing.ByteString [3]: https://docs.astral.sh/ruff/rules/missing-return-type-special-method/
Tested with mypy 1.5.1 on Python 3.11. This changes `ByteString` into `Union[bytes, bytearray, memoryview]`. See python/cpython#91896. Python 3.11 documentation on `typing.ByteString`: > Deprecated since version 3.9, will be removed in version 3.14: Prefer > typing_extensions.Buffer, or a union like > `bytes | bytearray | memoryview`. Python 3.8 documentation on `typing.ByteString` [2]: > This type represents the types `bytes`, `bytearray`, and `memoryview` > of byte sequences. > > As a shorthand for this type, `bytes` can be used to annotate > arguments of any of the types mentioned above. While at it, also add the return types for special methods which are still optional in mypy strict mode (Ruff's ANN024 rule [3]). [1]: https://docs.python.org/3.11/library/typing.html#typing.ByteString [2]: https://docs.python.org/3.8/library/typing.html#typing.ByteString [3]: https://docs.astral.sh/ruff/rules/missing-return-type-special-method/
Tested with mypy 1.5.1 on Python 3.11. This changes `ByteString` into `Union[bytes, bytearray, memoryview]`. See python/cpython#91896. Python 3.11 documentation on `typing.ByteString`: > Deprecated since version 3.9, will be removed in version 3.14: Prefer > typing_extensions.Buffer, or a union like > `bytes | bytearray | memoryview`. Python 3.8 documentation on `typing.ByteString` [2]: > This type represents the types `bytes`, `bytearray`, and `memoryview` > of byte sequences. > > As a shorthand for this type, `bytes` can be used to annotate > arguments of any of the types mentioned above. While at it, also add the return types for special methods which are still optional in mypy strict mode (Ruff's ANN024 rule [3]). [1]: https://docs.python.org/3.11/library/typing.html#typing.ByteString [2]: https://docs.python.org/3.8/library/typing.html#typing.ByteString [3]: https://docs.astral.sh/ruff/rules/missing-return-type-special-method/
Tested with mypy 1.5.1 on Python 3.11. This changes `ByteString` into `Union[bytes, bytearray, memoryview]`. See python/cpython#91896. Python 3.11 documentation on `typing.ByteString`: > Deprecated since version 3.9, will be removed in version 3.14: Prefer > typing_extensions.Buffer, or a union like > `bytes | bytearray | memoryview`. Python 3.8 documentation on `typing.ByteString` [2]: > This type represents the types `bytes`, `bytearray`, and `memoryview` > of byte sequences. > > As a shorthand for this type, `bytes` can be used to annotate > arguments of any of the types mentioned above. While at it, also add the return types for special methods which are still optional in mypy strict mode (Ruff's ANN024 rule [3]). [1]: https://docs.python.org/3.11/library/typing.html#typing.ByteString [2]: https://docs.python.org/3.8/library/typing.html#typing.ByteString [3]: https://docs.astral.sh/ruff/rules/missing-return-type-special-method/
This is an ABC that never really made much sense and was deprecated in python/cpython#91896
The current docstring of
collections.abc.ByteString
is:Let's do that last thing. This will be useful for typing code that accepts both bytes and bytearray, especially with my proposal in PEP-688 to make bytes no longer acceptable as a shortcut for bytearray in the type system.
cc @rhettinger for
collections.abc
Linked PRs
ByteString
deprecation warnings #104294ByteString
#104424The text was updated successfully, but these errors were encountered: