Skip to content

TYPE_CHECKING-only Final-annotated nested always-true/false flags crashes with conditional type aliases #19185

Description

@bzoracler

Crash Report & To Reproduce

See mypy Playground (reproduces on both 1.15 and 1.16).

The motivation in this snippet is to avoid runtime imports from typing.

# mypy: always-false=INTERNAL_DEVELOPMENT_FLAGTYPE_CHECKING: Final=FalseINTERNAL_DEVELOPMENT_FLAG: Final=FalseifTYPE_CHECKING:
fromtyping_extensionsimportTypeAliasType, Final, TypeVarT1=TypeVar("T1")
T2=TypeVar("T2", default=T1)
ifINTERNAL_DEVELOPMENT_FLAG:
Alias=TypeAliasType("Alias", "MyClass", type_params=(T1, T2))
else:
Alias=TypeAliasType("Alias", T1, type_params=(T1, T2))
classMyClass(tuple["T1", "T2"]): ...

The crash seems to be avoidable if the flags aren't annotated with Final, or the typing.* imports aren't under one of the flags.

Traceback

Deferral trace:
__main__:3
__main__:3
__main__:3
__main__:3
__main__:3
__main__:4
__main__:4
__main__:4
__main__:4
__main__:4
__main__:13
__main__:13
__main__:13
__main__:13
__main__:13
__main__:13
__main__:13
__main__:13
__main__:13
__main__:13
__main__:13
__main__:13
__main__:13
__main__:13
__main__:13
__main__:13
__main__:13
__main__:13
__main__:13
main.py: error: INTERNAL ERROR: maximum semantic analysis iteration count reached
Found 1 error in 1 file (errors prevented further checking)

Your Environment

  • Mypy version used: 1.15, 1.16
  • Mypy command-line flags: --show-traceback
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.9, 3.12

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions