-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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 Union types #90172
Comments
It's not currently possible to use Example:
This dies with:
I've implemented a workaround which digs the types out of the union and registers them one-by-one:
Looking at the source for The only other mentions I've found of this online are: |
Support added as an enhancement in Python 3.11. Thanks, Yurii! ✨ 🍰 ✨ |
This is awesome! Should a note be added to the functools documentation mentioning the new feature? (Genuine question — I'm not sure whether it's necessary myself.) |
Will it support Optional as well? According to PEP-604, these two shall be equivalent (using Python 3.10 below)?
Also, IMHO, the documentation of singledispatch should be improved. It needs to state that only Python types are supported with few exceptions (union type now), but other annotation types are not supported, i.e. generics like |
^Because of that, it will work with typing.Optional as well as typing.Union and types.UnionType, yes. I am planning on submitting a docs PR at some point in the next few days (probably linked to a new BPO issue). |
Reopening as per #32282 (comment) |
It does indeed work (continuing from the OP's example): >>> @test.register
... def _(arg: str | None):
... print(f"{arg} may be a string")
...
>>> test(None)
None may be a string
>>> test("x")
x may be a string But it's not obvious that it should, because So anyone up for adding a test case that uses an Optional type? |
…one type (#92174) Signed-off-by: prwatson <prwatson@redhat.com> Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Thanks @Thaddeus1499 for contributing more tests. |
functools.singledispatch
changes #32282Note: 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: