Default Path value in typer.Option combined with Annotated crashes #1213
-
First Check
Commit to Help
Example Code#FILE: broken.py
from pathlib import Path
from typing import Annotated
import typer
DEFAULT_CFG = Path.home() / "config.yaml"
def main(config: Annotated[Path, typer.Option(DEFAULT_CFG, "--config", "-c")]):
typer.echo(f"Using configuration: {config}")
if __name__ == "__main__":
typer.run(main)
#FILE: works.py
from pathlib import Path
import typer
DEFAULT_CFG = Path.home() / "config.yaml"
# Only difference is not using Annotated[]
def main(config: Path = typer.Option(DEFAULT_CFG, "--config", "-c")):
typer.echo(f"Using configuration: {config}")
if __name__ == "__main__":
typer.run(main) DescriptionRunning
Running
Operating SystemLinux Operating System DetailsNo response Typer Version0.15.3 Python Version3.12.3 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Answered by
ulrikrasmussen
May 9, 2025
Replies: 1 comment
-
I found the solution; it is because default values should of course not be passed as the first argument to from pathlib import Path
from typing import Annotated
import typer
DEFAULT_CFG = Path.home() / "config.yaml"
def main(config: Annotated[Path, typer.Option("--config", "-c") = DEFAULT_CFG]):
typer.echo(f"Using configuration: {config}")
if __name__ == "__main__":
typer.run(main) |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
ulrikrasmussen
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I found the solution; it is because default values should of course not be passed as the first argument to
typer.Option
when usingAnnotated
, but instead they should be specified as default values for the method parameter: