[issue45359] TopologicalSorter is not Generic at runtime (but is in typeshed)
Jacob Hayes added the comment: Thanks for the tips! I've been using this patch in my own code in a early imported `__init__.py`: ``` from graphlib import TopologicalSorter from types import GenericAlias if not hasattr(TopologicalSorter, "__class_getitem__"): # pragma: no cover TopologicalSorter.__class_getitem__ = classmethod(GenericAlias) # type: ignore ``` Certainly a bit hacky, but aside from no-op on >=3.11 and satisfying mypy, it supports runtime type inspection via `get_type_hints` before 3.11 (which otherwise still errors for stringized versions). The stringized versions are probably a bit simpler/safer for most folks though :) -- ___ Python tracker <https://bugs.python.org/issue45359> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue45167] deepcopy of GenericAlias with __deepcopy__ method is broken
New submission from Jacob Hayes : When deepcopying a parametrized types.GenericAlias (eg: a dict subclass) that has a __deepcopy__ method, the copy module doesn't detect the GenericAlias as a type and instead tries to call cls.__deepcopy__, passing `memo` inplace of self. This doesn't seem to happen with `typing.Generic` however. Example: ``` from copy import deepcopy class X(dict): def __deepcopy__(self, memo): return self print(deepcopy(X())) print(deepcopy(X)) print(type(X[str, int])) print(deepcopy(X[str, int]())) print(deepcopy(X[str, int])) ``` shows ``` {} {} Traceback (most recent call last): File "/tmp/demo.py", line 14, in print(deepcopy(X[str, int])) File "/Users/jacobhayes/.pyenv/versions/3.9.6/lib/python3.9/copy.py", line 153, in deepcopy y = copier(memo) TypeError: __deepcopy__() missing 1 required positional argument: 'memo' ``` I don't know if it's better to update `copy.deepcopy` here or perhaps narrow the `__getattr__` for `types.GenericAlias` (as `typing. _BaseGenericAlias` seems to). -- components: Library (Lib) messages: 401601 nosy: JacobHayes priority: normal severity: normal status: open title: deepcopy of GenericAlias with __deepcopy__ method is broken type: behavior versions: Python 3.9 ___ Python tracker <https://bugs.python.org/issue45167> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue45359] TopologicalSorter is not Generic at runtime (but is in typeshed)
New submission from Jacob Hayes : Reproduction: ``` from graphlib import TopologicalSorter TopologicalSorter[str]({"a": {}, "b": {"a"}}) ``` ``` $ mypy /tmp/toposort.py Success: no issues found in 1 source file $ python3 /tmp/toposort.py Traceback (most recent call last): File "/tmp/toposort.py", line 3, in TopologicalSorter[str]({"a": {}, "b": {"a"}}) TypeError: 'type' object is not subscriptable ``` I opened the issue here (rather than typeshed) because we'd presumably like to support this at runtime too. Typeshed link: https://github.com/python/mypy/blob/0a830481980bfc554ded61a3eaaaecde384a21e4/mypy/typeshed/stdlib/graphlib.pyi#L6 -- components: Library (Lib) messages: 403115 nosy: JacobHayes priority: normal severity: normal status: open title: TopologicalSorter is not Generic at runtime (but is in typeshed) type: behavior versions: Python 3.9 ___ Python tracker <https://bugs.python.org/issue45359> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue45359] TopologicalSorter is not Generic at runtime (but is in typeshed)
Change by Jacob Hayes : -- keywords: +patch pull_requests: +27064 stage: -> patch review pull_request: https://github.com/python/cpython/pull/28714 ___ Python tracker <https://bugs.python.org/issue45359> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue45359] TopologicalSorter is not Generic at runtime (but is in typeshed)
Jacob Hayes added the comment: Thanks for merging! Should typeshed be updated for <3.11 in the meantime or do you suggest `if TYPE_CHECKING` blocks on user side? Perhaps it's a non-issue if no one else has noticed this. :) -- ___ Python tracker <https://bugs.python.org/issue45359> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com