jsonargparse icon indicating copy to clipboard operation
jsonargparse copied to clipboard

In python 3.11, add_subclass_arguments with list[Class] is not working.

Open harimkang opened this issue 4 months ago • 1 comments

🐛 Bug report

Hi all, I'm working on a project that utilizes this great library. However, I ran into a problem with python 3.11.

scheduler:
  - class_path: otx.algo.schedulers.warmup_schedulers.LinearWarmupScheduler
    init_args:
      num_warmup_steps: 10
  - class_path: lightning.pytorch.cli.ReduceLROnPlateau
    init_args:
      mode: max
      factor: 0.5
      patience: 1
      monitor: val/accuracy
parser.add_subclass_arguments(
            baseclass=(LRScheduler, ReduceLROnPlateau, list[LRSchedulerTypeUnion]),
            nested_key="scheduler",
            **scheduler_kwargs,
        )

I've been configuring arguments like the one above in project, which is mainly python 3.10 (& 3.9), but this doesn't work since python 3.11. The reason for this is that as of python 3.11, list[Optimizer] is False in inspect.isclass(). https://github.com/omni-us/jsonargparse/blob/88fc9c8782fd959aa844b9bfa1ab3a324132b842/jsonargparse/_signatures.py#L502-L503 If I comment out that if statement and the raise statement, this works fine. Do I need to use a different method to use list[Optimizer] or list[Scheduler]? This used to work in python 3.9 and 3.10, but a new workaround is needed in 3.11.

I was wondering if could relax that if statement to make it more flexible.

To reproduce

Expected behavior

Environment

  • jsonargparse version (e.g., 4.8.0):
  • Python version (e.g., 3.9):
  • How jsonargparse was installed (e.g. pip install jsonargparse[all]):
  • OS (e.g., Linux):

harimkang avatar Feb 22 '24 12:02 harimkang