Fix used-before-assignment false positive for TYPE_CHECKING if/elif/else usage#8071
Fix used-before-assignment false positive for TYPE_CHECKING if/elif/else usage#8071Pierre-Sassoulas merged 21 commits intopylint-dev:mainfrom zenlyj:false_positive_type_checking
used-before-assignment false positive for TYPE_CHECKING if/elif/else usage#8071Conversation
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.
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 urlopenGives:
E AssertionError: Wrong results for file "used_before_assignment_typing":
E
E Unexpected in testdata:
E 124: used-before-assignment
|
@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-assignmentused-before-assignment false positive for TYPE_CHECKING if/elif/else usage
jacobtylerwalls
left a comment
There was a problem hiding this comment.
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.
|
🤖 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! |
Pierre-Sassoulas
left a comment
There was a problem hiding this comment.
Amazing, thank you !
Type of Changes
Description
Extend existing logic to prevent false positive when TYPE_CHECKING is used with if/elif/else blocks.
Closes #7574