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

False positive: reportUnnecessaryComparison #5339

Closed
Questionsdenombres opened this issue Jun 18, 2023 · 3 comments
Closed

False positive: reportUnnecessaryComparison #5339

Questionsdenombres opened this issue Jun 18, 2023 · 3 comments
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@Questionsdenombres
Copy link

Questionsdenombres commented Jun 18, 2023

Environment data

  • Windows 11
  • VScode 1.79.2
  • Python 3.11.4 (tags/v3.11.4:d2340ef, Jun 7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)] on win32
  • Pylance v2023.6.20 (mode strict)

Code Snippet

N: int = 10
tags: list[bool] = [False for _ in range(N)]
adjacents: list[list[int]] = [[] for _ in range(N)]

def walk(start_node: int):
    step: int = 1
    node: int = start_node
    while True:
        for node in adjacents[node]:
            if (not tags[node]) or ((node == start_node) and (step == 2)): # here is the report
                tags[node] = True
                step += 1
                break
        else:
            return

Actual behavior

  • A report is displayed under the code: step == 2
  • reportUnnecessaryComparison: Condition will always evaluate to False since the types "Literal[1]" and "Literal[2]" have no overlap

Expected behavior

  • no report.
  • It seems that pylance understands that the `step' variable will always evaluates Literal[1].
  • The code meaning is irrelevant: just look at the step variable. The condition is part of an or and the variable is incremented two lines later within a loop.
@erictraut
Copy link
Collaborator

Thanks for the bug report.

@StellaHuang95, please transfer this to the pyright project since it's a core type checking bug.

@erictraut
Copy link
Collaborator

This will be fixed in the next insiders release of pylance.

@erictraut erictraut added bug Something isn't working addressed in next version Issue is fixed and will appear in next published version labels Jun 19, 2023
@erictraut
Copy link
Collaborator

This is included in pyright 1.1.315, which I just published. It will also be included in this week's insiders release of pylance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants