-
Notifications
You must be signed in to change notification settings - Fork 4
Open
Description
This doesn't cause an issue during run-time, but if you call these functions in your code, the code after them is highlighted as UNREACHEABLE in PyCharm.
NoReturn explanation:
Functions that raise exceptions and never return:
from typing import NoReturn
def fatal_error(message: str) -> NoReturn:
"""This function never returns; it always raises an exception."""
raise RuntimeError(message)
fatal_error("Something went wrong!") # 🚨 Raises RuntimeError
Here, fatal_error() always raises an exception and does not return any value.
Infinite loops (never terminating functions):
from typing import NoReturn
def infinite_loop() -> NoReturn:
"""This function never returns because it runs forever."""
while True:
print("Running forever...")
infinite_loop() # 🔄 Runs indefinitely
Since the function runs indefinitely, it never reaches a return statement.
What NoReturn Does Not Mean
It does not mean the function returns None.
If a function returns nothing, it should be annotated as -> None instead.
def do_nothing() -> None:
pass # ✅ This function returns None, so NoReturn is NOT correct.
Metadata
Metadata
Assignees
Labels
No labels