Repository navigation
Is support for Union types on the roadmap? #461
Description
Activity
Just wanted to throw some more engagement on this issue. The number of issues around even the simple
| Noneuse 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 tostrorAnyand 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
strannotation 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.Reacted by Christophe BAL, Neil Girdhar, Matteo Bruni, desmond-dsouza, Sascha Desch, Nikos Alexandris, Alexandr Shashkin, Rachael Sexton, bernardcooke53, Allan Daemon and 1 moreReacted by Matt Sarmiento, gvoysey and George Pearse@ntaylorwss excellent example. much better than the one i gave and, funnily enough, exactly what i'm after as well. cheers!
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 supporttyping.Unionat all... literals of literals automatically merge the options (like a union).See the example here from those docs (the
AllowedColorsexample 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']
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.
Reacted by Rachael Sexton, Hamid Dimyati, bernardcooke53, Amit Kotlovski, Didier Marin, Allan Daemon and Angelo Tata+1 Would love to see Literal handling as well.
RuntimeError: Type not yet supported: typing.LiteralReacted by Amit Kotlovski, Didier Marin and sarvensisUse case for a Union: I'd like my program to accept URL (custom type) or Path as a parameter
source: Path | MyUrlReacted by Richard Höchenberger, Galen Han, Nikos Alexandris, Zhan Rongrui, Ric Sherlock, Janusz Dziurzyński, Allan Daemon, Carlos Moreno, jborman-stonex, George Pearse and 1 moreIs there any workaround for this currently? I've got a usecase where I need to be able to accept any of
bool | str | float | intin a data science CLI application. I'm taking a str and using literal_eval to cast it back to its original type. Is there a better option?Reacted by Allan DaemonIt would be nice if typer would allow me to use a Union when passing a
click_type. I don't mind implementing the union functionality myself, but typer throws an exception before even checking if I passed aclick_type- locked and limited conversation to collaborators
on Feb 22, 2026
First Check
Commit to Help
Example Code
Description
Typer does not yet support Union types. Running the above snippet yields:
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 💖.