Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when parsing config file with type=Path #94

Closed
alealv opened this issue Sep 23, 2021 · 3 comments
Closed

Error when parsing config file with type=Path #94

alealv opened this issue Sep 23, 2021 · 3 comments
Labels
bug Something isn't working question Further information is requested

Comments

@alealv
Copy link

alealv commented Sep 23, 2021

How to reproduce

from jsonargparse import ArgumentParser
from pathlib import Path

parser = ArgumentParser()
parser.add_argument("--path", type=Path)
parser.parse_args(["--path", "any/path"])

Error

test.py: error: Parser key "output_dir": not enough values to unpack (expected 2, got 1)

Version

➜ pip list | grep jsonarg
jsonargparse                       3.19.3

Tracing

The error fails here and it's called from here.

@mauvilsa
Copy link
Member

mauvilsa commented Sep 23, 2021

Thank you for reporting!

Note that pathlib.Path is not supported as a core type. What is happening when you add an argument with type=Path is that it creates a subclass argument, see https://jsonargparse.readthedocs.io/en/stable/#class-type-and-sub-classes. The not enough values to unpack error is a bug since it should be giving the same error as if you did parser.parse_args(["--path", "any/path.ext"]). Will look at this.

Also note that jsonargparse has specific types for checking paths, see https://jsonargparse.readthedocs.io/en/stable/#parsing-paths. If you specifically want pathlib.Path as a core type, then you can register it, see https://jsonargparse.readthedocs.io/en/stable/#registering-types.

from jsonargparse.typing import register_type
from pathlib import Path
register_type(Path, type_check=lambda v, t: isinstance(v, t))

@mauvilsa mauvilsa added the bug Something isn't working label Sep 23, 2021
@mauvilsa
Copy link
Member

Even though pathlib.Path can be registered, it is not very useful. The issue is that pathlib.Path accepts any string, thus no validation and the point of the parser is that values are validated and fail if invalid. That is why there are path types in jsonargparse which do validation.

@alealv
Copy link
Author

alealv commented Sep 24, 2021

Thanks for the explanation.

I noticed that jsonargparse has specific Path type checking but I just wanted to have an object of type pathlib.Path and do the validation or dir creation afterward in the code.

@mauvilsa mauvilsa added the question Further information is requested label Sep 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants