Skip to content

Commit c527eb1

Browse files
authored
gh-91896: Revert some very noisy DeprecationWarnings for ByteString (#104424)
1 parent 1be80ed commit c527eb1

File tree

7 files changed

+19
-79
lines changed

7 files changed

+19
-79
lines changed

Doc/library/collections.abc.rst

+1-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414

1515
.. testsetup:: *
1616

17-
import warnings
18-
# Ignore warning when ByteString is imported
19-
with warnings.catch_warnings(action='ignore', category=DeprecationWarning):
20-
from collections.abc import *
17+
from collections.abc import *
2118
import itertools
2219
__name__ = '<doctest>'
2320

Doc/whatsnew/3.12.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -831,8 +831,8 @@ Pending Removal in Python 3.14
831831
For use in typing, prefer a union, like ``bytes | bytearray``, or :class:`collections.abc.Buffer`.
832832
(Contributed by Shantanu Jain in :gh:`91896`.)
833833

834-
* :class:`typing.ByteString`, deprecated since Python 3.9, now causes an
835-
:exc:`DeprecationWarning` to be emitted when it is used or accessed.
834+
* :class:`typing.ByteString`, deprecated since Python 3.9, now causes a
835+
:exc:`DeprecationWarning` to be emitted when it is used.
836836

837837
* Creating immutable types (:data:`Py_TPFLAGS_IMMUTABLETYPE`) with mutable
838838
bases using the C API.

Lib/collections/abc.py

-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
11
from _collections_abc import *
22
from _collections_abc import __all__
33
from _collections_abc import _CallableGenericAlias
4-
5-
_deprecated_ByteString = globals().pop("ByteString")
6-
7-
def __getattr__(attr):
8-
if attr == "ByteString":
9-
import warnings
10-
warnings._deprecated("collections.abc.ByteString", remove=(3, 14))
11-
return _deprecated_ByteString
12-
raise AttributeError(f"module 'collections.abc' has no attribute {attr!r}")

Lib/test/libregrtest/refleak.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,11 @@ def dash_R(ns, test_name, test_func):
4848
else:
4949
zdc = zipimport._zip_directory_cache.copy()
5050
abcs = {}
51-
# catch and ignore collections.abc.ByteString deprecation
52-
with warnings.catch_warnings(action='ignore', category=DeprecationWarning):
53-
for abc in [getattr(collections.abc, a) for a in collections.abc.__all__]:
54-
if not isabstract(abc):
55-
continue
56-
for obj in abc.__subclasses__() + [abc]:
57-
abcs[obj] = _get_dump(obj)[0]
51+
for abc in [getattr(collections.abc, a) for a in collections.abc.__all__]:
52+
if not isabstract(abc):
53+
continue
54+
for obj in abc.__subclasses__() + [abc]:
55+
abcs[obj] = _get_dump(obj)[0]
5856

5957
# bpo-31217: Integer pool to get a single integer object for the same
6058
# value. The pool is used to prevent false alarm when checking for memory
@@ -176,8 +174,7 @@ def dash_R_cleanup(fs, ps, pic, zdc, abcs):
176174

177175
# Clear ABC registries, restoring previously saved ABC registries.
178176
# ignore deprecation warning for collections.abc.ByteString
179-
with warnings.catch_warnings(action='ignore', category=DeprecationWarning):
180-
abs_classes = [getattr(collections.abc, a) for a in collections.abc.__all__]
177+
abs_classes = [getattr(collections.abc, a) for a in collections.abc.__all__]
181178
abs_classes = filter(isabstract, abs_classes)
182179
for abc in abs_classes:
183180
for obj in abc.__subclasses__() + [abc]:

Lib/test/test_collections.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import string
1212
import sys
1313
from test import support
14-
from test.support.import_helper import import_fresh_module
1514
import types
1615
import unittest
1716

@@ -26,7 +25,7 @@
2625
from collections.abc import Set, MutableSet
2726
from collections.abc import Mapping, MutableMapping, KeysView, ItemsView, ValuesView
2827
from collections.abc import Sequence, MutableSequence
29-
from collections.abc import Buffer
28+
from collections.abc import ByteString, Buffer
3029

3130

3231
class TestUserObjects(unittest.TestCase):
@@ -1940,8 +1939,6 @@ def assert_index_same(seq1, seq2, index_args):
19401939
nativeseq, seqseq, (letter, start, stop))
19411940

19421941
def test_ByteString(self):
1943-
with self.assertWarns(DeprecationWarning):
1944-
from collections.abc import ByteString
19451942
for sample in [bytes, bytearray]:
19461943
with self.assertWarns(DeprecationWarning):
19471944
self.assertIsInstance(sample(), ByteString)
@@ -1963,11 +1960,6 @@ class X(ByteString): pass
19631960
# No metaclass conflict
19641961
class Z(ByteString, Awaitable): pass
19651962

1966-
def test_ByteString_attribute_access(self):
1967-
collections_abc = import_fresh_module("collections.abc")
1968-
with self.assertWarns(DeprecationWarning):
1969-
collections_abc.ByteString
1970-
19711963
def test_Buffer(self):
19721964
for sample in [bytes, bytearray, memoryview]:
19731965
self.assertIsInstance(sample(b"x"), Buffer)

Lib/test/test_typing.py

+6-22
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import re
99
import sys
1010
import warnings
11-
from test.support.import_helper import import_fresh_module
1211
from unittest import TestCase, main, skipUnless, skip
1312
from unittest.mock import patch
1413
from copy import copy, deepcopy
@@ -3909,14 +3908,7 @@ class MyChain(typing.ChainMap[str, T]): ...
39093908
self.assertEqual(MyChain[int]().__orig_class__, MyChain[int])
39103909

39113910
def test_all_repr_eq_any(self):
3912-
typing = import_fresh_module("typing")
3913-
with warnings.catch_warnings(record=True) as wlog:
3914-
warnings.filterwarnings('always', '', DeprecationWarning)
3915-
objs = [getattr(typing, el) for el in typing.__all__]
3916-
self.assertEqual(
3917-
[str(w.message) for w in wlog],
3918-
["'typing.ByteString' is deprecated and slated for removal in Python 3.14"]
3919-
)
3911+
objs = (getattr(typing, el) for el in typing.__all__)
39203912
for obj in objs:
39213913
self.assertNotEqual(repr(obj), '')
39223914
self.assertEqual(obj, obj)
@@ -6005,15 +5997,13 @@ def test_mutablesequence(self):
60055997

60065998
def test_bytestring(self):
60075999
with self.assertWarns(DeprecationWarning):
6008-
from typing import ByteString
6000+
self.assertIsInstance(b'', typing.ByteString)
60096001
with self.assertWarns(DeprecationWarning):
6010-
self.assertIsInstance(b'', ByteString)
6002+
self.assertIsInstance(bytearray(b''), typing.ByteString)
60116003
with self.assertWarns(DeprecationWarning):
6012-
self.assertIsInstance(bytearray(b''), ByteString)
6004+
class Foo(typing.ByteString): ...
60136005
with self.assertWarns(DeprecationWarning):
6014-
class Foo(ByteString): ...
6015-
with self.assertWarns(DeprecationWarning):
6016-
class Bar(ByteString, typing.Awaitable): ...
6006+
class Bar(typing.ByteString, typing.Awaitable): ...
60176007

60186008
def test_list(self):
60196009
self.assertIsSubclass(list, typing.List)
@@ -8309,10 +8299,6 @@ def test_no_isinstance(self):
83098299
class SpecialAttrsTests(BaseTestCase):
83108300

83118301
def test_special_attrs(self):
8312-
with warnings.catch_warnings(
8313-
action='ignore', category=DeprecationWarning
8314-
):
8315-
typing_ByteString = typing.ByteString
83168302
cls_to_check = {
83178303
# ABC classes
83188304
typing.AbstractSet: 'AbstractSet',
@@ -8321,7 +8307,7 @@ def test_special_attrs(self):
83218307
typing.AsyncIterable: 'AsyncIterable',
83228308
typing.AsyncIterator: 'AsyncIterator',
83238309
typing.Awaitable: 'Awaitable',
8324-
typing_ByteString: 'ByteString',
8310+
typing.ByteString: 'ByteString',
83258311
typing.Callable: 'Callable',
83268312
typing.ChainMap: 'ChainMap',
83278313
typing.Collection: 'Collection',
@@ -8646,8 +8632,6 @@ def test_all_exported_names(self):
86468632
getattr(v, '__module__', None) == typing.__name__
86478633
)
86488634
}
8649-
# Deprecated; added dynamically via module __getattr__
8650-
computed_all.add("ByteString")
86518635
self.assertSetEqual(computed_all, actual_all)
86528636

86538637

Lib/typing.py

+3-24
Original file line numberDiff line numberDiff line change
@@ -2772,6 +2772,9 @@ class Other(Leaf): # Error reported by type checker
27722772
MutableMapping = _alias(collections.abc.MutableMapping, 2)
27732773
Sequence = _alias(collections.abc.Sequence, 1)
27742774
MutableSequence = _alias(collections.abc.MutableSequence, 1)
2775+
ByteString = _DeprecatedGenericAlias(
2776+
collections.abc.ByteString, 0, removal_version=(3, 14) # Not generic.
2777+
)
27752778
# Tuple accepts variable number of parameters.
27762779
Tuple = _TupleType(tuple, -1, inst=False, name='Tuple')
27772780
Tuple.__doc__ = \
@@ -3571,27 +3574,3 @@ def method(self) -> None:
35713574
# read-only property, TypeError if it's a builtin class.
35723575
pass
35733576
return method
3574-
3575-
3576-
def __getattr__(attr):
3577-
if attr == "ByteString":
3578-
import warnings
3579-
warnings._deprecated("typing.ByteString", remove=(3, 14))
3580-
with warnings.catch_warnings(
3581-
action="ignore", category=DeprecationWarning
3582-
):
3583-
# Not generic
3584-
ByteString = globals()["ByteString"] = _DeprecatedGenericAlias(
3585-
collections.abc.ByteString, 0, removal_version=(3, 14)
3586-
)
3587-
return ByteString
3588-
raise AttributeError(f"module 'typing' has no attribute {attr!r}")
3589-
3590-
3591-
def _remove_cached_ByteString_from_globals():
3592-
try:
3593-
del globals()["ByteString"]
3594-
except KeyError:
3595-
pass
3596-
3597-
_cleanups.append(_remove_cached_ByteString_from_globals)

0 commit comments

Comments
 (0)