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

Return of NoReturn #15142

Closed
danmur97 opened this issue Apr 26, 2023 · 0 comments · Fixed by #17294
Closed

Return of NoReturn #15142

danmur97 opened this issue Apr 26, 2023 · 0 comments · Fixed by #17294
Labels
bug mypy got something wrong

Comments

@danmur97
Copy link

danmur97 commented Apr 26, 2023

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
@danmur97 danmur97 added the bug mypy got something wrong label Apr 26, 2023
hauntsaninja added a commit that referenced this issue May 28, 2024
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.

1 participant