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

Fix #5500: Fix false-positive used-before-assignment for assignments in except blocks following try blocks that return #5506

Merged
merged 5 commits into from
Dec 13, 2021

Conversation

jacobtylerwalls
Copy link
Member

Type of Changes

Type
🐛 Bug fix

Description

Fixes false positive for used-before-assignment when evaluating statements after except handlers where the corresponding try returned. In this scenario, we should assume one of the except handlers did execute.

There is the potential for a false-negative where only ONE of the except handlers defines the variable in question, but to check that, we would need to permit scenarios where the other except handlers merely raise or return--that case shouldn't emit a warning. I think the feasibility of that should be evaluated separately.

Closes #5500

@coveralls
Copy link

coveralls commented Dec 11, 2021

Pull Request Test Coverage Report for Build 1573137423

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 93.654%

Totals Coverage Status
Change from base Build 1573037236: 0.0%
Covered Lines: 14183
Relevant Lines: 15144

💛 - Coveralls

@Pierre-Sassoulas Pierre-Sassoulas added the False Positive 🦟 A message is emitted but nothing is wrong with the code label Dec 11, 2021
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.13.0 milestone Dec 11, 2021
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.

LGTM, I think it's a clever fix to bring better control flow.

@cdce8p cdce8p self-requested a review December 11, 2021 23:40
@DanielNoord
Copy link
Collaborator

Fix itself seems fine, but this should be documented in the description of the used-before-assignment message. I'm not sure how to do this properly, especially since the original issue is also discussing another exception to the general "assume tries don't execute" rule. I think whatever we end up assuming should be clearly documented for end-users without the need of looking at our actual code or previous changelogs.

@jacobtylerwalls
Copy link
Member Author

Sorry for the extra force push, I pushed a comment to the wrong branch and removed it when I discovered it to avoid cluttering the branch history.

Copy link
Member

@cdce8p cdce8p left a comment

Choose a reason for hiding this comment

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

The change looks good and indeed fixes the false-positives.

One thing I noticed though is that the filtered_nodes list comprehension has gotten rather complicated with multiple nested ifs and comprehensions itself. This should probably be a function instead.

That would also allow n.statement() to be cached witch would improve performance at least a tiny bit.

Could you create a followup PR for it?

@jacobtylerwalls
Copy link
Member Author

Could you create a followup PR for it?

Sure thing--I agree, the comprehension is quite ugly now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

Regression used-before-assignment with try-except
5 participants