omni-us / jsonargparse

Implement minimal boilerplate CLIs derived from type hints and parse from command line, config files and environment variables
https://jsonargparse.readthedocs.io
MIT License
326 stars 49 forks source link

Selecting a subtype raises an exception in Python 3.12.7 #590

Closed eggry closed 1 month ago

eggry commented 1 month ago

🐛 Bug report

To reproduce

First, create a file named demo.py with the following content:

from typing import Optional
from jsonargparse import CLI

class Foo: ...

class Bar(Foo):
    def __init__(self, val: int) -> None:
        self.val = val

def main(data: Optional[Foo] = None) -> None:
    print(data)

if __name__ == "__main__":
    CLI(main)

Then, run the following command in your terminal:

python demo.py --data Bar --data.val 1

The output is:

usage: demo.py [-h] [--config CONFIG] [--print_config[=flags]] [--data.help CLASS_PATH_OR_NAME] [--data DATA]
error: cannot unpack non-iterable ActionTypeHint object

Such behavior will also affect LitGPT. For example, the command

    litgpt finetune tiiuae/falcon-7b \
        --data JSON \
        --data.json_path data/mydata.json \
        --out_dir data/mydata-finetuned

will also result in the same error because JSON is a subclass of DataModule.

Expected behavior

On Python 3.12.6, the output is:

<__main__.Bar object at 0x*******>

Environment

mauvilsa commented 1 month ago

Fixed with #591