diff --git a/mypy/checker.py b/mypy/checker.py index 99ef6c8e6bd6..e1cccb8d79a2 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -2016,6 +2016,10 @@ def infer_variable_type(self, name: Var, lvalue: Lvalue, # Make the type more general (strip away function names etc.). init_type = strip_type(init_type) + # Special case if target is named '_' -- the variable gets type Any. + if isinstance(lvalue, NameExpr) and lvalue.name == '_': + init_type = AnyType(TypeOfAny.special_form) + self.set_inferred_type(name, lvalue, init_type) def infer_partial_type(self, name: Var, lvalue: Lvalue, init_type: Type) -> bool: diff --git a/mypy/types.py b/mypy/types.py index 96b5849ffee2..85c0e5aae906 100644 --- a/mypy/types.py +++ b/mypy/types.py @@ -281,6 +281,7 @@ class TypeOfAny(Enum): from_error = 'from_error' # Is this a type that can't be represented in mypy's type system? For instance, type of # call to NewType...). Even though these types aren't real Anys, we treat them as such. + # Also used for variables named '_'. special_form = 'special_form' # Does this Any come from interaction with another Any? from_another_any = 'from_another_any'