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

ReportUnnecessaryComparison False Positive #5900

Closed
birgersp opened this issue May 19, 2024 · 1 comment
Closed

ReportUnnecessaryComparison False Positive #5900

birgersp opened this issue May 19, 2024 · 1 comment
Assignees

Comments

@birgersp
Copy link

birgersp commented May 19, 2024

Environment data

  • Language Server version: 2024.5.1
  • OS and version: Pop OS (Ubuntu 22.04)
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.10.12

Problem

Pylance reports that the condition process.stdout is None will always evaluate to False since the types "bytes" and "None" have no overlap. This is not true; process.stdout can actually be None in some cases.

Code Snippet

def foobar() -> str:
    process = subprocess.run("echo", shell=True)
    if process.stdout is None:  # <--- yields a Pylance warning!
        return "no"
    else:
        return "yes"

print(foobar())

Repro Steps

  1. XXX

Expected behavior

No warning should occur on line 3

Actual behavior

A warning occurs

@github-actions github-actions bot added the needs repro Issue has not been reproduced yet label May 19, 2024
@erictraut
Copy link
Contributor

It looks like you've enabled the reportUnnecessaryComparison check. This check is off by default in "basic" and "standard" type checking mode. That means you've either enabled "strict" type checking mode or manually enabled this specific check. If you do not want to see diagnostics for this check, you can leave it disabled.

When this check is enabled, pyright (the type checker that pylance is built upon) reports cases where a conditional expression always evaluates to True or False. In this case, the expression process.stdout is None always evaluates to False based on type analysis. That's because the call to subprocess.run returns a value of type CompletedProcess[bytes], and the type of process.stdout evaluates to type bytes. That means it can never be None.

This type analysis is performed based on type information provided in the type stubs provided by the typeshed project. If you think that there is an error in these typeshed stubs, please report the error to the maintainers of typeshed.

In any case, pyright is working as intended here. The reportUnnecessaryComparison check is correctly identifying and reporting a conditional check in your code that (according to type analysis) will always evaluate to False, which may indicate that you have a bug in your code.

@debonte debonte added by design and removed needs repro Issue has not been reproduced yet labels May 19, 2024
@debonte debonte closed this as completed May 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants