Skip to content

Commit a1136d9

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 -- I don't think I actually understand the relevant pieces of code enough to feel confident volunteering a fix. So, I settled for just fixing the regression. Basically, it seems this bug is due to how we try inferring the type of the lambda in multiple passes to resolve the types. We pencil in an ErasedType during the first pass -- and then subsequently crash when attempting to type check the body during that pass. I'll leave more details about this in the linked issue.
1 parent 9101707 commit a1136d9

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

mypy/checker.py

+1-1
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

+11
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)