Skip to content

False negative when lvalue has union type #17859

Open
@JukkaL

Description

@JukkaL

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions