Skip to content
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

Regression used-before-assignment with type annotation #5342

Closed
cdce8p opened this issue Nov 19, 2021 · 3 comments · Fixed by #5454
Closed

Regression used-before-assignment with type annotation #5342

cdce8p opened this issue Nov 19, 2021 · 3 comments · Fixed by #5454
Assignees
Labels
C: used-before-assignment Issues related to 'used-before-assignment' check Regression

Comments

@cdce8p
Copy link
Member

cdce8p commented Nov 19, 2021

Bug description

from typing import Optional

class A:
    def func(self) -> None:
        _node: Optional[A] = self  # false-positive: used-before-assignment

Noticed this in astroid first -> astroid/nodes/node_ng.py. The example above is valid as the function body isn't evaluated until called at witch time the class is already defined. This is a regression in main compared to v2.11.1.

/CC: @DanielNoord Haven't looked at it yet, but I would guess this might be related to the work you've been doing in this area.

Configuration

No response

Command used

pylint test.py

Pylint output

E0601: Using variable 'A' before assignment (used-before-assignment)

Expected behavior

No error

Pylint version

pylint 2.11.2-dev0
astroid 2.8.6-dev0
Python 3.10.0 (v3.10.0:b494f5935c, Oct  4 2021, 14:59:20) [Clang 12.0.5 (clang-1205.0.22.11)]

OS / Environment

No response

Additional dependencies

No response

@cdce8p cdce8p added Regression C: used-before-assignment Issues related to 'used-before-assignment' check labels Nov 19, 2021
@DanielNoord DanielNoord self-assigned this Nov 19, 2021
@DanielNoord
Copy link
Collaborator

I'll have a look!

@DanielNoord
Copy link
Collaborator

DanielNoord commented Nov 20, 2021

For future reference.

This does create a false positive:

class MyClass:
    def self_referential_optional_within_method(self) -> None:
        variable: Optional[MyClass] = self
        print(variable)

    def correct_inner_typing_method(self) -> bool:
        def inner_method(self, other: MyClass) -> bool:
            return self == other

        return inner_method(self, MyClass())

But this doesn't:

class MyClass:
    def correct_inner_typing_method(self) -> bool:
        def inner_method(self, other: MyClass) -> bool:
            return self == other

        return inner_method(self, MyClass())
    
    def self_referential_optional_within_method(self) -> None:
        variable: Optional[MyClass] = self
        print(variable)

This is (somewhat) blocked by #5241 as I would like to merge the refactor before working on this to avoid future merge conflicts.

@The-Compiler
Copy link
Contributor

FWIW I can confirm this seems fully fixed in 2.12.2 - thanks everyone!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: used-before-assignment Issues related to 'used-before-assignment' check Regression
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants