diff options
| author | Miro Hrončok <miro@hroncok.cz> | 2020-01-05 00:13:52 +0100 |
|---|---|---|
| committer | Miro Hrončok <miro@hroncok.cz> | 2020-01-05 00:14:29 +0100 |
| commit | 89907e760bdc32c3a6e5169015ded485b4d992fb (patch) | |
| tree | 6133f92c3a3b268c1444ad4f2c82e90790c27b9a /src | |
| parent | 482f0833884fe490d50eb03a08eebfb19e9ed46d (diff) | |
| download | python-decorator-git-89907e760bdc32c3a6e5169015ded485b4d992fb.tar.gz | |
On Python 3.9, we cannot longer avoid using collections.abc
Related to https://github.com/micheles/decorator/issues/75#issuecomment-570827328
Diffstat (limited to 'src')
| -rw-r--r-- | src/tests/documentation.py | 40 |
1 files changed, 21 insertions, 19 deletions
diff --git a/src/tests/documentation.py b/src/tests/documentation.py index bd3367c..60a37a2 100644 --- a/src/tests/documentation.py +++ b/src/tests/documentation.py @@ -9,6 +9,7 @@ try: import collections.abc as c except ImportError: c = collections + collections.abc = collections from decorator import (decorator, decorate, FunctionMaker, contextmanager, dispatch_on, __version__) @@ -1008,15 +1009,16 @@ Consider this class: $$WithLength This class defines a ``__len__`` method, and is therefore -considered to be a subclass of the abstract base class ``collections.Sized``: +considered to be a subclass of the abstract base class +``collections.abc.Sized`` (``collections.Sized`` on Python 2): ```python ->>> issubclass(WithLength, collections.Sized) +>>> issubclass(WithLength, collections.abc.Sized) True ``` -However, ``collections.Sized`` is not in the MRO_ of ``WithLength``; it +However, ``collections.abc.Sized`` is not in the MRO_ of ``WithLength``; it is not a true ancestor. Any implementation of generic functions (even with single dispatch) must go through some contorsion to take into account the virtual ancestors. @@ -1037,7 +1039,7 @@ $$get_length_sized ``` -...even if ``collections.Sized`` is not a true ancestor of ``WithLength``. +...even if ``collections.abc.Sized`` is not a true ancestor of ``WithLength``. Of course, this is a contrived example--you could just use the builtin ``len``--but you should get the idea. @@ -1053,14 +1055,14 @@ the following: $$SomeSet Here, the author of ``SomeSet`` made a mistake by inheriting from -``collections.Sized`` (instead of ``collections.Set``). +``collections.abc.Sized`` (instead of ``collections.abc.Set``). This is not a problem. You can register *a posteriori* -``collections.Set`` as a virtual ancestor of ``SomeSet``: +``collections.abc.Set`` as a virtual ancestor of ``SomeSet``: ```python ->>> _ = collections.Set.register(SomeSet) ->>> issubclass(SomeSet, collections.Set) +>>> _ = collections.abc.Set.register(SomeSet) +>>> issubclass(SomeSet, collections.abc.Set) True ``` @@ -1080,10 +1082,10 @@ the class registry, so it uses the more specific implementation for ``Set``: ``` Sometimes it is not clear how to dispatch. For instance, consider a -class ``C`` registered both as ``collections.Iterable`` and -``collections.Sized``, and defines a generic function ``g`` with -implementations for both ``collections.Iterable`` *and* -``collections.Sized``: +class ``C`` registered both as ``collections.abc.Iterable`` and +``collections.abc.Sized``, and defines a generic function ``g`` with +implementations for both ``collections.abc.Iterable`` *and* +``collections.abc.Sized``: $$singledispatch_example1 @@ -1784,7 +1786,7 @@ class WithLength(object): return 0 -class SomeSet(collections.Sized): +class SomeSet(collections.abc.Sized): # methods that make SomeSet set-like # not shown ... def __len__(self): @@ -1796,12 +1798,12 @@ def get_length(obj): raise NotImplementedError(type(obj)) -@get_length.register(collections.Sized) +@get_length.register(collections.abc.Sized) def get_length_sized(obj): return len(obj) -@get_length.register(collections.Set) +@get_length.register(collections.abc.Set) def get_length_set(obj): return 1 @@ -1810,8 +1812,8 @@ class C(object): "Registered as Sized and Iterable" -collections.Sized.register(C) -collections.Iterable.register(C) +collections.abc.Sized.register(C) +collections.abc.Iterable.register(C) def singledispatch_example1(): @@ -1821,11 +1823,11 @@ def singledispatch_example1(): def g(obj): raise NotImplementedError(type(g)) - @g.register(collections.Sized) + @g.register(collections.abc.Sized) def g_sized(object): return "sized" - @g.register(collections.Iterable) + @g.register(collections.abc.Iterable) def g_iterable(object): return "iterable" |
