Skip to content

lambda doesn't propagate NoReturn behavior #10520

@gvanrossum

Description

@gvanrossum

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"?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions