Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Code is unreachable #6028

Closed
mfeyx opened this issue Jun 18, 2024 · 6 comments
Closed

Bug: Code is unreachable #6028

mfeyx opened this issue Jun 18, 2024 · 6 comments
Assignees
Labels
fixed in next version (pyright) A fix has been implemented and will appear in an upcoming version

Comments

@mfeyx
Copy link

mfeyx commented Jun 18, 2024

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
@github-actions github-actions bot added the needs repro Issue has not been reproduced yet label Jun 18, 2024
@erictraut
Copy link
Contributor

Reachability checks are based on type analysis. The obj parameter is declared to be str. If we assume that this type declaration is not violated, the conditional expression isinstance(obj, str) will always be true.

Based on the behavior of the code, it looks like the function also accepts values of type list and dict. If my assumption is correct, then you should adjust the type annotation for the obj parameter to be obj: str | list | dict. This will eliminate the apparently-unreachable code.

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

    return obj

@mfeyx
Copy link
Author

mfeyx commented Jun 18, 2024

@erictraut in my particular case I have a stringified json object that can have multiple levels of "stringification" and will, eventually, return a list or a dict. so it is a string until it's not that is why my load_json helper function only accepts a str input and should return a list or dict. So it's kind of tricky and maybe I just get rid of the input type.

Anyways, your solution works as well, thanks!

@bschnurr
Copy link
Member

@mfeyx see this issue. might help with typing. python/typing#182 (comment)

erictraut added a commit to microsoft/pyright that referenced this issue Jun 20, 2024
…e was not supported for `if` or `else` suites when the condition type was narrowed to `Never`. This addresses microsoft/pylance-release#6028.
erictraut added a commit to microsoft/pyright that referenced this issue Jun 20, 2024
…e was not supported for `if` or `else` suites when the condition type was narrowed to `Never`. This addresses microsoft/pylance-release#6028.
erictraut added a commit to microsoft/pyright that referenced this issue Jun 20, 2024
…e was not supported for `if` or `else` suites when the condition type was narrowed to `Never`. This addresses microsoft/pylance-release#6028.
@erictraut
Copy link
Contributor

@mfeyx, I took a closer look at this issue because of the inconsistency you pointed out in your bug report between the code that uses an else and the code that does not. These should be equivalent, so you should see "unreachable code" in both cases. This inconsistency was due to a bug in the reachability code in pyright. It will be fixed in the next release of pyright and in a future release of pylance.

erictraut added a commit to microsoft/pyright that referenced this issue Jun 20, 2024
…e was not supported for `if` or `else` suites when the condition type was narrowed to `Never`. This addresses microsoft/pylance-release#6028. (#8190)
@StellaHuang95
Copy link
Contributor

Thanks Eric. I will keep this issue open until Pylance releases the fix.

@StellaHuang95 StellaHuang95 reopened this Jun 20, 2024
@StellaHuang95 StellaHuang95 added fixed in next version (pyright) A fix has been implemented and will appear in an upcoming version and removed needs repro Issue has not been reproduced yet labels Jun 20, 2024
@rchiodo rchiodo closed this as completed Jun 20, 2024
@rchiodo rchiodo reopened this Jun 20, 2024
@rchiodo
Copy link
Contributor

rchiodo commented Jun 25, 2024

This issue has been fixed in prerelease version 2024.6.102, which we've just released. You can find the changelog here: CHANGELOG.md

@rchiodo rchiodo closed this as completed Jun 25, 2024
DetachHead added a commit to DetachHead/basedpyright that referenced this issue Jul 5, 2024
…able code was not supported for `if` or `else` suites when the condition type was narrowed to `Never`. This addresses microsoft/pylance-release#6028. (#8190)"

This reverts commit b841e11.
DetachHead added a commit to DetachHead/basedpyright that referenced this issue Jul 6, 2024
…able code was not supported for `if` or `else` suites when the condition type was narrowed to `Never`. This addresses microsoft/pylance-release#6028. (#8190)"

This reverts commit b841e11.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed in next version (pyright) A fix has been implemented and will appear in an upcoming version
Projects
None yet
Development

No branches or pull requests

5 participants