-
-
Notifications
You must be signed in to change notification settings - Fork 31.3k
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
functools' singledispatch does not support GenericAlias #90190
Comments
functools' singledispatch does not support GenericAlias from functools import singledispatch
@singledispatch
def func(x):
print("any")
@func.register
def _(x: list[str]):
print("list[str]")
func(["a", "b"]) |
My opinion is that supporting The first option would be efficient, simple, and similar to the way singledispatch treats most other argument-types. However, it would be unintuitive. The second option would be more intuitive, but could be extremely inefficient if a very long list was passed in. It would also make the code more complicated. |
It would be well worth it to improve the error message, however:
|
The above traceback is because the |
Yes, it is related to bpo-45665. It is a complicated case due to coincidence of several circumstances.
In 2-argument registry() typing.List[int] is passed due to (4) and ignored in dispatch() due to (2). list[int] is passed due to (4), but caused error due to (3). In other uses of registry() (1-argument decorator factory and decorator with annotations) typing.List[int] is not passed due to 1. list[int] is passed due to (1) and caused error due to (3). The proposed PR makes list[int] be treated the same way as typing.List[int]. It also makes 2-argument registry() rejecting invalid first argument, so all three forms of registry() accept and reject now the same types. |
The PR looks good to me. I think it's also important that we document that these types aren't supported, as it's not mentioned anywhere at the moment. Related: bpo-34498. |
I came across this today, and I was quite surprised I could not do this but then it made sense since we cannot do I see there is still no documentation stating this is not supported. https://docs.python.org/3/library/functools.html#functools.singledispatch Proposal, I'll create a PR adding an example with additional example of how to properly type hint it correctly? I'm not sure if there even is a way to properly type hint that a function takes a i.e.
raises error |
If you want to use type hints with generics in a singledispatch function, I'd recommend doing something like this: @singledispatch
def func(x):
print("any")
@func.register(list)
def _(x: list[str]):
print("list[str]") (But note that that will dispatch on any call that passes a list, not just calls that pass lists where all items in the list are A PR to improve the docs would be welcome. If you ping me on the PR, I can review it. |
…ction type hints (pythonGH-116544) (cherry picked from commit 2357d5b) Co-authored-by: Matt Delengowski <matt.delengowski@gmail.com>
…ction type hints (pythonGH-116544) (cherry picked from commit 2357d5b) Co-authored-by: Matt Delengowski <matt.delengowski@gmail.com>
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs
singledispatch
with precise collection type hints #116544singledispatch
with precise collection type hints (GH-116544) #124710singledispatch
with precise collection type hints (GH-116544) #124711The text was updated successfully, but these errors were encountered: