-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Mypy complains about lambda that returns None #1425
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
Comments
Possibly this would also be fixed once we introduce strict Optional checking. |
Maybe -- we'd likely still want to keep no return value from a function and a regular |
Would be fixed by #1278, which will likely happen as part of strict Optional checking. I actually think we will not want to have no return value distinct from regular |
But PEP 8 states:
It then shows two "No" examples. I would like mypy to catch those:
even though they would be annotated with |
For For |
Both foo() and bar() have Optional[float] as type. Both have a style error; I don't consider falling of the end different from a return without value in the middle. I think it's reasonable if mypy enforces things that could be considered style issues, and I don't think we always need to provide a way to turn them off. (Taken to the extreme, being consistent in the return type is a style issue too.) |
Hmm. I agree that both of those are style errors. I think the former is slightly more likely to be accidental than the latter, but I think I was wrong to propose that they be treated differently. I also think that it's reasonable for mypy to enforce style issues, especially when they may have correctness implications (e.g. forgetting to actually write a return statement is pretty common in my experience). I'm not sure we always need to provide a way to turn them off, but I think we should consider it on a case-by-case basis, because they have the potential to be unnecessary roadblocks to people using mypy. I actually do think there's a reasonable line we can draw between style and correctness issues -- it can be fuzzy at times, but by and large I think it's a distinction worth making. |
"lambda: None" might be used either where a function returning an Optional[T] is expected, or where a function not returning a value is expected. Previously it would always treated as the former; now the context is used to decide. When no context is available, the lambda is now treated as a function not returning a value. This seems more consistent with the fact that you can't define a variable 'a = None' without a type annotation or declare a function to return NoneTyp. (If really desperate for the other meaning of "lambda: None", you can write a generic function.) Fixes python#1425.
"lambda: None" might be used either where a function returning an Optional[T] is expected, or where a function not returning a value is expected. Previously it would always treated as the former; now the context is used to decide. When no context is available, mypy continues to treat the lambda as returning the value None, rather than no value at all. When the other meaning is desired, it is generally possible to provide a context, or in any case to use a function defined with `def`. Fixes python#1425.
"lambda: None" might be used either where a function returning an Optional[T] is expected, or where a function not returning a value is expected. Previously it would always treated as the former; now the context is used to decide. When no context is available, mypy continues to treat the lambda as returning the value None, rather than no value at all. When the other meaning is desired, it is generally possible to provide a context, or in any case to use a function defined with `def`. Fixes python#1425.
"lambda: None" might be used either where a function returning an Optional[T] is expected, or where a function not returning a value is expected. Previously it would always treated as the former; now the context is used to decide. When no context is available, mypy continues to treat the lambda as returning the value None, rather than no value at all. When the other meaning is desired, it is generally possible to provide a context, or in any case to use a function defined with `def`. Fixes #1425.
waiting on python/mypy#1425
waiting on python/mypy#1425
Mypy gives a bogus error when giving a lambda that returns
None
when a callable returningNone
is expected:Likely this is due to
Void
vs.NoneTyp
, and we'd have to replace an inferredNoneTyp
of a lambda expression withVoid
.The text was updated successfully, but these errors were encountered: