Skip to content

MyPy treats initialization of a variable as an assignment for variance checks for complex dictionary. #20102

@markshannon

Description

@markshannon

The problem is that when initializing a variable from a dictionary expression it treats the type as invariant, but in the case of initialialization, it is covariant.

Example:

CODES = {
    "a": "b",
    "c": "d",
    "e": "f",
}

# MyPy rejects this

PATTERNS: dict[str, tuple[str | None, str | None]] = {
    "1" + k: ((v + "1" if v else None), "x")
    for (k, v) in CODES.items()
} | {
    "2" + k: ((v + "2" if v else None), "x")
    for (k, v) in CODES.items()
}

# But is OK with this:

X: str | None = "x"

PATTERNS2: dict[str, tuple[str | None, str | None]] = {
    "1" + k: ((v + "1" if v else None), X)
    for (k, v) in CODES.items()
} | {
    "1" + k: ((v + "1" if v else None), X)
    for (k, v) in CODES.items()
}

Both patterns should be OK.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-type-contextType context / bidirectional inference

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions