Skip to content

Commit

Permalink
Special case assignment to '_': always infer 'Any'
Browse files Browse the repository at this point in the history
  • Loading branch information
Guido van Rossum committed Mar 30, 2018
1 parent ad5b302 commit 1cfcda0
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
4 changes: 4 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2013,6 +2013,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:
Expand Down
1 change: 1 addition & 0 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 1cfcda0

Please sign in to comment.