Skip to content

Bug: Code is unreachable #6028

Closed
Closed
@mfeyx

Description

@mfeyx

Hi there,

Sometimes Pylance states that my "Code is unreachable" even though the code works as expected. For instance, if I use an if-statement without an else. If I add the else statement the code is "reachable" and not greyed out. (Please find the examples below).

In my understanding the additional else is not neccessary and "bad" practice, if you will.

Environment data

python

Python version 3.10.9.final.0 (and others)

vs code

Version: 1.90.1 (system setup)
Commit: 611f9bfce64f25108829dd295f54a6894e87339d
Date: 2024-06-11T21:01:24.262Z
Electron: 29.4.0
ElectronBuildId: 9593362
Chromium: 122.0.6261.156
Node.js: 20.9.0
V8: 12.2.281.27-electron.0
OS: Windows_NT x64 10.0.19045

pylance

v2024.6.1
(Client) Pylance async client (2024.6.1) started with python extension (2024.8.1)
Pylance language server 2024.6.1 (pyright version 1.1.364, commit 0618acc5) starting

Published
2020-06-30, 22:05:55
Last released
2024-06-14, 02:25:45
Last updated
2024-06-07, 09:08:27
Identifier
ms-python.vscode-pylance

Code Snippet

"Code is unreachable"

image

import json


def load_json(obj: str) -> list | dict:
    if isinstance(obj, str):
        obj = json.loads(obj)
        return load_json(obj)
    return obj

Code is reachable

image

import json


def load_json(obj: str) -> list | dict:
    if isinstance(obj, str):
        obj = json.loads(obj)
        return load_json(obj)
    else:
        return obj

Repro Steps

  • create a new python file
  • paste the code

Metadata

Metadata

Assignees

Labels

fixed in next version (pyright)A fix has been implemented and will appear in an upcoming version

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions