Skip to content

Parsing TYPE_CHECKING guarded import doesn't work in combination with Type #572

@EthanMarx

Description

@EthanMarx

🐛 Bug report

When using the Type annotation in conjunction with a TYPE_CHECKING guarded import, e.g.

from typing import Type, TYPE_CHECKING
from jsonargparse import ArgumentParser

if TYPE_CHECKING:
    from pytorch.lightning.cli import LightningCLI

def dummy(cli_cls: Type["LightningCLI"]):
    print(cli_cls)


def main():
    parser = ArgumentParser()
    parser.add_function_arguments(dummy)
    args = parser.parse_args()

I get the following error

ValueError: With fail_untyped=True, all mandatory parameters must have a supported type. Parameter 'cli_cls' from '__main__.test' does not specify a type.

Although, this parser set up works when removing the TYPE_CHECKING guarded import, and importing directly

Expected behavior

Environment

  • jsonargparse version: 4.3.1
  • Python version: 3.9
  • How jsonargparse was installed: poetry
  • OS: linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions