[issue45684] `functools.singledispatchmethod` does not define `__class_getitem__`

Alex Waygood report at bugs.python.org
Fri Nov 5 14:53:52 EDT 2021


Alex Waygood <Alex.Waygood at Gmail.com> added the comment:

To clarify why this is an issue:

It's probably quite improbable that anybody would ever need to use `singledispatchmethod` in a type annotation. But, if they do, they'll find themselves in an impossible situation if they're using mypy with the `--strict` setting. If you're using `--strict` (as I always do), mypy will complain if you leave unparameterised any class that is marked as a generic in typeshed. But if you try to parameterise `singledispatchmethod`, and you're not using `from __future__ import annotations`, your code will fail at runtime.

It would be better if the cpython source were changed to add `__class_getitem__` to `singledispatchmethod`, rather than making the class no longer generic in typeshed. This is because being able to parameterise classes gives mypy much more information to work with, giving it the ability to provide far more fine-grained analysis of your code.

Here is a minimal reproducible example of the error mypy will give: https://mypy-play.net/?mypy=latest&python=3.10&flags=show-error-codes%2Cstrict

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45684>
_______________________________________


More information about the Python-bugs-list mailing list