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

Add new checker bad-chained-comparison #7990

Merged
merged 21 commits into from
Feb 26, 2023
Merged

Add new checker bad-chained-comparison #7990

merged 21 commits into from
Feb 26, 2023

Conversation

zenlyj
Copy link
Contributor

@zenlyj zenlyj commented Dec 27, 2022

Type of Changes

Type
πŸ› Bug fix
βœ“ ✨ New feature
πŸ”¨ Refactoring
βœ“ πŸ“œ Docs

Description

As suggested by the issue, a default checker is added to warn users about potential logical mistakes from a chained comparison consisting of comparisons that belong to different semantic categories/groups. Please take a look at the referenced issue as this implementation is pretty much a 1-1 of the solution suggested there.

Any advice is greatly appreciated!

Closes #6559

@github-actions

This comment has been minimized.

@Pierre-Sassoulas
Copy link
Member

The new message in primer seems genuine ! Amazing !

@Pierre-Sassoulas Pierre-Sassoulas added the Enhancement ✨ Improvement to a component label Dec 27, 2022
Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks pretty good already, thank you !

pylint/extensions/bad_chained_comparison.py Outdated Show resolved Hide resolved
pylint/extensions/bad_chained_comparison.py Outdated Show resolved Hide resolved
@nickdrozd
Copy link
Contributor

I think it would be reasonable to enable this by default and not make it an extension. The one primer result appears to be a full-on error and my guess is that most hits would be also be errors.

I know we try to avoid false positives, but on the other hand, surfacing latent errors helps to highlight the value of the tool.

@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.16.0 milestone Dec 27, 2022
Pierre-Sassoulas added a commit to Pierre-Sassoulas/sentry that referenced this pull request Dec 27, 2022
This seems to be a genuine error detected by the new pylint check in pylint-dev/pylint#7990.
@zenlyj
Copy link
Contributor Author

zenlyj commented Dec 28, 2022

I think it would be reasonable to enable this by default and not make it an extension. The one primer result appears to be a full-on error and my guess is that most hits would be also be errors.

I know we try to avoid false positives, but on the other hand, surfacing latent errors helps to highlight the value of the tool.

I agree, I feel that most hits would most likely be errors. I don't think there are many scenarios that warrant such confusing chained comparisons. But ultimately, it depends on how tolerant we are towards false positives.

Any thoughts on this? @Pierre-Sassoulas

@codecov
Copy link

codecov bot commented Dec 28, 2022

Codecov Report

Merging #7990 (05ae84a) into main (259b3d7) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #7990   +/-   ##
=======================================
  Coverage   95.42%   95.43%           
=======================================
  Files         176      177    +1     
  Lines       18521    18545   +24     
=======================================
+ Hits        17674    17698   +24     
  Misses        847      847           
Impacted Files Coverage Ξ”
pylint/checkers/bad_chained_comparison.py 100.00% <100.00%> (ΓΈ)

@github-actions

This comment has been minimized.

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @nickdrozd, as evidenced by the primer result this is a great check. It's not that often that we discover a real world bug in the primers with a 0% false positive rate from the start. In fact it never happened before. If we want to make this a default check we just have to move it to pylint/checker, please keep the code independent :)

I suggested some wording change and a shorter message with more details, let me know what you think. (I did not review the logic again)

doc/whatsnew/fragments/6559.new_check Outdated Show resolved Hide resolved
pylint/extensions/bad_chained_comparison.py Outdated Show resolved Hide resolved
pylint/extensions/bad_chained_comparison.py Outdated Show resolved Hide resolved
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

pylint/checkers/bad_chained_comparison.py Outdated Show resolved Hide resolved
pylint/checkers/bad_chained_comparison.py Outdated Show resolved Hide resolved
pylint/checkers/bad_chained_comparison.py Outdated Show resolved Hide resolved
@github-actions

This comment has been minimized.

@Pierre-Sassoulas Pierre-Sassoulas added the Needs review πŸ” Needs to be reviewed by one or multiple more persons label Dec 30, 2022
@Pierre-Sassoulas
Copy link
Member

I think it's a pretty nice checker, great job @zenlyj. Let's wait for another maintainer review, this is a default check after all, mistakes would affect a lot of users.

@zenlyj zenlyj changed the title Add extension for chained comparison warning Add default check for chained comparison warning Dec 30, 2022
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@Pierre-Sassoulas
Copy link
Member

I have a really hard time to understand the pre-commit error. I can reproduce locally but I can't seem to fix it.

@zenlyj
Copy link
Contributor Author

zenlyj commented Jan 1, 2023

@Pierre-Sassoulas it seems that the pre-commit error is due to the files having Windows style line breaks. It should be fixed now, sorry for the trouble!

Regarding the documentation check fail, is this expected for new documentation pages?

@github-actions

This comment has been minimized.

@zenlyj zenlyj changed the title Add default check for chained comparison warning Add default check to warn about bad chained comparisons Jan 1, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Jan 2, 2023

πŸ€– Effect of this PR on checked open source code: πŸ€–

Effect on sentry:
The following messages are now emitted:

  1. bad-chained-comparison:
    Suspicious 2-part chained comparison using semantically incompatible operators ('in' and 'is not')
    https://github.com/getsentry/sentry/blob/6d6c0bd9b8125d554f966da1bb0b70fcab601c27/src/sentry/api/endpoints/project_details.py#L550

This comment was generated for commit 05ae84a

@Pierre-Sassoulas
Copy link
Member

The documentation check is expected to fail as long as it's not merged in main and in the official doc.

@Pierre-Sassoulas Pierre-Sassoulas modified the milestones: 2.16.0, 2.17.0 Jan 8, 2023
@zenlyj zenlyj changed the title Add default check to warn about bad chained comparisons Add new checker bad-chained-comparison Feb 4, 2023
Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First check that not only did not have a single primer false positives but even detected a real bug to fix in a very popular well scrutinized project. It already detected and permitted to fix a bug in sentry, which is pretty impressive ! Great checker, amazing job πŸ‘Œ

@Pierre-Sassoulas Pierre-Sassoulas merged commit 52a2a04 into pylint-dev:main Feb 26, 2023
@Pierre-Sassoulas Pierre-Sassoulas removed the Needs review πŸ” Needs to be reviewed by one or multiple more persons label Feb 26, 2023
@zenlyj zenlyj deleted the chained-comparisons branch March 1, 2023 00:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement ✨ Improvement to a component
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Warn about if x is None == y is None
4 participants