Replies: 11 comments
This comment has been hidden.
This comment has been hidden.
-
|
Just wanted to throw some more engagement on this issue. The number of issues around even the simple A quick example of what I'm looking to do: Currently, I have to fall back on a |
Beta Was this translation helpful? Give feedback.
-
|
@ntaylorwss excellent example. much better than the one i gave and, funnily enough, exactly what i'm after as well. cheers! |
Beta Was this translation helpful? Give feedback.
-
|
Just wanted to chime in here. The official Mypy docs lean toward using See the example here from those docs (the 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'] |
Beta Was this translation helpful? Give feedback.
-
|
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. |
Beta Was this translation helpful? Give feedback.
-
|
+1 Would love to see Literal handling as well.
|
Beta Was this translation helpful? Give feedback.
-
|
Use case for a Union: I'd like my program to accept URL (custom type) or Path as a parameter |
Beta Was this translation helpful? Give feedback.
-
|
Is there any workaround for this currently? I've got a usecase where I need to be able to accept any of |
Beta Was this translation helpful? Give feedback.
-
|
It would be nice if typer would allow me to use a Union when passing a |
Beta Was this translation helpful? Give feedback.
-
|
Please, please make UNION possible for typer as e.g. |
Beta Was this translation helpful? Give feedback.
-
|
A start could be allowing union types when a parser is added? def parseit(val: str) -> int | str | None:
try:
return int(val)
except ValueError:
if val.lower() == "none":
return None
return val
@cli.command()
def _main(val: Annotated[int | str | None, typer.Argument(parser=parseit)]) -> None:
if isinstance(val, int):
print(flaf.get_one(val))
else:
print(f"Got non-int value: {val!r}")The only "workaround" I've found is exploiting a class class TyperUnion[T]:
def __init__(self, val: T) -> None:
self.val = val
def parseit(val: str) -> TyperUnion[int | str | None]:
try:
return TyperUnion(int(val))
except ValueError:
if val.lower() == "none":
return TyperUnion(None)
return TyperUnion(val)
@cli.command()
def _main(val: Annotated[TyperUnion[int | str | None], typer.Argument(parser=parseit)]) -> None:
_val = val.val
if isinstance(_val, int):
print(_val)
else:
print(f"Got non-int value: {_val!r}") |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
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 💖.
Beta Was this translation helpful? Give feedback.
All reactions