Skip to content

New semantic analyzer: Support multiple assignments to underscore #6450

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

Merged
merged 2 commits into from
Feb 21, 2019
Merged
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
16 changes: 11 additions & 5 deletions mypy/newsemanal/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1938,8 +1938,9 @@ def analyze_typeddict_assign(self, s: AssignmentStmt) -> bool:
# type checker will try to infer Any for the l.h.s.
# Remove this after new analyzer is the default one!
lvalue.fullname = self.qualified_name(name)
lvalue.node = self.make_name_lvalue_var(lvalue, self.current_symbol_kind(),
inferred=True)
lvalue.is_inferred_def = True
lvalue.kind = kind = self.current_symbol_kind()
lvalue.node = self.make_name_lvalue_var(lvalue, kind, inferred=True)
return True

def analyze_lvalues(self, s: AssignmentStmt) -> None:
Expand Down Expand Up @@ -2315,11 +2316,19 @@ def analyze_name_lvalue(self,
added = self.add_symbol(name, var, lvalue)
# Only bind expression if we successfully added name to symbol table.
if added:
lvalue.is_new_def = True
lvalue.is_inferred_def = True
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this depend on explicit_type (presence of type annotation)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, I just copied the logic from make_name_lvalue_var(). My understanding is that process_type_annotation() which is called after analyze_lvalues() will set it to False (depending on annotation).

TBH, the logic is quite opaque here, and should be clean-up. I would however not do this because analyzers are merged, because it is very easy to break checker. I will create a follow-up issue.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened #6458

lvalue.kind = kind
lvalue.node = var
if kind == GDEF:
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)
if is_final and self.is_final_redefinition(kind, name):
self.fail("Cannot redefine an existing name as final", lvalue)
else:
Expand Down Expand Up @@ -2378,9 +2387,6 @@ def make_name_lvalue_var(self, lvalue: NameExpr, kind: int, inferred: bool) -> V
# fullanme should never stay None
v._fullname = lvalue.name
v.is_ready = False # Type not inferred yet
lvalue.is_new_def = True
lvalue.is_inferred_def = True
lvalue.kind = kind
return v

def make_name_lvalue_point_to_existing_def(
Expand Down
1 change: 0 additions & 1 deletion mypy/test/hacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
'check-incomplete-fixture.test',
'check-incremental.test',
'check-inference-context.test',
'check-inference.test',
'check-isinstance.test',
'check-literal.test',
'check-modules.test',
Expand Down
10 changes: 7 additions & 3 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -432,14 +432,16 @@ def id(x: T) -> T:
[out]

[case testUnderspecifiedInferenceResult]
# flags: --new-semantic-analyzer --no-strict-optional
from typing import TypeVar
T = TypeVar('T')
class A: pass
a = None # type: A

def ff() -> None:
x = f() # E: Need type annotation for 'x'
reveal_type(x) # E: Revealed type is 'Any'
reveal_type(x) # E: Revealed type is 'Any' \
# E: Cannot determine type of 'x'

g(None) # Ok
f() # Ok because not used to infer local variable type
Expand Down Expand Up @@ -1045,7 +1047,7 @@ class B: pass
[builtins fixtures/for.pyi]

[case testReusingInferredForIndex2]
# flags: --allow-redefinition
# flags: --allow-redefinition --no-new-semantic-analyzer

def f() -> None:
for a in [A()]: pass
Expand Down Expand Up @@ -1550,7 +1552,9 @@ def add():
map[1] = 2
[builtins fixtures/dict.pyi]

-- Depends on #6425
[case testSpecialCaseEmptyListInitialization]
# flags: --no-new-semantic-analyzer
def f(blocks: Any): # E: Name 'Any' is not defined
to_process = [] # E: Need type annotation for 'to_process'
to_process = list(blocks)
Expand Down Expand Up @@ -2455,7 +2459,7 @@ _ = 0
_ = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")

[case testUnusedTargetNotClass]
# flags: --allow-redefinition
# flags: --allow-redefinition --no-new-semantic-analyzer
class C:
_, _ = 0, 0
_ = ''
Expand Down