-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix used-before-assignment
false positive for TYPE_CHECKING if/elif/else usage
#8071
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 used-before-assignment
false positive for TYPE_CHECKING if/elif/else usage
#8071
Conversation
β¦itive_type_checking
for more information, see https://pre-commit.ci
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #8071 +/- ##
=======================================
Coverage 95.53% 95.54%
=======================================
Files 177 177
Lines 18622 18633 +11
=======================================
+ Hits 17791 17802 +11
Misses 831 831
|
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
This still misses one false positive that I think is in scope. Perhaps you would find _exhaustively_define_name_raise_or_return
or _defines_name_raises_or_returns_recursive
useful to handle this? If so, we can (in a separate commit) promote it out of the NamesConsumer into utils.py. (EDIT: no, I wouldn't move it, it's too many related methods and should be discussed separately.)
diff --git a/tests/functional/u/used/used_before_assignment_typing.py b/tests/functional/u/used/used_before_assignment_typing.py
index 6e1f87131..94d28830b 100644
--- a/tests/functional/u/used/used_before_assignment_typing.py
+++ b/tests/functional/u/used/used_before_assignment_typing.py
@@ -11,7 +11,10 @@ if TYPE_CHECKING:
import calendar
from urllib.request import urlopen
elif input():
- import calendar
+ if input() + 1:
+ import calendar
+ else:
+ import calendar
else:
from urllib.request import urlopen
Gives:
E AssertionError: Wrong results for file "used_before_assignment_typing":
E
E Unexpected in testdata:
E 124: used-before-assignment
β¦itive_type_checking
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
@jacobtylerwalls Thanks for the review! I looked into the usage I took reference from those 2 and implemented a new function in Please let me know if I missed out on anything here! |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
used-before-assignment
used-before-assignment
false positive for TYPE_CHECKING if/elif/else usage
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is amazing! Such a useful generalized solution (vastly outstripping the underlying issue in importance!) π
I left some cosmetic feedback, but this looks good to go.
β¦itive_type_checking
π€ According to the primer, this change has no effect on the checked open source code. π€π This comment was generated for commit c24f62a |
@Pierre-Sassoulas this PR should be ready for final review, whenever you are free. Thank you! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Amazing, thank you !
β¦/else usage (#8071) (#8229) (cherry picked from commit 58fce61) Co-authored-by: Zen Lee <[email protected]>
Type of Changes
Description
Extend existing logic to prevent false positive when TYPE_CHECKING is used with if/elif/else blocks.
Closes #7574