Skip to content

Commit 3c5d0f0

Browse files
committed
Fix regression in container check logic
This PR fixes the crash reported in python#8230, by replacing the 'pass' with the 'continue', as suggested. However, it does *not* fix the underlying root cause -- for example, changing the lambda input type to `Optional[T]` bypasses the check and triggers the crash again: ``` from typing import Callable, Optional, TypeVar T = TypeVar('T') def foo(f: Callable[[Optional[T]], bool], it: T) -> None: ... foo(reveal_type(lambda x: x in [1, 2] and bool()), 3) ``` I'll update the issue with what I discovered while investigating this, but I don't really have a deep understanding of how our generics/function inference/deferred passes logic works, so didn't feel comfortable volunteering a fix. So, I settled just for fixing the regression.
1 parent 9101707 commit 3c5d0f0

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

mypy/checker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3915,7 +3915,7 @@ def find_isinstance_check_helper(self, node: Expression) -> Tuple[TypeMap, TypeM
39153915

39163916
# We only try and narrow away 'None' for now
39173917
if not is_optional(item_type):
3918-
pass
3918+
continue
39193919

39203920
collection_item_type = get_proper_type(builtin_item_type(collection_type))
39213921
if collection_item_type is None or is_optional(collection_item_type):

test-data/unit/check-optional.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,17 @@ else:
550550
reveal_type(b) # N: Revealed type is 'Union[__main__.A, None]'
551551
[builtins fixtures/ops.pyi]
552552

553+
[case testInferInWithErasedTypes]
554+
from typing import TypeVar, Callable, Optional
555+
556+
T = TypeVar('T')
557+
def foo(f: Callable[[T], bool], it: T) -> None: ...
558+
def bar(f: Callable[[Optional[T]], bool], it: T) -> None: ...
559+
560+
foo(lambda x: x in [1, 2] and bool(), 3)
561+
bar(lambda x: x in [1, 2] and bool(), 3)
562+
[builtins fixtures/list.pyi]
563+
553564
[case testWarnNoReturnWorksWithStrictOptional]
554565
# flags: --warn-no-return
555566
def f() -> None:

0 commit comments

Comments
 (0)