Skip to content

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

Description

@jelle-openai

This crashes mypy:

fromtypingimportGeneric, TypeVardeftest() ->None:
T=TypeVar("T", bound="Model")
classQuery(Generic[T]):
deffilter(self, value: T) ->None:
raiseNotImplementedErrorclassModel:
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

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions