diff --git a/mypy/semanal.py b/mypy/semanal.py index ea5ccb73c05f..124bcb21b56b 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -2878,11 +2878,18 @@ def analyze_name_lvalue(self, lvalue.fullname = var._fullname else: lvalue.fullname = lvalue.name - if self.is_func_scope(): - if unmangle(name) == '_': - # Special case for assignment to local named '_': always infer 'Any'. - typ = AnyType(TypeOfAny.special_form) - self.store_declared_types(lvalue, typ) + + # Special case for assignment to local named '_': always infer 'Any'. + is_special_unused = self.is_func_scope() and unmangle(name) == '_' + + # Special case, `Any` is defined as `= object()` in typeshed, + # so we need to make it a real type variable. + is_typing_any = var.fullname == 'typing.Any' + + if is_special_unused or is_typing_any: + typ = AnyType(TypeOfAny.special_form) + self.store_declared_types(lvalue, typ) + if is_final and self.is_final_redefinition(kind, name): self.fail("Cannot redefine an existing name as final", lvalue) else: