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

Revisit the body of a loop if the number of partial types has changed. #18180

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,11 +607,14 @@ def accept_loop(
"""
# The outer frame accumulates the results of all iterations
with self.binder.frame_context(can_skip=False, conditional_frame=True):
partials_old = sum(len(pts.map) for pts in self.partial_types)
while True:
with self.binder.frame_context(can_skip=True, break_frame=2, continue_frame=1):
self.accept(body)
if not self.binder.last_pop_changed:
partials_new = sum(len(pts.map) for pts in self.partial_types)
if (partials_new == partials_old) and not self.binder.last_pop_changed:
break
partials_old = partials_new
if exit_condition:
_, else_map = self.find_isinstance_check(exit_condition)
self.push_type_map(else_map)
Expand Down
4 changes: 2 additions & 2 deletions mypyc/test-data/commandline.test
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@ wtvr = next(i for i in range(10) if i == 5)

d1 = {1: 2}

# Make sure we can produce an error when we hit the awful None case
# Since PR 18180, the following pattern should pose no problems anymore:
def f(l: List[object]) -> None:
x = None # E: Local variable "x" has inferred type None; add an annotation
x = None
for i in l:
if x is None:
x = i
Expand Down
37 changes: 37 additions & 0 deletions test-data/unit/check-narrowing.test
Original file line number Diff line number Diff line change
Expand Up @@ -2352,3 +2352,40 @@ def fn_while(arg: T) -> None:
return None
return None
[builtins fixtures/primitives.pyi]

[case testRefinePartialTypeWithinLoop]

x = None
for _ in range(2):
if x is not None:
reveal_type(x) # N: Revealed type is "builtins.int"
x = 1
reveal_type(x) # N: Revealed type is "Union[builtins.int, None]"

def f() -> bool: ...

y = None
while f():
reveal_type(y) # N: Revealed type is "None" \
# N: Revealed type is "Union[builtins.int, None]"
y = 1
reveal_type(y) # N: Revealed type is "Union[builtins.int, None]"

z = [] # E: Need type annotation for "z" (hint: "z: List[<type>] = ...")
def g() -> None:
for i in range(2):
while f():
if z:
z[0] + "v" # E: Unsupported operand types for + ("int" and "str")
z.append(1)

class A:
def g(self) -> None:
z = [] # E: Need type annotation for "z" (hint: "z: List[<type>] = ...")
for i in range(2):
while f():
if z:
z[0] + "v" # E: Unsupported operand types for + ("int" and "str")
z.append(1)

[builtins fixtures/primitives.pyi]
1 change: 1 addition & 0 deletions test-data/unit/fixtures/primitives.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class memoryview(Sequence[int]):
class tuple(Generic[T]):
def __contains__(self, other: object) -> bool: pass
class list(Sequence[T]):
def append(self, v: T) -> None: pass
def __iter__(self) -> Iterator[T]: pass
def __contains__(self, other: object) -> bool: pass
def __getitem__(self, item: int) -> T: pass
Expand Down
Loading