-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix #5713: Emit used-before-assignment
instead of undefined-variable
when accessing unused type annotations
#5718
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
Pierre-Sassoulas
merged 3 commits into
pylint-dev:main
from
jacobtylerwalls:change-msg-type
Jan 26, 2022
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
90 changes: 90 additions & 0 deletions
90
tests/functional/u/use/used_before_assignment_type_annotations.py
This file contains hidden or 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,90 @@ | ||
"""Tests for annotation of variables and potential use before assignment""" | ||
# pylint: disable=too-few-public-methods, global-variable-not-assigned | ||
from collections import namedtuple | ||
from typing import List | ||
|
||
def value_and_type_assignment(): | ||
"""The variable assigned a value and type""" | ||
variable: int = 2 | ||
print(variable) | ||
|
||
|
||
def only_type_assignment(): | ||
"""The variable never gets assigned a value""" | ||
variable: int | ||
print(variable) # [used-before-assignment] | ||
|
||
|
||
def both_type_and_value_assignment(): | ||
"""The variable first gets a type and subsequently a value""" | ||
variable: int | ||
variable = 1 | ||
print(variable) | ||
|
||
|
||
def value_assignment_after_access(): | ||
"""The variable gets a value after it has been accessed""" | ||
variable: int | ||
print(variable) # [used-before-assignment] | ||
variable = 1 | ||
|
||
|
||
def value_assignment_from_iterator(): | ||
"""The variables gets a value from an iterator""" | ||
variable: int | ||
for variable in (1, 2): | ||
print(variable) | ||
|
||
|
||
def assignment_in_comprehension(): | ||
"""A previously typed variables gets used in a comprehension. Don't crash!""" | ||
some_list: List[int] | ||
some_list = [1, 2, 3] | ||
some_list = [i * 2 for i in some_list] | ||
|
||
|
||
def decorator_returning_function(): | ||
"""A decorator that returns a wrapper function with decoupled typing""" | ||
def wrapper_with_decoupled_typing(): | ||
print(var) | ||
|
||
var: int | ||
var = 2 | ||
return wrapper_with_decoupled_typing | ||
|
||
|
||
def decorator_returning_incorrect_function(): | ||
"""A decorator that returns a wrapper function with decoupled typing""" | ||
def wrapper_with_type_and_no_value(): | ||
# This emits NameError rather than UnboundLocalError, so | ||
# undefined-variable is okay, even though the traceback refers | ||
# to "free variable 'var' referenced before assignment" | ||
print(var) # [undefined-variable] | ||
|
||
var: int | ||
return wrapper_with_type_and_no_value | ||
|
||
|
||
def typing_and_value_assignment_with_tuple_assignment(): | ||
"""The typed variables get assigned with a tuple assignment""" | ||
var_one: int | ||
var_two: int | ||
var_one, var_two = 1, 1 | ||
print(var_one) | ||
print(var_two) | ||
|
||
|
||
def nested_class_as_return_annotation(): | ||
"""A namedtuple as a class attribute is used as a return annotation | ||
|
||
Taken from https://github.com/PyCQA/pylint/issues/5568""" | ||
class MyObject: | ||
"""namedtuple as class attribute""" | ||
Coords = namedtuple('Point', ['x', 'y']) | ||
|
||
def my_method(self) -> Coords: | ||
"""Return annotation is valid""" | ||
# pylint: disable=unnecessary-pass | ||
pass | ||
|
||
print(MyObject) |
3 changes: 3 additions & 0 deletions
3
tests/functional/u/use/used_before_assignment_type_annotations.txt
This file contains hidden or 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:15:10:15:18:only_type_assignment:Using variable 'variable' before assignment:HIGH | ||
used-before-assignment:28:10:28:18:value_assignment_after_access:Using variable 'variable' before assignment:HIGH | ||
undefined-variable:62:14:62:17:decorator_returning_incorrect_function.wrapper_with_type_and_no_value:Undefined variable 'var':HIGH |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.