Skip to content

bpo-45684: Add __class_getitem__ method to singledispatchmethod #29355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Nov 1, 2021

@merwok
Copy link
Member

merwok commented Nov 5, 2021

Is there anything to add to the doc of singledispatch?

TBH I don’t fully understand what the bug is. Is it that typeshed suggests you can type a callback parameter with functools.singledispatch[int] rather than Callable but it’s not correctly supported by CPython?

@AlexWaygood
Copy link
Member Author

AlexWaygood commented Nov 5, 2021

Is there anything to add to the doc of singledispatch?

I don't think so -- when __class_getitem__ has been added to other classes such as functools.partial or os.PathLike, nothing was added to the documentation for either of those classes. Though I'm happy to add a note if you think it's warranted 🙂

TBH I don’t fully understand what the bug is. Is it that typeshed suggests you can type a callback parameter with functools.singledispatch[int] rather than Callable but it’s not correctly supported by CPython?

Yes, essentially. 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, as leaving such classes unparameterised gives mypy less information to work with when checking your code. But if you try to parameterise singledispatchmethod, and you're not using from __future__ import annotations, your code will fail at runtime.

Minimal reproducible example showing the error message mypy will give.

@merwok
Copy link
Member

merwok commented Nov 5, 2021

Thanks! Can you add this info to the bug report for clarity?

@AlexWaygood
Copy link
Member Author

Thanks! Can you add this info to the bug report for clarity?

Yes, of course!

@AlexWaygood
Copy link
Member Author

AlexWaygood commented Dec 12, 2021

I'm closing this for now. I still find the inconsistency between typeshed and cpython annoying, but I don't think the use case I presented is strong enough, and there are ultimately more important things to worry about. The PR also now has a merge conflict.

A better solution to this kind of problem might be something like python/typing#956.

@AlexWaygood AlexWaygood deleted the make-singledispatchmethod-generic branch December 12, 2021 10:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants