Skip to content

Incorrect inferred type for attribute with two partial types #20257

Open

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.

Activity

  1. added
    bugmypy got something wrong
    topic-inferenceWhen to infer types or require explicit annotations
    on Nov 18, 2025
  2. lamia-bel commented on Dec 8, 2025

    @lamia-bel

    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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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