From 478def26b9cb011191336b816e6acb619343f895 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Thu, 24 Mar 2016 23:40:29 +0100 Subject: [PATCH 1/7] Reversible references collections.abc.Reversible if it exists. Also if it exists Reversible is a subclass of Iterable, and Sequence is subclass of Reversible. --- src/typing.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/typing.py b/src/typing.py index d11bee7a0..1bd92db0c 100644 --- a/src/typing.py +++ b/src/typing.py @@ -1374,12 +1374,16 @@ def __round__(self, ndigits: int = 0) -> T_co: pass -class Reversible(_Protocol[T_co]): - __slots__ = () +if hasattr(collections_abc, 'Reversible'): + class Reversible(Iterable[T_co], extra=collection_abc.Reversible): + __slots__ = () +else: + class Reversible(_Protocol[T_co]): + __slots__ = () - @abstractmethod - def __reversed__(self) -> 'Iterator[T_co]': - pass + @abstractmethod + def __reversed__(self) -> 'Iterator[T_co]': + pass Sized = collections_abc.Sized # Not generic. @@ -1410,10 +1414,14 @@ class Mapping(Sized, Iterable[KT], Container[KT], Generic[VT_co], class MutableMapping(Mapping[KT, VT], extra=collections_abc.MutableMapping): pass - -class Sequence(Sized, Iterable[T_co], Container[T_co], +if hasattr(collections_abc, 'Reversible'): + class Sequence(Sized, Reversible[T_co], Container[T_co], extra=collections_abc.Sequence): - pass + pass +else: + class Sequence(Sized, Iterable[T_co], Container[T_co], + extra=collections_abc.Sequence): + pass class MutableSequence(Sequence[T], extra=collections_abc.MutableSequence): From 342bdf533a83fbafc5d1bd21fa28011ae13efe69 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Thu, 24 Mar 2016 23:46:02 +0100 Subject: [PATCH 2/7] Fix a typo --- src/typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/typing.py b/src/typing.py index 1bd92db0c..cf501c70b 100644 --- a/src/typing.py +++ b/src/typing.py @@ -1375,7 +1375,7 @@ def __round__(self, ndigits: int = 0) -> T_co: if hasattr(collections_abc, 'Reversible'): - class Reversible(Iterable[T_co], extra=collection_abc.Reversible): + class Reversible(Iterable[T_co], extra=collections_abc.Reversible): __slots__ = () else: class Reversible(_Protocol[T_co]): From 450c81402612df0aa4f7100e9be825dfbe73b545 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Sun, 3 Apr 2016 12:14:27 +0200 Subject: [PATCH 3/7] Use reversible abc also in python2 if it exists --- python2/typing.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/python2/typing.py b/python2/typing.py index d219aebd6..c78f19182 100644 --- a/python2/typing.py +++ b/python2/typing.py @@ -1363,12 +1363,16 @@ def __abs__(self): pass -class Reversible(_Protocol[T_co]): - __slots__ = () +if hasattr(collections_abc, 'Reversible'): + class Reversible(Iterable[T_co], extra=collections_abc.Reversible): + __slots__ = () +else: + class Reversible(_Protocol[T_co]): + __slots__ = () - @abstractmethod - def __reversed__(self): - pass + @abstractmethod + def __reversed__(self) -> 'Iterator[T_co]': + pass Sized = collections_abc.Sized # Not generic. @@ -1399,8 +1403,14 @@ class MutableMapping(Mapping[KT, VT]): __extra__ = collections_abc.MutableMapping -class Sequence(Sized, Iterable[T_co], Container[T_co]): - __extra__ = collections_abc.Sequence +if hasattr(collections_abc, 'Reversible'): + class Sequence(Sized, Reversible[T_co], Container[T_co], + extra=collections_abc.Sequence): + pass +else: + class Sequence(Sized, Iterable[T_co], Container[T_co], + extra=collections_abc.Sequence): + pass class MutableSequence(Sequence[T]): From 6c7f682c33c8ca6d1c6b7fcba34eb2174a9c9b24 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Sun, 3 Apr 2016 12:20:29 +0200 Subject: [PATCH 4/7] Update proptocol tests Reversible is not always a protocol (it is a generic ABC if collections.abc.Reversible exists), but SupportsAbs is always a protocol. --- src/test_typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test_typing.py b/src/test_typing.py index 0f40b93a2..fdee131c2 100644 --- a/src/test_typing.py +++ b/src/test_typing.py @@ -550,7 +550,7 @@ def test_reversible(self): def test_protocol_instance_type_error(self): with self.assertRaises(TypeError): - isinstance([], typing.Reversible) + isinstance(0, typing.SupportsAbs) class GenericTests(TestCase): From 39751b5c9aeacdeaa133334ce3781d9d2a3dbb66 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Sun, 3 Apr 2016 12:22:43 +0200 Subject: [PATCH 5/7] update python2 tests Again, Reversible could not be a protocol. --- python2/test_typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python2/test_typing.py b/python2/test_typing.py index e709ffb08..6e1feb996 100644 --- a/python2/test_typing.py +++ b/python2/test_typing.py @@ -527,7 +527,7 @@ def test_reversible(self): def test_protocol_instance_type_error(self): with self.assertRaises(TypeError): - isinstance([], typing.Reversible) + isinstance(0, typing.SupportsAbs) class GenericTests(TestCase): From 1f88314dcc71c93cf4d6e6d20bf0390710e681e3 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Sun, 3 Apr 2016 12:32:01 +0200 Subject: [PATCH 6/7] fix python2 syntax for extra --- python2/typing.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/python2/typing.py b/python2/typing.py index c78f19182..eafdbc9fb 100644 --- a/python2/typing.py +++ b/python2/typing.py @@ -1364,8 +1364,9 @@ def __abs__(self): if hasattr(collections_abc, 'Reversible'): - class Reversible(Iterable[T_co], extra=collections_abc.Reversible): + class Reversible(Iterable[T_co]): __slots__ = () + __extra__ = collections_abc.Reversible else: class Reversible(_Protocol[T_co]): __slots__ = () @@ -1404,13 +1405,11 @@ class MutableMapping(Mapping[KT, VT]): if hasattr(collections_abc, 'Reversible'): - class Sequence(Sized, Reversible[T_co], Container[T_co], - extra=collections_abc.Sequence): - pass + class Sequence(Sized, Reversible[T_co], Container[T_co]): + __extra__ = collections_abc.Sequence else: - class Sequence(Sized, Iterable[T_co], Container[T_co], - extra=collections_abc.Sequence): - pass + class Sequence(Sized, Iterable[T_co], Container[T_co]): + __extra__ = collections_abc.Sequence class MutableSequence(Sequence[T]): From 4e9e36aa695f4c8c51c553f8c613f53b9a21b5d1 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Sun, 3 Apr 2016 12:40:46 +0200 Subject: [PATCH 7/7] fixed annotation syntax for python2 --- python2/typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python2/typing.py b/python2/typing.py index eafdbc9fb..04521205c 100644 --- a/python2/typing.py +++ b/python2/typing.py @@ -1372,7 +1372,7 @@ class Reversible(_Protocol[T_co]): __slots__ = () @abstractmethod - def __reversed__(self) -> 'Iterator[T_co]': + def __reversed__(self): pass