Optional[Path] = typer.Argument returns TypeError when passed into open()
#1315
-
First Check
Commit to Help
Example Code# cli.py
@app.command()
def main(
config_file: Optional[Path] = typer.Argument(
"config.yaml", # Default if not provided
exists=True,
file_okay=True,
dir_okay=False,
writable=False,
readable=True,
resolve_path=True,
)
):
config_data = parse_yaml_config_file(config_file)
# config.py
import yaml
def parse_yaml_config_file(file) -> dict:
with open(file) as yaml_file:
return yaml.safe_load(yaml_file)DescriptionThe above code works as expected in dev, and all unit tests pass. However, after creating a I've check my debugger and with print, and the I've tried converting to a I'm not sure why it seems to be of type Operating SystemWindows Operating System DetailsWindows 11 Typer Version0.4.0 Python Version3.10 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
|
Maybe you are using calling main() instead of typer.run(main)? |
Beta Was this translation helpful? Give feedback.
-
|
I am facing a similar issue when using However, during the check A snippet for the explaining the basic issue is there: >>> import typer
>>> var = typer.Option(None)
>>> var
<typer.models.OptionInfo object at 0x7fc09002d880>
>>> var is None
FalseThe expectation is that This causes the error that I get: AttributeError: 'OptionInfo' object has no attribute 'open'as I am trying to read the file. |
Beta Was this translation helpful? Give feedback.
-
|
@Mirandatz nailed it ❤️. In my case I had a entry point defined with Poetry: [tool.poetry.scripts]
fact = "fact.cli:main"But I was pointing that to: def main(n: int = Argument(..., min=0, help="The input n of fact(n)")) -> None:But that wasn't being wrapped in I fixed this by pointing the console script at: def entry_point() -> None:
typer.run(main) |
Beta Was this translation helpful? Give feedback.
@Mirandatz nailed it ❤️.
In my case I had a entry point defined with Poetry:
But I was pointing that to:
But that wasn't being wrapped in
typer.run(main).I fixed this by pointing the console script at: