mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Incorrect inferred type for attribute with two partial types

Open JukkaL opened this issue 2 months ago • 1 comments

Unexpected type is inferred for attribute x in this example:

class C:
    def __init__(self) -> None:
        if int():
            self.x = None
            return
        self.x = []
        self.x.append("x")

reveal_type(C().x)  # "list[str]" -- but should be "list[str] | None"

The inferred type should be list[str] | None, or alternatively mypy could require a type annotation for x, if this is too tricky to infer.

This should be tested with and without --allow-redefinition-new, since it may make a difference.

JukkaL avatar Nov 18 '25 17:11 JukkaL

hello , i would like to work on this issue. Could you please confirm if this issue is still open for contributions? I would be happy to start working on it.

Thank you!

lamia-bel avatar Dec 08 '25 10:12 lamia-bel