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. |
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:
The text was updated successfully, but these errors were encountered: