-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix false positive for
used-before-assignment
for vars in methods (…
…#5454) * Create new ``used_before_assignment_typing`` test file * Fix false positive for ``used-before-assignment`` for vars in methods
- Loading branch information
1 parent
c15d1cf
commit 8f12337
Showing
6 changed files
with
77 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,2 @@ | ||
used-before-assignment:8:19:8:22::Using variable 'MSG' before assignment:UNDEFINED | ||
used-before-assignment:10:20:10:24::Using variable 'MSG2' before assignment:UNDEFINED | ||
used-before-assignment:17:21:17:28:MyClass.incorrect_typing_method:Using variable 'MyClass' before assignment:UNDEFINED | ||
used-before-assignment:22:26:22:33:MyClass.incorrect_nested_typing_method:Using variable 'MyClass' before assignment:UNDEFINED | ||
used-before-assignment:27:20:27:27:MyClass.incorrect_default_method:Using variable 'MyClass' before assignment:UNDEFINED | ||
used-before-assignment:6:19:6:22::Using variable 'MSG' before assignment:UNDEFINED | ||
used-before-assignment:8:20:8:24::Using variable 'MSG2' before assignment:UNDEFINED |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
"""Tests for used-before-assignment for typing related issues""" | ||
# pylint: disable=missing-function-docstring | ||
|
||
|
||
from typing import List, Optional | ||
|
||
|
||
class MyClass: | ||
"""Type annotation or default values for first level methods can't refer to their own class""" | ||
|
||
def incorrect_typing_method( | ||
self, other: MyClass # [used-before-assignment] | ||
) -> bool: | ||
return self == other | ||
|
||
def incorrect_nested_typing_method( | ||
self, other: List[MyClass] # [used-before-assignment] | ||
) -> bool: | ||
return self == other[0] | ||
|
||
def incorrect_default_method( | ||
self, other=MyClass() # [used-before-assignment] | ||
) -> bool: | ||
return self == other | ||
|
||
def correct_string_typing_method(self, other: "MyClass") -> bool: | ||
return self == other | ||
|
||
def correct_inner_typing_method(self) -> bool: | ||
def inner_method(self, other: MyClass) -> bool: | ||
return self == other | ||
|
||
return inner_method(self, MyClass()) | ||
|
||
|
||
class MySecondClass: | ||
"""Class to test self referential variable typing. | ||
This regressed, reported in: https://github.com/PyCQA/pylint/issues/5342 | ||
""" | ||
|
||
def self_referential_optional_within_method(self) -> None: | ||
variable: Optional[MySecondClass] = self | ||
print(variable) | ||
|
||
def correct_inner_typing_method(self) -> bool: | ||
def inner_method(self, other: MySecondClass) -> bool: | ||
return self == other | ||
|
||
return inner_method(self, MySecondClass()) | ||
|
||
|
||
class MyOtherClass: | ||
"""Class to test self referential variable typing, no regression.""" | ||
|
||
def correct_inner_typing_method(self) -> bool: | ||
def inner_method(self, other: MyOtherClass) -> bool: | ||
return self == other | ||
|
||
return inner_method(self, MyOtherClass()) | ||
|
||
def self_referential_optional_within_method(self) -> None: | ||
variable: Optional[MyOtherClass] = self | ||
print(variable) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
used-before-assignment:12:21:12:28:MyClass.incorrect_typing_method:Using variable 'MyClass' before assignment:UNDEFINED | ||
used-before-assignment:17:26:17:33:MyClass.incorrect_nested_typing_method:Using variable 'MyClass' before assignment:UNDEFINED | ||
used-before-assignment:22:20:22:27:MyClass.incorrect_default_method:Using variable 'MyClass' before assignment:UNDEFINED |