Skip to content

Incorrect inferred type for attribute with two partial types #20257

@JukkaL

Description

@JukkaL

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-inferenceWhen to infer types or require explicit annotations

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions