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

pointless comparison hint is frequently very off base #12617

Closed
lolbinarycat opened this issue Aug 1, 2024 · 6 comments
Closed

pointless comparison hint is frequently very off base #12617

lolbinarycat opened this issue Aug 1, 2024 · 6 comments
Labels
good first issue Good for newcomers help wanted Contributions especially welcome rule Implementing or modifying a lint rule

Comments

@lolbinarycat
Copy link

code (playground):

def is_some(n):
    n is not None

here, the user is trying to do an implicit return, but python does not have implicit returns. the hint given is this:

B015: Pointless comparison. Did you mean to assign a value? Otherwise, prepend `assert` or remove it.

the chance of someone meaning to write = and instead writing is not is approximately zero. if a pointless comparison appears at the end of a function, it should suggest returning it instead of suggesting assert, and the assignment suggestion should only appear if the operator contains =.

@MichaReiser MichaReiser added rule Implementing or modifying a lint rule help wanted Contributions especially welcome good first issue Good for newcomers labels Aug 1, 2024
@MichaReiser
Copy link
Member

MichaReiser commented Aug 1, 2024

Makes sense to me. We can probably improve the message over all.

@AlexWaygood
Copy link
Member

the chance of someone meaning to write = and instead writing is not is approximately zero.

FWIW, I don't think that's the assumption the rule is making when it says "Did you mean to assign a value?". Instead, I think the rule is assuming that the user meant to write something like

def is_some(n):
    x = n is not None

or

GLOBAL_OBJECT = ...

def is_some(n):
    GLOBAL_OBJECT.x = n is not None

As a general rule, to me it seems more likely that someone made a typo than that they misunderstood the fundamentals of Python semantics (thinking that Python has implicit returns when it does not). But I think you're right that if it's the last line of the function, the more likely typo is that they forgot to use a return statement; it's less likely that they wanted to assign the value to something if it's the last line of a function.

TL;DR: I'd also accept a PR making the error message adaptive here depending on whether it's the last line of a function or not 😄

@lolbinarycat
Copy link
Author

As a general rule, to me it seems more likely that someone made a typo than that they misunderstood the fundamentals of Python semantics (thinking that Python has implicit returns when it does not).

missing the entire left-hand side is a pretty big typo, and python is very frequently used by beginner programmers and programmers who do not usually specialize in python (like me, i'm mostly a rust programmer, but i know just enough python to get by with basic tweaks)

@thejchap
Copy link

thejchap commented Sep 2, 2024

@MichaReiser @AlexWaygood looks like this one is up for grabs - would like to give it a shot (first time contributor here :)

@thejchap
Copy link

thejchap commented Sep 2, 2024

nevermind - missed this

@dhruvmanila
Copy link
Member

Resolved in #12944. Sorry for the churn, @thejchap.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Contributions especially welcome rule Implementing or modifying a lint rule
Projects
None yet
Development

No branches or pull requests

5 participants