-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Crash in lambda expression as generic argument #8230
Comments
Whoops, I think that's the bug, yeah. I can send a PR with that fix in a minute (unless you're already working on one). |
I didn't start working on a fix yet, you can submit a PR with a quick fix, otherwise I will do this tomorrow. I am wondering if there is a deeper logical problem: |
Yeah, I was just about to suggest this -- after I made the change, I tried adjusting the test case so filter accepts a |
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.
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.
So, I think what's happening is this:
And of course, if we get lucky and hit a code path that doesn't trigger a crash, the subsequent passes eventually end up doing the right thing. I'm not really sure what the correct fix is though. Some tentative ideas I had were:
The discussion on overhauling how mypy infers generics in #5738 is maybe related/could address this? |
This PR fixes the crash reported in #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.
Yes, this is indeed related, but unlike many other related issues, this causes a crash, not a leaked type variable etc. I would therefore prefer to track this separately. I will re-title the issue accordingly. |
To update on this issue: while the original crash was fixed ages ago in #8232, a second crash identified by @Michael0x2a in #8230 (comment) lingered for a while. A full repro for this crash was: from typing import List, TypeVar, Callable, Optional
T = TypeVar('T')
def filter(f: Callable[[Optional[T]], bool], it: List[T]) -> List[T]: ...
xs: List[int]
filter(lambda x: x in [1, 2] and bool(), [3, 4]) This crashed on mypy <=0.930, but doesn't crash on 0.931+. The crash was fixed by commit a0f27b1 (@sobolevn's #11924). Weirdly, while I can reproduce this crash with @JelleZijlstra, @JukkaL -- thoughts? |
Let's just close it. The issue hasn't received attention in two years; if somebody else has a similar problem we'll hear about it. |
This test crashes on current master (and causes troubles internally):
with a traceback that ends in
This may be related to #8148, also note there is a weird
pass
here (it should probably becontinue
).cc @Michael0x2a
The text was updated successfully, but these errors were encountered: