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

Fix used-before-assignment for conditional self-referential typing #5532

Merged
merged 1 commit into from
Dec 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ Release date: TBA

Closes #3793

* Fixed false positive for ``used-before-assignment`` with self-referential type
annotation in conditional statements within class methods.

Closes #5499

* ``used-before-assignment`` now assumes that assignments in except blocks
may not have occurred and warns accordingly.

Expand Down
5 changes: 5 additions & 0 deletions doc/whatsnew/2.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ Other Changes

Closes #3793

* Fixed false positive for ``used-before-assignment`` with self-referential type
annotation in conditional statements within class methods.

Closes #5499

* ``used-before-assignment`` now assumes that assignments in except blocks
may not have occurred and warns accordingly.

Expand Down
2 changes: 1 addition & 1 deletion pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1805,7 +1805,7 @@ def _is_first_level_self_reference(
"""
if (
node.frame().parent == defstmt
and node.statement(future=True) not in node.frame().body
and node.statement(future=True) == node.frame()
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand how this work, could you explain ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Basically for arguments in function definitions their statement is also their frame (the function definition). For nodes within the body or even within sub-bodies (conditionals) the statement will be different from the frame (the latter still being the function definition).

Copy link
Member

Choose a reason for hiding this comment

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

That makes sense, thank you.

):
# Check if used as type annotation
# Break but don't emit message if postponed evaluation is enabled
Expand Down
13 changes: 13 additions & 0 deletions tests/functional/u/use/used_before_assignment_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,16 @@ def inner_method(self, other: MyOtherClass) -> bool:
def self_referential_optional_within_method(self) -> None:
variable: Optional[MyOtherClass] = self
print(variable)


class MyThirdClass:
"""Class to test self referential variable typing within conditionals.
This regressed, reported in: https://github.com/PyCQA/pylint/issues/5499
"""

def function(self, var: int) -> None:
if var < 0.5:
_x: MyThirdClass = self

def other_function(self) -> None:
_x: MyThirdClass = self