Skip to content
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

Closed
wants to merge 7 commits into from
Closed

Support Python 3.10 optional union types #522

wants to merge 7 commits into from

Conversation

potatoeggy
Copy link

@potatoeggy potatoeggy commented Dec 30, 2022

In Python 3.10+, recognise that SomeType | None is analogous to Optional[SomeType]. AFAIK, this shouldn't break anything pre-3.10 because it checks things via duck typing. Resolves #371, resolves #348.

Instead of:

from typing import Optional

app = typer.Typer()

@app.command()
def command(option: Optional[str] = None, test: Optional[int] = typer.Option(None)):
  ...

Allow:

app = typer.Typer()

@app.command()
def command(option: str | None = None, test: int | None = typer.Option(None)):
  ...

@github-actions
Copy link

📝 Docs preview for commit 34d651a at: https://63ae3749ecc68308c0751f2a--typertiangolo.netlify.app

@github-actions
Copy link

📝 Docs preview for commit d22b0b2 at: https://63ae429473d46f006486e12f--typertiangolo.netlify.app

@github-actions
Copy link

📝 Docs preview for commit 584adbb at: https://63ae447e3639b1027b3da4da--typertiangolo.netlify.app

@johnthagen johnthagen mentioned this pull request Jan 3, 2023
7 tasks
@github-actions
Copy link

github-actions bot commented Jan 3, 2023

📝 Docs preview for commit 447c55f at: https://63b4abee01dd205283004b17--typertiangolo.netlify.app

potatoeggy added a commit to potatoeggy/mandown that referenced this pull request Feb 17, 2023
waiting for fastapi/typer#522 then i can finally also remove Optional
@HansAarneLiblik
Copy link

Any advancements on this ?

@potatoeggy
Copy link
Author

cc @tiangolo?

@ArVar
Copy link

ArVar commented Mar 20, 2023

I would appriciate this merge as well. :-)

@NiklasRosenstein
Copy link

NiklasRosenstein commented Mar 23, 2023

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 Typer. All it does is run a function being passed to Typer through this beforehand:

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 "str | None" to Optional[str] and "list[int]" to List[int].

It could also replace Typer's currently, if I may say so, seemingly brittle, implementation of introspecting type hints. The typeapi module provides an API for type hints so that Typer could focus on interpreting the actual type hints. I say "brittle" because the code currently tries to do two things at the same time, i.e. trying to work with the unstable and inconsistent typing API while also trying to make sense of the actual typing information.

Disclaimer: I'm the author of typeapi.

Edit: terracomp-typer started as a utility library for another tool; but I've found it useful in many of my projects now which is why I migrated it to typer-builder on PyPI.

@kudlatyamroth
Copy link

can we get this merged?

JimMadge added a commit to alan-turing-institute/data-safe-haven that referenced this pull request Jul 25, 2023
Typer does not support newer union syntax
fastapi/typer#522
JimMadge added a commit to alan-turing-institute/data-safe-haven that referenced this pull request Jul 26, 2023
Typer does not support newer union syntax
fastapi/typer#522

Co-authored-by: James Robinson <[email protected]>
@johnthagen
Copy link

@tiangolo Would you have time to review this PR? ❤️

Copy link

@realstealthninja realstealthninja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@potatoeggy potatoeggy closed this by deleting the head repository Dec 31, 2023
@potatoeggy
Copy link
Author

Superceded by #676, which is a more elegant solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TypeError: unsupported operand type(s) for |: 'type' and 'NoneType' PEP 604 Support
8 participants