Skip to content

isinstance erases type info #12949

Closed
@gsakkis

Description

@gsakkis

Bug Report

Accessing a variable of a generic type after an if isinstance() against a (non-generic) subclass of said type changes the variable's type to <nothing> within the isinstance clause scope:

from typing import List, TypeVar

class IntList(List[int]):
    pass

T = TypeVar("T")

def f(s: List[T]) -> T:
    reveal_type(s)
    if isinstance(s, IntList):
        reveal_type(s)
        s.pop()
    reveal_type(s)
    return s.pop()

Expected Behavior

No error.

Actual Behavior

note: Revealed type is "builtins.list[T`-1]"
note: Revealed type is "<nothing>"
error: <nothing> has no attribute "pop"  [attr-defined]
note: Revealed type is "builtins.list[T`-1]"

Your Environment

  • Mypy version used: 0.960

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions