[BUG] typing_extensions.Annotated
cannot be used with from __future__ import annotations
in py38
#802
-
First Check
Commit to Help
Example Codefrom __future__ import annotations
import typer
from typing_extensions import Annotated
def main(force: Annotated[bool, typer.Option("--force")] = False):
if force:
print("Forcing operation")
else:
print("Not forcing")
if __name__ == "__main__":
typer.run(main) DescriptionThis issue only happens with Python ≤ 3.8 because as of py39, The above is taken from the official tutorial and adding
Operating SystemWindows Operating System DetailsNo response Typer Version0.12.3 Python Version3.8 Additional Contextvenv packages:
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
I've noticed the same thing. To add to your report: I've tested with python versions |
Beta Was this translation helpful? Give feedback.
-
I proposed a fix here: #814 |
Beta Was this translation helpful? Give feedback.
-
Unfortunately I found that even with this patch, the following env combination still won't work well.
Reproducerfrom __future__ import annotations
from typing import Optional
from typing_extensions import Annotated
import typer
def patch_typer() -> None:
import typer.utils
from typing_extensions import get_type_hints
setattr(typer.utils, "get_type_hints", get_type_hints)
patch_typer()
def main(
accept: Annotated[
Optional[bool],
typer.Option("--accept/--reject", help="Accept or reject the thing"),
] = True,
) -> None:
if accept:
print("Accepting!")
else:
print("Rejecting!")
if __name__ == "__main__":
typer.run(main) run it with $ python test.py --help and I get this
It still suffers the same issue: #721
|
Beta Was this translation helpful? Give feedback.
I proposed a fix here: #814
It was rather simple to fix in the end ^^