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

Is support for Union types on the roadmap? #461

Open
7 tasks done
msarmi9 opened this issue Sep 24, 2022 · 7 comments
Open
7 tasks done

Is support for Union types on the roadmap? #461

msarmi9 opened this issue Sep 24, 2022 · 7 comments
Labels
investigate question Question or problem

Comments

@msarmi9
Copy link

msarmi9 commented Sep 24, 2022

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the Typer documentation, with the integrated search.
  • I already searched in Google "How to X in Typer" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to Typer but to Click.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

import typer
from typing import Union


def main(name: Union[str, int]):
    print(f"Hello {name}")


if __name__ == "__main__":
    typer.run(main)

Description

Typer does not yet support Union types. Running the above snippet yields:

AssertionError: Typer Currently doesn't support Union types

Not sure how much demand there is for this feature, but I'd personally love to have it.

Operating System

macOS

Operating System Details

No response

Typer Version

0.6.1

Python Version

3.10

Additional Context

Typer is amazing. Absolutely in love with it 💖.

@msarmi9 msarmi9 added the question Question or problem label Sep 24, 2022
@EmmaJaneBonestell
Copy link

EmmaJaneBonestell commented Jan 13, 2023

https://typer.tiangolo.com/tutorial/commands/help/#help-panels-for-cli-parameters
It might be nice to remove Unions from your examples if it's not on the horizon.

@ntaylorwss
Copy link

ntaylorwss commented Jan 15, 2023

Just wanted to throw some more engagement on this issue. The number of issues around even the simple | None use case (Union syntax for Optional types) seems to be increasing. For my part, I have a growing number of cases in my CLI where I want to use a Union of types (particularly enums), and I've had to resort to str or Any and parse the type inside the command, because of this missing feature. I would really appreciate if this picked up steam soon.

A quick example of what I'm looking to do:

class EnumA(str, enum.Enum):
    A = "A"
    B = "B"

class EnumB(str, enum.Enum):
    C = "C"
    D = "D"

@cli.command()
def my_command(choice: EnumA | EnumB = typer.Argument(..., help="")) -> None:
    typer.echo(f"You chose {choice}")

Currently, I have to fall back on a str annotation and then parse the str into an enum as the first operation of the command. This also means the documentation for the command is lacking clarity on the options, unless I write it out myself in the help.

@msarmi9
Copy link
Author

msarmi9 commented Jan 19, 2023

@ntaylorwss excellent example. much better than the one i gave and, funnily enough, exactly what i'm after as well. cheers!

@rtbs-dev
Copy link

Just wanted to chime in here. The official Mypy docs lean toward using typing.Literal[] syntax for these kinds of cases, and you don't actually need to support typing.Union at all... literals of literals automatically merge the options (like a union).

See the example here from those docs (the AllowedColors example is particularly salient).

Note that supporting literals would solve #76, which has a lot of recent activity, despite a bot marking it closed 😅... I think #669 is a pretty complete PR for this, though @pchanial would have to chime in on whether that PR already supports compound literals already. :)

To demonstrate @ntaylorwss 's example,

OptA = Literal['A']
OptB = Literal['B']

MyOpts = Literal[OptA, OptB]

### or, equivalently, but without needed OptA/B later ###
MyOpts = Literal['A', 'B']

@pchanial
Copy link

I haven't tried compound Literals, but that's something I could look at if @tiangolo is willing to add Literal handling on the roadmap.

@dotKokott
Copy link

+1 Would love to see Literal handling as well.

RuntimeError: Type not yet supported: typing.Literal

@rafalkrupinski
Copy link

rafalkrupinski commented Feb 20, 2024

Use case for a Union: I'd like my program to accept URL (custom type) or Path as a parameter
source: Path | MyUrl

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

No branches or pull requests

8 participants