Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Treat underscore variables as anonymous #3125

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1566,12 +1566,14 @@ def check_lvalue(self, lvalue: Lvalue) -> Tuple[Type, IndexExpr, Var]:
elif isinstance(lvalue, IndexExpr):
index_lvalue = lvalue
elif isinstance(lvalue, MemberExpr):
lvalue_type = self.expr_checker.analyze_ordinary_member_access(lvalue,
True)
lvalue_type = self.expr_checker.analyze_ordinary_member_access(lvalue, True)
self.store_type(lvalue, lvalue_type)
elif isinstance(lvalue, NameExpr):
lvalue_type = self.expr_checker.analyze_ref_expr(lvalue, lvalue=True)
self.store_type(lvalue, lvalue_type)
if lvalue.name == '_':
lvalue_type = AnyType()
else:
self.store_type(lvalue, lvalue_type)
elif isinstance(lvalue, TupleExpr) or isinstance(lvalue, ListExpr):
types = [self.check_lvalue(sub_expr)[0] for sub_expr in lvalue.items]
lvalue_type = TupleType(types, self.named_type('builtins.tuple'))
Expand Down
3 changes: 2 additions & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3294,7 +3294,8 @@ def name_not_defined(self, name: str, ctx: Context) -> None:
self.fail(message, ctx)

def name_already_defined(self, name: str, ctx: Context) -> None:
self.fail("Name '{}' already defined".format(name), ctx)
if name != '_':
self.fail("Name '{}' already defined".format(name), ctx)

def fail(self, msg: str, ctx: Context, serious: bool = False, *,
blocker: bool = False) -> None:
Expand Down
20 changes: 20 additions & 0 deletions test-data/unit/check-basic.test
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,26 @@ main:3: error: Incompatible types in assignment (expression has type "B", variab
main:7: error: Incompatible types in assignment (expression has type "A", variable has type "B")
main:9: error: Incompatible types in assignment (expression has type "B", variable has type "A")

[case testUnderscoreVariableIsFresh]
from typing import Tuple
_ = 1
_ = ""
_ = "" # type: int
(_, _) = ("", 1.0 + "") # E: Unsupported left operand type for + ("float")
(_, _) = (1.0, "")
for (_, _) in ((1, "a"), (2, "b")): pass
(None for (_, _) in ((1, "a"), (2, "b")))

class A:
def __enter__(self) -> Tuple[int, str]: ...
def __exit__(self, *_) -> None: ...

with A() as (_, _): pass

def f(_: int) -> None:
_ = ""
_, _ = (1.0, b"")

[case testGlobalDefinedInBlockWithType]

class A: pass
Expand Down