Skip to content

mypy seems to forget inferred type after asigning an Any typed variable #22518

Open

Description

@calestyo

Hey

Bug Report

The documentation gives the following example:

from typing import Any

num = 1         # Statically typed (inferred to be int)
num = 'x'       # error: Incompatible types in assignment (expression has type "str", variable has type "int")

dyn: Any = 1    # Dynamically typed (type Any)
dyn = 'x'       # OK

num = dyn       # No error, mypy will let you assign a value of type Any to any variable
num += 1        # Oops, mypy still thinks num is an int

the last line says, that even after assigning an Any typed value to num, mypy still holds it for an int, which is also why the example shows now typing error but the Oops because there’ll be a runtime error.
However, it seems that mypy actually does "narrow down" the inferred type to Any, which unfortunately causes all later type checking to be ignored.

To Reproduce

     1	from typing import Any
     2	
     3	dyn: Any = 1
     4	
     5	num = 1
     6	num = dyn
     7	reveal_type(num)
     8	num.nonexistent()
     9	num = "x"
    10	
    11	annotated: int = 1
    12	annotated = dyn
    13	reveal_type(annotated)
    14	annotated.nonexistent()

which gives:

foo.py:7: note: Revealed type is "Any"
foo.py:9: error: Incompatible types in assignment (expression has type "str", variable has type "int")  [assignment]
foo.py:13: note: Revealed type is "int"
foo.py:14: error: "int" has no attribute "nonexistent"  [attr-defined]
Found 2 errors in 1 file (checked 1 source file)

So for num, whose type is inferred from its first assignment:

  • the inferred type int is still enforced for assignments (line 9 is an error as expected),
  • but after num = dyn, mypy treats num as Any, so num.nonexistent() (line 8) is not reported.

For annotated, which has an explicit annotation, mypy keeps the type int after the assignment of dyn (as described in the documentation), so annotated.nonexistent() is reported.

Expected Behavior

mypy keeps treating num as int after the assignment of an Any value, as the documentation describes and as it already does for explicitly annotated variables, or

I mean one could just update the documentation to the actual beahviour, but that seems pretty odd, as a single unexpected Any assignment could cause quite some unnoticed errors?

Your Environment

  • Mypy version used: 2.4.1
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.13.5

Thanks,
Chris.

Activity

  1. deleted a comment from zhengdaodeguL on Nov 19, 2026
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 wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions