-
-
Notifications
You must be signed in to change notification settings - Fork 50
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
Command line parsing of *.json values #159
Comments
The parsing depends on the type hints. What is the type hint for |
If you don't have type hints, then add them. This is how the parser knows how to validate. In LightningCLI it is configured such that when there is no type hint, it defaults to |
In my LightningDataModule, the type hint for
where Could this be an issue? |
With the following: import os
from jsonargparse import ArgumentParser
from typing import Any, List, Union
parser = ArgumentParser()
PathLike = Union[str, os.PathLike]
parser.add_argument('--path', type=Union[List[PathLike], PathLike])
cfg = parser.parse_args(['--path=issue_159.json'])
print(cfg) The result is correct
|
@mauvilsa I updated the description with the code to reproduce. I have initialized the arguments with |
@LourencoVazPato thank you for adding the reproduction code. Unfortunately I have been unable to reproduce it. First I tried in a normal virtual environment and then I tried with poetry to be a close as possible to what you reported. In Ubuntu 20.04 I get: $ poetry run python issue_159.py
The currently activated Python version 3.8.10 is not supported by the project (^3.9).
Trying to find and use a compatible version.
Using python3.9 (3.9.13)
Namespace(data=Namespace(class_path='__main__.SubDataModule', init_args=Namespace(data_path='data.json')))
Namespace(data=<__main__.SubDataModule object at 0x7f7c5f185c70>) The
|
@mauvilsa I've been able to reproduce with this poetry env: [tool.poetry]
name = "issue-159"
version = "0.1.0"
description = ""
authors = []
[tool.poetry.dependencies]
python = "^3.9"
jsonargparse = {extras = ["signatures"], version = "4.13.0"}
pandas = "^1.4.4"
pytorch-lightning = "1.6.5"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api" If you want to reproduce my python env here's the poetry lock. poetry.lock
|
- Fixed subclass nested argument incorrectly loaded as subclass config #159.
I had a silly mistake when trying to reproduce. I was able to reproduce it in a normal virtual environment and any python version. I have pushed the fix in commit 744c0c1. |
🐛 Bug report
Not sure if a bug or a feature, but when I call a script (e.g. PyTorch-Lightning CLI) with an argument like
--dataset_path *.json
, the parser reads the json file and interprets it as a configuration file (not as a dataset file in this case), and errors out because it is not a valid config file.I can see there's documentation on parsing file paths, but cannot find any reference on reading them as string arguments.
Is it possible to disable this parsing? What other alternatives are there?
Thanks in advance
To reproduce
Create a
data.json
file containing some JSON dataset. E.g.:Errors out with:
Curiously, it passes if I use a
data.csv
instead of JSON.Expected behavior
The parser reads the arg value as a
str
.Environment
pip install jsonargparse[all]
): poetry add jsonargparseThe text was updated successfully, but these errors were encountered: