FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Revisit the body of a loop if the number of partial types has changed. by tyralla · Pull Request #18180 · python/mypy · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .pyi  (1) .test  (2) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
5 changes: 4 additions & 1 deletion mypy/checker.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL