-
-
Notifications
You must be signed in to change notification settings - Fork 682
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
Support Python 3.10 optional union types #522
Conversation
use duck typing to check for the union and reuse logic from Optional
📝 Docs preview for commit 34d651a at: https://63ae3749ecc68308c0751f2a--typertiangolo.netlify.app |
📝 Docs preview for commit d22b0b2 at: https://63ae429473d46f006486e12f--typertiangolo.netlify.app |
📝 Docs preview for commit 584adbb at: https://63ae447e3639b1027b3da4da--typertiangolo.netlify.app |
📝 Docs preview for commit 447c55f at: https://63b4abee01dd205283004b17--typertiangolo.netlify.app |
waiting for fastapi/typer#522 then i can finally also remove Optional
Any advancements on this ? |
cc @tiangolo? |
I would appriciate this merge as well. :-) |
May I suggest using the typeapi module to add support for new-style type hints in Python versions before 3.10? It can evaluate forward references that make use of
I'm currently using it in my own Typer wrapper terracomp_typer to convert annotations before they are passed to from inspect import signature
from typeapi import get_annotations
def _evaluate_type_hints(func: Callable[..., Any]) -> Callable[..., Any]:
annotations = get_annotations(func)
sig = signature(func)
sig = sig.replace(
parameters=[p.replace(annotation=annotations.get(p.name, p.empty)) for p in sig.parameters.values()],
return_annotation=annotations.get("return", Signature.empty),
)
func.__signature__ = sig # type: ignore[attr-defined]
func.__annotations__ = annotations
return func This will evaluate string annotations and forward references, most notably it can turn It could also replace Typer's currently, if I may say so, seemingly brittle, implementation of introspecting type hints. The Disclaimer: I'm the author of Edit: |
can we get this merged? |
Typer does not support newer union syntax fastapi/typer#522
Typer does not support newer union syntax fastapi/typer#522 Co-authored-by: James Robinson <[email protected]>
@tiangolo Would you have time to review this PR? ❤️ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Superceded by #676, which is a more elegant solution. |
In Python 3.10+, recognise that
SomeType | None
is analogous toOptional[SomeType]
. AFAIK, this shouldn't break anything pre-3.10 because it checks things via duck typing. Resolves #371, resolves #348.Instead of:
Allow: