Skip to content

Commit c87433d

Browse files
author
Guido van Rossum
committed
Special case assignment to '_': always infer 'Any'
Fixes python#465
1 parent 6a8460f commit c87433d

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

mypy/checker.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2016,6 +2016,10 @@ def infer_variable_type(self, name: Var, lvalue: Lvalue,
20162016
# Make the type more general (strip away function names etc.).
20172017
init_type = strip_type(init_type)
20182018

2019+
# Special case if target is named '_' -- the variable gets type Any.
2020+
if isinstance(lvalue, NameExpr) and lvalue.name == '_':
2021+
init_type = AnyType(TypeOfAny.special_form)
2022+
20192023
self.set_inferred_type(name, lvalue, init_type)
20202024

20212025
def infer_partial_type(self, name: Var, lvalue: Lvalue, init_type: Type) -> bool:

mypy/types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ class TypeOfAny(Enum):
281281
from_error = 'from_error'
282282
# Is this a type that can't be represented in mypy's type system? For instance, type of
283283
# call to NewType...). Even though these types aren't real Anys, we treat them as such.
284+
# Also used for variables named '_'.
284285
special_form = 'special_form'
285286
# Does this Any come from interaction with another Any?
286287
from_another_any = 'from_another_any'

0 commit comments

Comments
 (0)