Skip to content

Crash with recursive default type parameter #21269

Description

@George-Ogden

Traceback

error: INTERNAL ERROR -- Please try using mypy master on GitHub:
https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 1.20.1
Traceback (most recent call last):
File "mypy/semanal.py", line 7665, in accept
File "mypy/nodes.py", line 1651, in accept
File "mypy/semanal.py", line 1842, in visit_class_def
File "mypy/semanal.py", line 1996, in analyze_class
File "mypy/semanal.py", line 7311, in deferAssertionError: Must not defer during final iterationnote: use --pdb to drop into pdb

To Reproduce

fromtypingimportProtocolclassTrait[T: Pattern=Pattern](Protocol):
defmethod(self, other: T) ->bool:
"""Partial order on generality."""raiseNotImplementedError()
classPattern1(Trait):
defmethod(self, other: Pattern) ->bool:
returnFalseclassPattern2(Trait):
defmethod(self, other: Pattern) ->bool:
returnFalse
type Pattern=Pattern1|Pattern2

This simplified version does not crash:

fromtypingimportProtocolclassTrait[T](Protocol):
defmethod(self, other: T) ->bool:
"""Partial order on generality."""raiseNotImplementedError()
classPattern1(Trait["Pattern"]):
defmethod(self, other: Pattern) ->bool:
returnFalseclassPattern2(Trait["Pattern"]):
defmethod(self, other: Pattern) ->bool:
returnFalse
type Pattern=Pattern1|Pattern2

Your Environment

  • Mypy version used: mypy 1.20.1 (compiled: yes)
  • Mypy command-line flags: --show-traceback
  • Mypy configuration options from mypy.ini (and other config files):
[mypy]warn_unreachable = true
disallow_subclassing_any = true
disallow_incomplete_defs = true
disallow_untyped_defs = true
warn_unused_ignores = true
warn_unused_configs = true
strict_equality = true
exclude_gitignore = true
warn_redundant_casts = true
python_version = 3.14
  • Python version used: 3.14
  • Operating system and version: Linux

Related Issues

This may be a duplicate but is a different way to reproduce the issue.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions