Code is unreachable warning in Python [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
4 min

banner

# Code is unreachable warning in Python [Solved]

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.

main.py
def example(): a = 123 return 'bobbyhadz.com' # ⛔️ Code is unreachable Pylance # Unreachable code pylint(unreachable) b = 456 example()

unreachable code pylint

Notice that the b variable is declared after the function has returned, therefore the line of code is unreachable - it will never run.

The 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.

# Correct your indentation

If you meant to declare the variable outside the function, remove the indentation.

main.py
# ✅ works as expected def example(): a = 123 return 'bobbyhadz.com' b = 456 # 👈️ Declared outside function print(b) example()
The code for this article is available on GitHub

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.

# Move the variable declaration above the return statement

You can also move the block of code or variable declaration above the return statement to resolve the issue.

main.py
def example(): a = 123 b = 456 # 👈️ Moved above return statement return 'bobbyhadz.com' example()
The code for this article is available on GitHub

The return statement is not followed by any unreachable code, so the issue is resolved.

# Having a condition that always returns True

The warning is also caused if you have a block of code that follows a condition that always runs.

main.py
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.

# Raising an extension within a function

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.

main.py
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.

main.py
def example(): a = 123 raise ValueError('invalid value') try: example() except ValueError: # 👇️ This runs print('A ValueError occurred...')
The code for this article is available on GitHub

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.

main.py
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.

# Resolving the issue in Visual Studio Code

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.

  1. Press Ctrl + Shift + P (or Command + Shift + P on macOS).
Note: you can also press F1 to open the Command Palette.
  1. Type user settings json.

  2. Click on Preferences: Open User Settings (JSON)

preferences open user settings

  1. Add the following lines of code to your settings.json file.
settings.json
"[python]": { "editor.showUnused": false }

disable show unused setting

You can also add the setting to your .vscode/settings.json file if you only want to apply it to the current project.

  1. In the root directory of your project, create a .vscode folder.

  2. Create a settings.json file in the .vscode folder.

  3. Add the following code to your settings.json file.

.vscode/settings.json
{ "[python]": { "editor.showUnused": false } }

disable show unused in vscode settings-json

When a setting is set in your .vscode/settings.json file, it only applies to the current project and overrides any global configuration.

# Try to restart your IDE

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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.