Skip to content

Commit eb24e8f

Browse files
committed
Remove Collections backport
In retrospect, attempting to backport typing.Collection was a mistake. It's difficult in practice to use it in a consistently meaningful way because the typing module deliberately does not have types like Sequence or List subclass anything resembling the Collections type for older versions of Python. This means that the following code will not typecheck on Python 2 and on older versions of Python 3: from mypy_extensions import Collection def foo(x): # type: Collection[int] -> int return sum(x) foo([1, 2, 3]) ...because `List` is not defined to be a subclass of `Collection` in Python 2 and Python 3.x - 3.5 at runtime and within the typing stubs in typeshed. I think this is potentially repairable with some careful surgery on the typing stubs, but doing that seems highly questionable to me, so I'm thinking attempting to backport `Collection` is out-of-scope for this pull request and should be handled later (or potentially never).
1 parent 8497aa5 commit eb24e8f

File tree

5 files changed

+1
-94
lines changed

5 files changed

+1
-94
lines changed

typing_extensions/README.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ All Python versions:
3535
--------------------
3636

3737
- ``ClassVar``
38-
- ``Collection``
3938
- ``ContextManager``
4039
- ``Counter``
4140
- ``DefaultDict``

typing_extensions/src_py2/test_typing_extensions.py

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from unittest import TestCase, main, skipUnless
77

88
from typing_extensions import NoReturn, ClassVar
9-
from typing_extensions import Collection, ContextManager, Counter, Deque, DefaultDict
9+
from typing_extensions import ContextManager, Counter, Deque, DefaultDict
1010
from typing_extensions import NewType, overload
1111
import typing
1212
import typing_extensions
@@ -117,25 +117,6 @@ def test_no_isinstance(self):
117117

118118
class CollectionsAbcTests(BaseTestCase):
119119

120-
def test_collection(self):
121-
self.assertIsInstance(tuple(), Collection)
122-
self.assertIsInstance(frozenset(), Collection)
123-
self.assertIsSubclass(dict, Collection)
124-
self.assertNotIsInstance(42, Collection)
125-
126-
def test_collection_instantiation(self):
127-
class MyAbstractCollection(Collection[int]):
128-
pass
129-
class MyCollection(Collection[int]):
130-
def __contains__(self, item): pass
131-
def __iter__(self): pass
132-
def __len__(self): pass
133-
134-
self.assertIsSubclass(type(MyCollection()), Collection)
135-
self.assertIsSubclass(MyCollection, Collection)
136-
with self.assertRaises(TypeError):
137-
MyAbstractCollection()
138-
139120
def test_contextmanager(self):
140121
@contextlib.contextmanager
141122
def manager():
@@ -242,7 +223,6 @@ def test_typing_extensions_includes_standard(self):
242223
self.assertIn('overload', a)
243224
self.assertIn('Text', a)
244225
self.assertIn('TYPE_CHECKING', a)
245-
self.assertIn('Collection', a)
246226

247227
def test_typing_extensions_defers_when_possible(self):
248228
exclude = {'overload', 'Text', 'TYPE_CHECKING'}

typing_extensions/src_py2/typing_extensions.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
'Type',
1515

1616
# Concrete collection types.
17-
'Collection',
1817
'ContextManager',
1918
'Counter',
2019
'Deque',
@@ -90,24 +89,3 @@ def __subclasshook__(cls, C):
9089
return True
9190
return NotImplemented
9291

93-
94-
# Backport collections.Collection
95-
class _CollectionAbc(collections.Sized,
96-
collections.Iterable,
97-
collections.Container):
98-
pass
99-
100-
101-
_CollectionAbc.register(list)
102-
_CollectionAbc.register(tuple)
103-
_CollectionAbc.register(set)
104-
_CollectionAbc.register(frozenset)
105-
_CollectionAbc.register(basestring)
106-
_CollectionAbc.register(dict)
107-
108-
109-
class Collection(typing.Sized,
110-
typing.Iterable[T_co],
111-
typing.Container[T_co]):
112-
__slots__ = ()
113-
__extra__ = _CollectionAbc

typing_extensions/src_py3/test_typing_extensions.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -371,26 +371,6 @@ def test_async_iterator(self):
371371
self.assertIsInstance(it, typing_extensions.AsyncIterator)
372372
self.assertNotIsInstance(42, typing_extensions.AsyncIterator)
373373

374-
def test_collection(self):
375-
self.assertIsInstance(tuple(), typing_extensions.Collection)
376-
self.assertIsInstance(frozenset(), typing_extensions.Collection)
377-
self.assertIsSubclass(dict, typing_extensions.Collection)
378-
self.assertNotIsInstance(42, typing_extensions.Collection)
379-
380-
@skipUnless(TYPING_3_5_1, "Behavior added in typing 3.5.1+")
381-
def test_collection_instantiation(self):
382-
class MyCollection(typing_extensions.Collection[int]):
383-
def __contains__(self, item): ...
384-
def __iter__(self): ...
385-
def __len__(self): ...
386-
387-
self.assertIsSubclass(
388-
type(MyCollection()),
389-
typing_extensions.Collection)
390-
self.assertIsSubclass(
391-
MyCollection,
392-
typing_extensions.Collection)
393-
394374
def test_deque(self):
395375
self.assertIsSubclass(collections.deque, typing_extensions.Deque)
396376
class MyDeque(typing_extensions.Deque[int]): ...
@@ -630,8 +610,6 @@ def test_typing_extensions_includes_standard(self):
630610

631611
if PY36:
632612
self.assertIn('AsyncGenerator', a)
633-
if hasattr(collections_abc, 'Collection'):
634-
self.assertIn('Collection', a)
635613

636614
def test_typing_extensions_defers_when_possible(self):
637615
exclude = {'overload', 'Text', 'TYPE_CHECKING'}

typing_extensions/src_py3/typing_extensions.py

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ def _check_methods_in_mro(C, *methods):
4646
# 'ChainMap',
4747

4848
# Concrete collection types.
49-
'Collection',
5049
'ContextManager',
5150
'Counter',
5251
'Deque',
@@ -391,33 +390,6 @@ class AsyncIterator(AsyncIterable[T_co],
391390
__slots__ = ()
392391

393392

394-
if hasattr(typing, 'Collection'):
395-
Collection = typing.Collection
396-
else:
397-
__all__.append('Collection')
398-
399-
# Backport collections.abc.Collections
400-
class _CollectionAbc(collections.Sized,
401-
collections.Iterable,
402-
collections.Container):
403-
__slots__ = ()
404-
405-
@classmethod
406-
def __subclasshook__(cls, C):
407-
if cls is _CollectionAbc:
408-
return _check_methods_in_mro(
409-
C, "__len__", "__iter__", "__contains__")
410-
return NotImplemented
411-
412-
extra = getattr(collections_abc, 'Collection', _CollectionAbc)
413-
414-
class Collection(typing.Sized,
415-
typing.Iterable[T_co],
416-
typing.Container[T_co],
417-
extra=extra):
418-
__slots__ = ()
419-
420-
421393
if hasattr(typing, 'Deque'):
422394
Deque = typing.Deque
423395
else:

0 commit comments

Comments
 (0)