Support Python 3.10 optional union types#522
Support Python 3.10 optional union types#522potatoeggy wants to merge 7 commits intofastapi:masterfrom potatoeggy:feat-uniontype-none
Conversation
use duck typing to check for the union and reuse logic from Optional
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 funcThis 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? ❤️ |
|
Superceded by #676, which is a more elegant solution. |
In Python 3.10+, recognise that
SomeType | Noneis 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: