Skip to content

Return of NoReturn #15142

Closed
Closed
@danmur97

Description

@danmur97

Bug Report

When a function receives as argument another function that does not return, then the error Return statement in function which does not return can raise or not depending on the argument construction:

  • if is lambda raises error
  • if is defined using def error does not raise

To Reproduce

from typing import Callable, NoReturn

def _raise(err: Exception) -> NoReturn:
    raise err

def _foo(f: Callable[[], NoReturn]) -> NoReturn:
    f()

def _raise_2() -> NoReturn:
    raise Exception("foo")

value: bool = True # dummy var to enable to put the two cases
if value:
    y: NoReturn = _foo(_raise_2) # mypy does not complain
else:
    x: NoReturn = _foo(lambda: _raise(Exception("foo"))) # mypy complains

Expected Behavior

  • mypy should not complain about non-existent return statements in the lambda definition (case of variable x) as the lambda definition is equivalent to the def _raise_2 definition.
  • even if return f() is used in _foo this should not trigger an error when the type being returned is also NoReturn

Actual Behavior

  • mypy sees an implicit return statement in lambdas definition
  • return of a NoReturn is not allowed

Your Environment
error: Return statement in function which does not return [misc]

  • Mypy version used: 1.0.1
  • Mypy command-line flags: --show-error-codes
  • Mypy configuration options from mypy.ini (and other config files):
[mypy]
warn_no_return = True
  • Python version used: 3.11

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