This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: `functools.singledispatchmethod` does not define `__class_getitem__`
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: AlexWaygood, lukasz.langa, rhettinger
Priority: normal Keywords: patch

Created on 2021-11-01 18:23 by AlexWaygood, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 29355 closed AlexWaygood, 2021-11-01 22:43
Messages (4)
msg405462 - (view) Author: Alex Waygood (AlexWaygood) * (Python triager) Date: 2021-11-01 18:23
`functools.singledispatchmethod` is marked as being a generic class in `typeshed`, but does not define `__class_getitem__`, so cannot be parameterized at runtime.

cpython source code: https://github.com/python/cpython/blob/e2063d6a1ebc3568e90a14ed163fa291b5977ae8/Lib/functools.py#L891

typeshed stub: https://github.com/python/typeshed/blob/f4143c40e85db42dc98549e09329e196668395ee/stdlib/functools.pyi#L94
msg405819 - (view) Author: Alex Waygood (AlexWaygood) * (Python triager) Date: 2021-11-05 18:53
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
msg405850 - (view) Author: Alex Waygood (AlexWaygood) * (Python triager) Date: 2021-11-06 08:57
^ 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
msg408373 - (view) Author: Alex Waygood (AlexWaygood) * (Python triager) Date: 2021-12-12 10:05
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.
History
Date User Action Args
2022-04-11 14:59:51adminsetgithub: 89847
2021-12-12 10:05:25AlexWaygoodsetstatus: open -> closed
resolution: not a bug
messages: + msg408373

stage: patch review -> resolved
2021-11-06 08:57:19AlexWaygoodsetmessages: + msg405850
2021-11-05 18:53:52AlexWaygoodsetmessages: + msg405819
2021-11-01 22:43:45AlexWaygoodsetkeywords: + patch
stage: patch review
pull_requests: + pull_request27615
2021-11-01 18:23:01AlexWaygoodcreate