Skip to content

Inferring a more precise type on assignment to a variable with Any type #2928

Open
@JukkaL

Description

@JukkaL

Mypy infers the type Any for y after the second assignment:

from typing import Any

def f(x: Any) -> None:
    y = x
    y = 1
    reveal_type(y)  # Any

Inferring int as the type might be less surprising. However, if a variable is explicitly declared with an Any type, such as x in the above example, it's less clear what would be the right thing to do.

A more general solution would allow redefining variables with arbitrary new types (#1174).

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions