Skip to content

Commit 5dc6d55

Browse files
committed
Update _collections_abc.py to 3.12.2
1 parent a8ab7dd commit 5dc6d55

1 file changed

Lines changed: 63 additions & 11 deletions

File tree

‎Lib/_collections_abc.py‎

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,32 @@
66
Unit tests are in test_collections.
77
"""
88

9+
############ Maintenance notes #########################################
10+
#
11+
# ABCs are different from other standard library modules in that they
12+
# specify compliance tests. In general, once an ABC has been published,
13+
# new methods (either abstract or concrete) cannot be added.
14+
#
15+
# Though classes that inherit from an ABC would automatically receive a
16+
# new mixin method, registered classes would become non-compliant and
17+
# violate the contract promised by ``isinstance(someobj, SomeABC)``.
18+
#
19+
# Though irritating, the correct procedure for adding new abstract or
20+
# mixin methods is to create a new ABC as a subclass of the previous
21+
# ABC. For example, union(), intersection(), and difference() cannot
22+
# be added to Set but could go into a new ABC that extends Set.
23+
#
24+
# Because they are so hard to change, new ABCs should have their APIs
25+
# carefully thought through prior to publication.
26+
#
27+
# Since ABCMeta only checks for the presence of methods, it is possible
28+
# to alter the signature of a method by adding optional arguments
29+
# or changing parameters names. This is still a bit dubious but at
30+
# least it won't cause isinstance() to return an incorrect result.
31+
#
32+
#
33+
#######################################################################
34+
935
from abc import ABCMeta, abstractmethod
1036
import sys
1137

@@ -23,7 +49,7 @@ def _f(): pass
2349
"Mapping", "MutableMapping",
2450
"MappingView", "KeysView", "ItemsView", "ValuesView",
2551
"Sequence", "MutableSequence",
26-
"ByteString",
52+
"ByteString", "Buffer",
2753
]
2854

2955
# This module has been renamed from collections.abc to _collections_abc to
@@ -413,6 +439,21 @@ def __subclasshook__(cls, C):
413439
return NotImplemented
414440

415441

442+
class Buffer(metaclass=ABCMeta):
443+
444+
__slots__ = ()
445+
446+
@abstractmethod
447+
def __buffer__(self, flags: int, /) -> memoryview:
448+
raise NotImplementedError
449+
450+
@classmethod
451+
def __subclasshook__(cls, C):
452+
if cls is Buffer:
453+
return _check_methods(C, "__buffer__")
454+
return NotImplemented
455+
456+
416457
class _CallableGenericAlias(GenericAlias):
417458
""" Represent `Callable[argtypes, resulttype]`.
418459
@@ -455,15 +496,8 @@ def __getitem__(self, item):
455496
# rather than the default types.GenericAlias object. Most of the
456497
# code is copied from typing's _GenericAlias and the builtin
457498
# types.GenericAlias.
458-
459499
if not isinstance(item, tuple):
460500
item = (item,)
461-
# A special case in PEP 612 where if X = Callable[P, int],
462-
# then X[int, str] == X[[int, str]].
463-
if (len(self.__parameters__) == 1
464-
and _is_param_expr(self.__parameters__[0])
465-
and item and not _is_param_expr(item[0])):
466-
item = (item,)
467501

468502
new_args = super().__getitem__(item).__args__
469503

@@ -491,9 +525,8 @@ def _type_repr(obj):
491525
492526
Copied from :mod:`typing` since collections.abc
493527
shouldn't depend on that module.
528+
(Keep this roughly in sync with the typing version.)
494529
"""
495-
if isinstance(obj, GenericAlias):
496-
return repr(obj)
497530
if isinstance(obj, type):
498531
if obj.__module__ == 'builtins':
499532
return obj.__qualname__
@@ -1038,8 +1071,27 @@ def count(self, value):
10381071
Sequence.register(range)
10391072
Sequence.register(memoryview)
10401073

1074+
class _DeprecateByteStringMeta(ABCMeta):
1075+
def __new__(cls, name, bases, namespace, **kwargs):
1076+
if name != "ByteString":
1077+
import warnings
1078+
1079+
warnings._deprecated(
1080+
"collections.abc.ByteString",
1081+
remove=(3, 14),
1082+
)
1083+
return super().__new__(cls, name, bases, namespace, **kwargs)
1084+
1085+
def __instancecheck__(cls, instance):
1086+
import warnings
1087+
1088+
warnings._deprecated(
1089+
"collections.abc.ByteString",
1090+
remove=(3, 14),
1091+
)
1092+
return super().__instancecheck__(instance)
10411093

1042-
class ByteString(Sequence):
1094+
class ByteString(Sequence, metaclass=_DeprecateByteStringMeta):
10431095
"""This unifies bytes and bytearray.
10441096
10451097
XXX Should add all their methods.

0 commit comments

Comments
 (0)