mypy icon indicating copy to clipboard operation
mypy copied to clipboard

set.__and__ false-positive involving generic inference

Open cebtenzzre opened this issue 2 years ago • 0 comments

Bug Report

The way mypy does type inference breaks calling a generic function on the right side of set.__and__, because it takes an argument of type AbstractSet[object]. It seems like it is propagating 'object' instead of the more specific type and then complaining when that does not work.

I reported this as a comment on #5738, but this is apparently a different issue because this problem was not fixed when that issue was closed.

To Reproduce

from typing import TypeVar

T = TypeVar('T')

def identity(x: set[T]) -> set[T]:
    return x

x = {0, 1}

print({0} & identity(x))

Actual Behavior

$ mypy repr.py
repr.py:10: error: Argument 1 to "identity" has incompatible type "set[int]"; expected "set[object]"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

$ mypy --new-type-inference repr.py
repr.py:10: error: Argument 1 to "identity" has incompatible type "set[int]"; expected "set[object]"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: mypy 1.5.0+dev.0873230ee60461110bd7bfde7ca3886878aae389 (compiled: no)
  • Python version used: Python 3.11.3

cebtenzzre avatar Jun 18 '23 23:06 cebtenzzre