Skip to content

Prevent redefined-outer-name for if t.TYPE_CHECKING #7525

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

Merged
merged 1 commit into from
Sep 28, 2022

Conversation

jacobtylerwalls
Copy link
Member

Type of Changes

Type
βœ“ πŸ› Bug fix

Description

Closes #7524
Similar solution to #6787

@jacobtylerwalls jacobtylerwalls added Needs backport Needs to be cherry-picked on the current patch version by a pylint's maintainer False Positive 🦟 A message is emitted but nothing is wrong with the code labels Sep 24, 2022
@coveralls
Copy link

coveralls commented Sep 24, 2022

Pull Request Test Coverage Report for Build 3118393877

  • 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 95.315%

Totals Coverage Status
Change from base Build 3115053445: 0.0%
Covered Lines: 17109
Relevant Lines: 17950

πŸ’› - Coveralls

@github-actions
Copy link
Contributor

πŸ€– Effect of this PR on checked open source code: πŸ€–

Effect on flask:
The following messages are no longer emitted:

  1. redefined-outer-name:
    Redefining name 'Flask' from outer scope (line 25)
    https://github.com/pallets/flask/blob/0d8c8ba71bc6362e6ea9af08146dc97e1a0a8abc/src/flask/cli.py#L36
  2. redefined-outer-name:
    Redefining name 'Flask' from outer scope (line 25)
    https://github.com/pallets/flask/blob/0d8c8ba71bc6362e6ea9af08146dc97e1a0a8abc/src/flask/cli.py#L115
  3. redefined-outer-name:
    Redefining name 'Response' from outer scope (line 24)
    https://github.com/pallets/flask/blob/0d8c8ba71bc6362e6ea9af08146dc97e1a0a8abc/src/flask/helpers.py#L267

This comment was generated for commit c4410ea

isinstance(definition.parent, nodes.If)
and definition.parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS
for definition in globs[name]
in_type_checking_block(definition) for definition in globs[name]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not sure if this is in scope, but there is only one reference to TYPING_TYPE_CHECKS_GUARDS remaining. Should we remove it as well and use this function everywhere?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think that should be a follow-up PR. At first glance I'm not sure there's a drop-in replacement.

Copy link
Collaborator

Choose a reason for hiding this comment

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

diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py
index 65a9e8c8b..59e9f7fba 100644
--- a/pylint/checkers/variables.py
+++ b/pylint/checkers/variables.py
@@ -2011,10 +2011,7 @@ class VariablesChecker(BaseChecker):
             if isinstance(defstmt, (nodes.Import, nodes.ImportFrom)):
                 defstmt_parent = defstmt.parent
 
-                if (
-                    isinstance(defstmt_parent, nodes.If)
-                    and defstmt_parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS
-                ):
+                if in_type_checking_block(defstmt):
                     # Exempt those definitions that are used inside the type checking
                     # guard or that are defined in both type checking guard branches.
                     used_in_branch = defstmt_parent.parent_of(node)

Passed all functional tests for me locally.

Copy link
Member Author

@jacobtylerwalls jacobtylerwalls Sep 25, 2022

Choose a reason for hiding this comment

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

I'm concerned there aren't enough tests. This example depends on defstmt_parent being nodes.If. in_type_checking_block is more robust, and doesn't assume direct parentage by a nodes.If. There's more control flow in this example; it needs more thought.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah yeah, my bad. What about:

if isinstance(defstmt_parent, nodes.If) and in_type_checking_block(defstmt):

The in_type_checking_block doesn't do any control flow analysis and only makes sure we infer instead of relying on the string representation. As far as I see it is a drop-in replacement for defstmt_parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS. (But not for isinstance(defstmt_parent, nodes.If) as you correctly spotted!)

Copy link
Member Author

Choose a reason for hiding this comment

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

I still don't think it's a drop-in replacement because in_type_checking_block() is recursive. We don't know which if ancestor met the condition, but the way this block is written, the assumption is that defstmt_parent is the one that met the condition, and doing a drop-in replacement will violate that condition. Leading me to think it needs more refactoring and a follow-up issue.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Fair enough. Shall we open an issue/todo for this? I'm also fine with leaving as is.

Copy link
Member Author

Choose a reason for hiding this comment

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

No problem, I'll do it. πŸ‘πŸ»

Copy link
Member Author

Choose a reason for hiding this comment

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

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'll let you merge when the remaining discussion is resolved.

@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.15.4 milestone Sep 27, 2022
@jacobtylerwalls jacobtylerwalls merged commit 530d790 into pylint-dev:main Sep 28, 2022
@jacobtylerwalls jacobtylerwalls deleted the issue-7524 branch September 28, 2022 20:41
@Pierre-Sassoulas Pierre-Sassoulas added Backported and removed Needs backport Needs to be cherry-picked on the current patch version by a pylint's maintainer labels Oct 9, 2022
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.

TYPE_CHECKS_GUARDS fooled by import alias
4 participants