-
-
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
Narrowed down types sometimes not propagated to lambda #4297
Labels
bug
mypy got something wrong
priority-1-normal
topic-strict-optional
topic-type-narrowing
Conditional type narrowing / binder
Comments
JukkaL
added
bug
mypy got something wrong
priority-1-normal
topic-strict-optional
labels
Nov 30, 2017
Workaround: def g(b: Optional[str]) -> None:
if b:
_b = b # https://github.com/python/mypy/issues/4297
f1(lambda: _b.upper()) |
This also seems to happen with native collection types whose size is "runtime" (i.e. recursive types in lambda-calculus terms), but not tuples (whose size is "fixed"). Error / non-error cases added for reproduction, based on the original post: from typing import Optional, Callable
def f1(key: Callable[[], str]) -> None: ...
def f2(key: object) -> None: ...
def g(b: Optional[str]) -> None:
if b is not None:
f1(lambda: b.upper()) # Item "None" of "Optional[str]" has no attribute "upper"
z: Callable[[], str] = lambda: b.upper() # Item "None" of "Optional[str]" has no attribute "upper"
d = { "foo": lambda: b.upper() } # Item "None" of "Optional[str]" has no attribute "upper"
l = [ lambda: b.upper() ] # Item "None" of "Optional[str]" has no attribute "upper"
t = ( lambda: b.upper(), ) # No error
f2(lambda: b.upper()) # No error
lambda: b.upper() # No error |
This was referenced Nov 1, 2023
ilevkivskyi
added a commit
that referenced
this issue
Nov 4, 2023
JukkaL
pushed a commit
that referenced
this issue
Nov 10, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
bug
mypy got something wrong
priority-1-normal
topic-strict-optional
topic-type-narrowing
Conditional type narrowing / binder
The errors generated for the following code are invalid (when using
--strict-optional
):My hypothesis is that callable type context for lambda somehow affects how type narrowing behaves.
This was reported by @tueda on Gitter.
The text was updated successfully, but these errors were encountered: