How to Solve the "Unreachable Code" Warning in Python
The unreachable code
warning in Python indicates that a section of your code will never be executed. This can result from misplaced return
or raise
statements, infinite loops, or conditions that are always true.
This guide explores the common causes of this warning and provides solutions to eliminate them, ensuring your code is efficient and error-free.
Understanding the Cause: return
Statements
The most common reason for unreachable code is a return
statement that prematurely exits a function. Code placed after the return
will never be executed:
def example():
a = 123
return 'tutorialreference.com'
b = 456 # Unreachable code
example()
Solution: Move variable declarations and other essential code before the return
statement.
Understanding the Cause: raise
Statements
Similar to return
, a raise
statement that throws an exception will also prevent any subsequent code from running.
def example():
a = 123
raise ValueError('invalid value')
print('This will not execute') # Unreachable code
example()
Solution: Ensure any necessary cleanup or handling code is placed within a try
/except
block before the raise
statement.
Incorrect Indentation
Incorrect indentation can lead to code being mistakenly placed inside a function, loop, or conditional block, rendering it unreachable:
def example():
a = 123
return 'tutorialreference.com'
b = 456 # Incorrect indent - outside the function body
print(b)
example()
Solution: Ensure code is properly indented according to its intended scope. Outdent code to move it out of a function body.
Conditions That Are Always True
Unreachable code can also result from an if
condition that always evaluates to True
.
def example():
a = 123
if 10 > 0:
return 'tutorialreference.com'
b = 456 # Unreachable code
example()
Solution: Review the code for impossible conditions in if
statements, and remove or adjust the logic appropriately.
Resolving the Warning in VS Code (Pylance)
Sometimes, the unreachable code
warning in Visual Studio Code (using the Pylance extension) can be a false positive.
Disabling editor.showUnused
You can disable the specific warning by disabling the editor.showUnused
setting:
- Press
Ctrl + Shift + P
(orCmd + Shift + P
on macOS) to open the Command Palette. - Type
user settings json
. - Click on
Preferences: Open User Settings (JSON)
. - Add the following to your
settings.json
file:
"[python]": {
"editor.showUnused": false
}
Settings in .vscode/settings.json
You can also apply this setting to the current project only by creating a .vscode
folder in the project root and adding a settings.json
file:
// .vscode/settings.json
{
"[python]": {
"editor.showUnused": false
}
}
- With this approach, you will only disable this warning in a specific project, not globally.
Restarting Your IDE
If the warning persists after correcting the code and adjusting settings, try restarting your code editor (Visual Studio Code, PyCharm, etc.). This can sometimes resolve issues with stale warnings.