Skip to content

Crash with recursive default type parameter #21269

@George-Ogden

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 defer
AssertionError: Must not defer during final iteration
note: use --pdb to drop into pdb

To Reproduce

from typing import Protocol


class Trait[T: Pattern = Pattern](Protocol):
    def method(self, other: T) -> bool:
        """Partial order on generality."""
        raise NotImplementedError()


class Pattern1(Trait):
    def method(self, other: Pattern) -> bool:
        return False

class Pattern2(Trait):
    def method(self, other: Pattern) -> bool:
        return False

type Pattern = Pattern1 | Pattern2

This simplified version does not crash:

from typing import Protocol


class Trait[T](Protocol):
    def method(self, other: T) -> bool:
        """Partial order on generality."""
        raise NotImplementedError()


class Pattern1(Trait["Pattern"]):
    def method(self, other: Pattern) -> bool:
        return False

class Pattern2(Trait["Pattern"]):
    def method(self, other: Pattern) -> bool:
        return False

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