Open OnkelTem opened 3 years ago
This is the expected behavior. When you pass exists=True
, you are telling Typer that the value that is passed needs to exist.
When you pass exists=False
, you are telling Typer that the value does not need to be checked for existence.
When you pass exists=True, you are telling Typer that the value that is passed needs to exist.
I think that should just check for existence and not raise unhandled fatal error.
I guess the problem lies underneath, in Click.
When you pass exists=True, you are telling Typer that the value that is passed needs to exist.
I think that should just check for existence and not raise unhandled fatal error.
What do you expect to happen instead? What does "just check for existence" actually mean?
Sorry don't you really get what's going on folks? :) The application just crashes. No nice message in the cli. It crashes.
What does "just check for existence" actually mean?
Return usage, Problem. Not the crash.
I remind, if I pass a wrong path via cli params - it works just well, it shows a regular error message about improper usage of params of the tool. But if I try to default it to unexisting dir, it crashes. And my defaults are not hardcoded, they're read from a config file.
You can circumvent the problem if you use a str
instead of a pathlib.Path
, e.g. like this:
def cli(
path: Path = typer.Option(
str(DEFAULT_MEDIA_DIR),
exists=True,
),
):
pass
You are right in saying that the problem lies in click. See this example:
import click
from pathlib import Path
DEFAULT_DIR = Path(".") / "non-existing-dir" # raises AttributeError
# DEFAULT_DIR = "./non-existing-dir" # works fine
@click.command()
@click.argument("path", default=DEFAULT_DIR, type=click.Path(exists=True))
def main(path):
click.echo(click.format_filename(path))
if __name__ == "__main__":
main()
This is probably related to https://github.com/pallets/click/issues/405.
There is no longer any crash, so this should probably be closed.
(I'm not sure if https://github.com/pallets/click/issues/405 is actually related to it, but in any case that is now implemented too in Click 8.x.)
Currently I'm not at my workstation so cannot check it.
Python: 3.8 Typer: 0.3.2
I have an optional path option of type Path and I want to assign it a default value:
But it fails with error:
If I remove the default value and set it to None, then it goes well.
I found only one workaround: set
exists=False
and check for existence in the body of the function.