Skip to content

Commit

Permalink
Fix regression in container check logic
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Michael0x2a committed Jan 2, 2020
1 parent 9101707 commit 3c5d0f0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mypy/checker.py
Expand Up @@ -3915,7 +3915,7 @@ def find_isinstance_check_helper(self, node: Expression) -> Tuple[TypeMap, TypeM

# We only try and narrow away 'None' for now
if not is_optional(item_type):
pass
continue

collection_item_type = get_proper_type(builtin_item_type(collection_type))
if collection_item_type is None or is_optional(collection_item_type):
Expand Down
11 changes: 11 additions & 0 deletions test-data/unit/check-optional.test
Expand Up @@ -550,6 +550,17 @@ else:
reveal_type(b) # N: Revealed type is 'Union[__main__.A, None]'
[builtins fixtures/ops.pyi]

[case testInferInWithErasedTypes]
from typing import TypeVar, Callable, Optional

T = TypeVar('T')
def foo(f: Callable[[T], bool], it: T) -> None: ...
def bar(f: Callable[[Optional[T]], bool], it: T) -> None: ...

foo(lambda x: x in [1, 2] and bool(), 3)
bar(lambda x: x in [1, 2] and bool(), 3)
[builtins fixtures/list.pyi]

[case testWarnNoReturnWorksWithStrictOptional]
# flags: --warn-no-return
def f() -> None:
Expand Down

0 comments on commit 3c5d0f0

Please sign in to comment.