Skip to content

Type of instance variable narrowed incorrectly after is not expression #20736

@p-sawicki

Description

@p-sawicki

Bug Report

Type of a.foo in the example below is narrowed to what looks like a function type which results in a strange error on the call to append later.

Issue started with ad30be8 @hauntsaninja

To Reproduce

from typing import Any

def f(a: Any, x: Any) -> None:
    if x is not list:
        x = []
    reveal_type(x) # note: Revealed type is "Any"
    if a.foo is not list:
        a.foo = []
    reveal_type(a.foo) # note: Revealed type is "Any | Overload(def [_T] () -> builtins.list[_T`1], def [_T] (typing.Iterable[_T`1]) -> builtins.list[_T`1])"
    a.foo.append(x) # error: Too few arguments for "append" of "list"  [call-arg]

Expected Behavior

a.foo should probably be typed as Any.

Actual Behavior

/Users/psawicki/test.py:6: note: Revealed type is "Any"
/Users/psawicki/test.py:9: note: Revealed type is "Any | Overload(def [_T] () -> builtins.list[_T`1], def [_T] (typing.Iterable[_T`1]) -> builtins.list[_T`1])"
/Users/psawicki/test.py:10: error: Too few arguments for "append" of "list"  [call-arg]

Your Environment

  • Mypy version used: mypy 1.20.0+dev.ad30be89f146adaae398bd854146952158a53cad and up
  • Mypy command-line flags: mypy ~/test.py
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.13.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-type-narrowingConditional type narrowing / binder

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions