Last updated: Apr 10, 2024
Reading time·4 min
The Python warning that code is unreachable occurs when you return a value from a function and try to run more code after the function has returned.
To resolve the issue, move your return statement to the bottom of the function and make sure your code is indented properly.
Here is an example of how the warning is caused.
def example(): a = 123 return 'bobbyhadz.com' # ⛔️ Code is unreachable Pylance # Unreachable code pylint(unreachable) b = 456 example()
Notice that the b
variable is declared after the function has returned,
therefore the line of code is unreachable - it will never run.
return
statement returns a value from the function and stops its execution to return control to the context of the caller (the global scope in the example).In other words, once you return from a function, none of the remaining code in the function runs.
If you meant to declare the variable outside the function, remove the indentation.
# ✅ works as expected def example(): a = 123 return 'bobbyhadz.com' b = 456 # 👈️ Declared outside function print(b) example()
The variable is now declared outside the function because the indentation is removed, so the warning is no longer raised.
The indented block of code is considered a part of the function.
If you dedent the code, it is a part of the global scope and runs without any issues.
You can also move the block of code or variable declaration above the return statement to resolve the issue.
def example(): a = 123 b = 456 # 👈️ Moved above return statement return 'bobbyhadz.com' example()
The return statement is not followed by any unreachable code, so the issue is resolved.
The warning is also caused if you have a block of code that follows a condition that always runs.
def example(): a = 123 if 10 > 0: return 'bobbyhadz.com' # ⛔️ Code is unreachable Pylance # Unreachable code pylint(unreachable) b = 456 example()
The if
block is always run because the condition is always met.
The if
block returns a value, so the declaration of the b
variable is
unreachable.
To resolve the issue, make sure you don't have conditions that always return
True
and are followed by other blocks of code.
The warning also occurs if you raise an exception in a function.
Raising an exception stops the execution of the function, so no code after the
raise
statement runs.
def example(): a = 123 raise ValueError('invalid value') # code here is also unreachable example() # ⛔️ Code is unreachable Pylance # Unreachable code pylint(unreachable) print('This never runs') # 👈️ this line is also unreachable
The raise ValueError
line raises an exception and stops the program.
When an exception is raised, the Python interpreter exits, so no additional code is run.
If you need to handle the exception, use a try/except statement.
def example(): a = 123 raise ValueError('invalid value') try: example() except ValueError: # 👇️ This runs print('A ValueError occurred...')
We call the function in the try
block, it raises an error that is then handled
by the except
block.
If you don't know the type of exception that might occur, use the generic
Exception
class.
def example(): a = 123 raise ValueError('invalid value') try: example() except Exception as e: print('A ValueError occurred...', e)
The Exception
class is the base class for all non-exit exceptions.
The warning is often caused mistakenly when using Pylance with Visual Studio Code.
If the issue is not in your code, try to solve it by disabling the
editor.showUnused
setting.
Ctrl
+ Shift
+ P
(or Command
+ Shift
+ P
on macOS).F1
to open the Command Palette.Type user settings json.
Click on Preferences: Open User Settings (JSON)
settings.json
file."[python]": { "editor.showUnused": false }
You can also add the setting to your .vscode/settings.json
file if you only
want to apply it to the current project.
In the root directory of your project, create a .vscode
folder.
Create a settings.json
file in the .vscode
folder.
Add the following code to your settings.json
file.
{ "[python]": { "editor.showUnused": false } }
When a setting is set in your .vscode/settings.json
file, it only applies to
the current project and overrides any global configuration.
If you still see the warning, try to restart your code editor.
PyCharm and Visual Studio Code often have issues with reporting stale, non-existent warnings long after they've been fixed.
You can learn more about the related topics by checking out the following tutorials:
You can learn more about the related topics by checking out the following tutorials: