Skip to content
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

lambda doesn't propagate NoReturn behavior #10520

Closed
gvanrossum opened this issue May 21, 2021 · 1 comment · Fixed by #17294
Closed

lambda doesn't propagate NoReturn behavior #10520

gvanrossum opened this issue May 21, 2021 · 1 comment · Fixed by #17294
Labels
bug mypy got something wrong

Comments

@gvanrossum
Copy link
Member

I have code like this:

class Possibility(Generic[T]):
    def __init__(self, produce: Callable[[], T]):
        self._produce = produce
    def produce(self) -> T:
        return self._produce()
def reject() -> NoReturn:
    raise RuntimeError
def nothing() -> Possibility[NoReturn]:
    return Possibility(lambda: reject())  # Error here

On the last line I get an error, "Return statement in function which does not return".

This is about the lambda, not about the explicit return statement! If I write return Possibility(reject) it works. (But in the real code, the lambda takes a parameter and reject is a method of that parameter, so I can't do that. My workaround is to turn the lambda into a function, but that's verbose.)

If you can't fix things so that the lambda inherits the "never returns" bit, then how about making the error clearer, e.g. "Lambda never returns"?

@gvanrossum gvanrossum added the bug mypy got something wrong label May 21, 2021
@gvanrossum gvanrossum changed the title lambda doesn't propagate NoReject behavior lambda doesn't propagate NoReturn behavior May 21, 2021
@Dreamsorcerer
Copy link
Contributor

I think this is a duplicate of #9590, information about a lambda seems to get lost when used inline (not assigned to a variable).

The code works if you change it to:

a = lambda: reject()
return Possibility(a)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants