mypy icon indicating copy to clipboard operation
mypy copied to clipboard

False negative when lvalue has union type

Open JukkaL opened this issue 1 year ago • 1 comments

Mypy generates no error, even though the code is unsafe:

class C[T]:
    a: T

def f(x: C[int] | C[str]) -> None:
    x.a = 1  # No error

c = C[str]()
f(c)
print(c.a, type(c.a))  # 1, int

I'd expect x.a = 1 to generate an error, since the assignment is unsafe.

I suspect that the lvalue type is inferred as int | str, and mypy thinks it's fine. instead, assignment should be valid for each union item separately.

JukkaL avatar Oct 01 '24 14:10 JukkaL