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

Implement basic control flow checks for consider-using-with #4929

Merged

Conversation

DudeNr33
Copy link
Collaborator

@DudeNr33 DudeNr33 commented Aug 28, 2021

  • Add a ChangeLog entry describing what your PR does.
  • If it's a new feature, or an important bug fix, add a What's New entry in
    doc/whatsnew/<current release.rst>.
  • Write a good description on what the PR does.

Type of Changes

Type
🐛 Bug fix

Description

The checker keeps track of context managers that have been assigned to a variable to see if they are later on used in a with block.
If a variable is reassigned before it is used in a with, the message will trigger (as intended).
However, the logic to check that was pretty basic, and did not handle cases where e.g. the variable is assigned inside two separate branches of control flow, like in if-else statements or try-except constructs.

This MR solves this. As the logic to determine if two nodes are in different branches of control flow might be useful in other places as well, I put the method inside the pylint.checkers.utils module.

Closes #4751

@coveralls
Copy link

coveralls commented Aug 28, 2021

Pull Request Test Coverage Report for Build 1179761563

  • 4 of 4 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.002%) to 92.733%

Totals Coverage Status
Change from base Build 1177664240: 0.002%
Covered Lines: 13462
Relevant Lines: 14517

💛 - Coveralls

@DudeNr33 DudeNr33 marked this pull request as ready for review August 28, 2021 19:46
@DudeNr33
Copy link
Collaborator Author

Oh wow, I just noticed that astroid already has a very similar method are_exclusive.
It covers not exactly the same cases (especially as it is focused on statements instead of notes, therefore it does not handle IfExp), but duplicating the logic inside pylint is of course nonsense.
I will refactor.

@DudeNr33 DudeNr33 marked this pull request as draft August 29, 2021 13:20
@DudeNr33 DudeNr33 marked this pull request as ready for review August 29, 2021 17:45
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.

Thank you for recreating an astroid function then refactoring everything to use astroid :D The final result is pretty clean.

@Pierre-Sassoulas Pierre-Sassoulas added Bug 🪲 False Positive 🦟 A message is emitted but nothing is wrong with the code labels Aug 30, 2021
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.11.0 milestone Aug 30, 2021
@Pierre-Sassoulas Pierre-Sassoulas merged commit 7d440dd into pylint-dev:main Aug 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 False Positive 🦟 A message is emitted but nothing is wrong with the code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

False positive consider-using-with in if/else block
3 participants