You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
functools.singledispatchmethod does not define __class_getitem__ #89847
functools.singledispatchmethod is marked as being a generic class in typeshed, but does not define __class_getitem__, so cannot be parameterized at runtime.
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.
Activity
AlexWaygood commentedon Nov 1, 2021
functools.singledispatchmethodis marked as being a generic class intypeshed, but does not define__class_getitem__, so cannot be parameterized at runtime.cpython source code:
cpython/Lib/functools.py
Line 891 in e2063d6
typeshed stub: https://github.com/python/typeshed/blob/f4143c40e85db42dc98549e09329e196668395ee/stdlib/functools.pyi#L94
AlexWaygood commentedon Nov 5, 2021
To clarify why this is an issue:
It's probably quite improbable that anybody would ever need to use
singledispatchmethodin a type annotation. But, if they do, they'll find themselves in an impossible situation if they're using mypy with the--strictsetting. 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 parameterisesingledispatchmethod, and you're not usingfrom __future__ import annotations, your code will fail at runtime.It would be better if the cpython source were changed to add
__class_getitem__tosingledispatchmethod, 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
AlexWaygood commentedon Nov 6, 2021
^ I posted the wrong link for the example; it should have been: https://mypy-play.net/?mypy=latest&python=3.10&flags=show-error-codes%2Cstrict&gist=6a26d31899f9803a823662261eae21df
AlexWaygood commentedon Dec 12, 2021
Closing this, as I don't think the use case I presented is strong enough, and there are ultimately more important things to worry about.