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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy