Skip to content

Commit 3e38e7d

Browse files
committed
Don't infer infinitely complex types in loops
1 parent 5bd6def commit 3e38e7d

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

mypy/checker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ def accept_loop(
618618
widened_new = len(self.widened_vars)
619619
if (
620620
(partials_new == partials_old)
621-
and not self.binder.last_pop_changed
621+
and (not self.binder.last_pop_changed or iter > 3)
622622
and (widened_new == widened_old or iter > 1)
623623
):
624624
break

test-data/unit/check-redefine2.test

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,3 +1061,16 @@ if int():
10611061

10621062
[out]
10631063
tmp/b.py:1: error: --local-partial-types must be enabled if using --allow-redefinition-new
1064+
1065+
[case testNewRedefineNestedLoopInfiniteExpansion]
1066+
# flags: --allow-redefinition-new --local-partial-types
1067+
def a(): ...
1068+
1069+
def f() -> None:
1070+
while int():
1071+
x = a()
1072+
1073+
while int():
1074+
x = [x]
1075+
1076+
reveal_type(x) # N: Revealed type is "Union[Any, builtins.list[Any], builtins.list[Union[Any, builtins.list[Any]]], builtins.list[Union[Any, builtins.list[Any], builtins.list[Union[Any, builtins.list[Any]]]]], builtins.list[Union[Any, builtins.list[Any], builtins.list[Union[Any, builtins.list[Any]]], builtins.list[Union[Any, builtins.list[Any], builtins.list[Union[Any, builtins.list[Any]]]]]]]]"

0 commit comments

Comments
 (0)