Skip to content

Improve handling of "lambda: None" #1557

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

Merged
merged 1 commit into from
Jun 15, 2016

Conversation

rwbarton
Copy link
Contributor

"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 #1425.

@gvanrossum
Copy link
Member

gvanrossum commented May 20, 2016 via email

@rwbarton
Copy link
Contributor Author

Yeah, let's just hold off on this until the strict optional checking changes land.

@ddfisher
Copy link
Collaborator

That'd be great, thanks!

@JukkaL
Copy link
Collaborator

JukkaL commented Jun 14, 2016

@ddfisher This is ready to review now?

@ddfisher
Copy link
Collaborator

LGTM!

@rwbarton
Copy link
Contributor Author

After rebasing this commit, one of the new strict optional checking tests fails:

[case testLambdaReturningNone]
f = lambda: None
x = f()

This tests the old behavior (which didn't change with strict optional checking) of returning NoneTyp here rather than Void. Should we update that test, or do we want that old behavior?

I don't have a strong opinion here, though I do feel that maintaining this NoneTyp/Void distinction in the type system is not worth it if we're not going to give the user a way to refer to both types... maybe discussion should move back to #1425.

@rwbarton rwbarton force-pushed the rwbarton-lambda-none branch from 38b9ad5 to bc2adc0 Compare June 15, 2016 17:05
@rwbarton
Copy link
Contributor Author

I really don't care much about the behavior here with no context, while the situation with a context arises frequently (see the just-submitted duplicate #1710), so I'm going to leave the no-context behavior as is. Pushed a new version of this commit with a rationalization for that behavior :)

"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.
@rwbarton rwbarton force-pushed the rwbarton-lambda-none branch from bc2adc0 to 136e158 Compare June 15, 2016 17:24
@ddfisher
Copy link
Collaborator

Sounds good to me. I also don't have a strong opinion here, aside from agreeing that we should kill Void.

@ddfisher ddfisher merged commit 741543f into python:master Jun 15, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants