Skip to content

Crash on generic class inside a function with a forward-reference bound #21550

@jelle-openai

Description

@jelle-openai

This crashes mypy:

from typing import Generic, TypeVar

def test() -> None:
    T = TypeVar("T", bound="Model")

    class Query(Generic[T]):
        def filter(self, value: T) -> None:
            raise NotImplementedError

    class Model:
        pass

mypy (both 2.1.0 and current master) crashes on this with:

AssertionError: Must not defer during final iteration

Necessary pieces, from reduction:

  • The definitions need to be inside a function.
  • The TypeVar needs a forward-ref bound to a later local class: bound="Model".
  • The local generic class needs to use that T in a method signature.

Similar issues: #12112, #13678, #18579.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions