diff --git a/mypy/checker.py b/mypy/checker.py index 1b11caf01f18..b966492315c7 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -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) diff --git a/mypyc/test-data/commandline.test b/mypyc/test-data/commandline.test index 672e879fbe1e..7c8c2d9d92c9 100644 --- a/mypyc/test-data/commandline.test +++ b/mypyc/test-data/commandline.test @@ -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 diff --git a/test-data/unit/check-narrowing.test b/test-data/unit/check-narrowing.test index 285d56ff7e50..ad59af01010c 100644 --- a/test-data/unit/check-narrowing.test +++ b/test-data/unit/check-narrowing.test @@ -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[] = ...") +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[] = ...") + 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] diff --git a/test-data/unit/fixtures/primitives.pyi b/test-data/unit/fixtures/primitives.pyi index 63128a8ae03d..e7d3e12bd5e6 100644 --- a/test-data/unit/fixtures/primitives.pyi +++ b/test-data/unit/fixtures/primitives.pyi @@ -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