Skip to content

Fix used-before-assignment for functions/classes defined in type checking guard #7370

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
Aug 27, 2022

Conversation

Rogdham
Copy link
Contributor

@Rogdham Rogdham commented Aug 27, 2022

Type of Changes

Type
βœ“ πŸ› Bug fix

Description

Closes #7368

This PR fixes the following false-positive:

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from typing_extensions import assert_type
else:

    def assert_type(val, _):
        return val

ANSWER = 42
assert_type(ANSWER, int)  # E0601: Using variable 'assert_type' before assignment (used-before-assignment)

The issue comes from definitions in the else block of a if type-guard.

In 3159b17 the case was already handled for assignments, I had only added cases for function and class that are defined in the else part.

@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 Aug 27, 2022
@jacobtylerwalls jacobtylerwalls added this to the 2.15.1 milestone Aug 27, 2022
@coveralls
Copy link

Pull Request Test Coverage Report for Build 2939756418

  • 4 of 4 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.0005%) to 95.318%

Totals Coverage Status
Change from base Build 2939490288: 0.0005%
Covered Lines: 16937
Relevant Lines: 17769

πŸ’› - Coveralls

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.

Looks great. Congrats on becoming a pylint contributor!

@github-actions
Copy link
Contributor

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

This comment was generated for commit a469ec8

@jacobtylerwalls jacobtylerwalls merged commit 728177c into pylint-dev:main Aug 27, 2022
@Rogdham Rogdham deleted the fix-7368 branch August 27, 2022 16:34
@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 Sep 1, 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.

E0601: used-before-assignment false positive for import under TYPE_CHECKING check
4 participants