Skip to content

Conversation

zenlyj
Copy link
Contributor

@zenlyj zenlyj commented Jan 17, 2023

Type of Changes

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

Description

Extend existing logic to prevent false positive when TYPE_CHECKING is used with if/elif/else blocks.

Closes #7574

@codecov
Copy link

codecov bot commented Jan 17, 2023

Codecov Report

Merging #8071 (c24f62a) into main (8d13dbe) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #8071   +/-   ##
=======================================
  Coverage   95.53%   95.54%           
=======================================
  Files         177      177           
  Lines       18622    18633   +11     
=======================================
+ Hits        17791    17802   +11     
  Misses        831      831           
Impacted Files Coverage Ξ”
pylint/checkers/utils.py 95.83% <100.00%> (+0.06%) ⬆️
pylint/checkers/variables.py 97.37% <100.00%> (-0.01%) ⬇️

@github-actions

This comment has been minimized.

@Pierre-Sassoulas Pierre-Sassoulas added the False Positive 🦟 A message is emitted but nothing is wrong with the code label Jan 17, 2023
Copy link
Member

@jacobtylerwalls jacobtylerwalls left a 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

@zenlyj
Copy link
Contributor Author

zenlyj commented Feb 2, 2023

@jacobtylerwalls Thanks for the review! I looked into the usage _exhaustively_define_name_raise_or_return and _defines_name_raises_or_returns_recursive, but I don't think they can be used without making significant changes to them.

I took reference from those 2 and implemented a new function in utils.py. Currently, the solution should handle the mentioned nested if/else case, alongside with a few other cases (reflected in the updated functional tests).

Please let me know if I missed out on anything here!

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@zenlyj zenlyj changed the title Fix false positive for used-before-assignment Fix used-before-assignment false positive for TYPE_CHECKING if/elif/else usage Feb 4, 2023
jacobtylerwalls
jacobtylerwalls previously approved these changes Feb 4, 2023
Copy link
Member

@jacobtylerwalls jacobtylerwalls left a 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.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 5, 2023

πŸ€– According to the primer, this change has no effect on the checked open source code. πŸ€–πŸŽ‰

This comment was generated for commit c24f62a

@zenlyj
Copy link
Contributor Author

zenlyj commented Feb 5, 2023

@Pierre-Sassoulas this PR should be ready for final review, whenever you are free. Thank you!

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.

Amazing, thank you !

@Pierre-Sassoulas Pierre-Sassoulas merged commit 58fce61 into pylint-dev:main Feb 7, 2023
github-actions bot pushed a commit that referenced this pull request Feb 7, 2023
Pierre-Sassoulas pushed a commit that referenced this pull request Feb 8, 2023
…/else usage (#8071) (#8229)

(cherry picked from commit 58fce61)

Co-authored-by: Zen Lee <53538590+zenlyj@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backported 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 used-before-assignment when TYPE_CHECKING is used with if/elif/else blocks
4 participants