Skip to content

Fixed false positive for global-variable-not-assigned #5656

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

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

Closes #5499

* Fixed false positive for ``global-variable-not-assigned`` when the ``del`` statement is used

Closes #5333

* By default, pylint does no longer take files starting with ``.#`` into account. Those are
considered `emacs file locks`. See
https://www.gnu.org/software/emacs/manual/html_node/elisp/File-Locks.html.
Expand Down
4 changes: 4 additions & 0 deletions doc/whatsnew/2.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,7 @@ Other Changes
raise ValueError

Closes #4955

* Fixed false positive for ``global-variable-not-assigned`` when the ``del`` statement is used

Closes #5333
9 changes: 9 additions & 0 deletions pylint/checkers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1672,6 +1672,15 @@ def is_reassigned_after_current(node: nodes.NodeNG, varname: str) -> bool:
)


def is_deleted_after_current(node: nodes.NodeNG, varname: str) -> bool:
"""Check if the given variable name is deleted in the same scope after the current node"""
return any(
getattr(target, "name", None) == varname and target.lineno > node.lineno
for del_node in node.scope().nodes_of_class(nodes.Delete)
for target in del_node.targets
)


def is_function_body_ellipsis(node: nodes.FunctionDef) -> bool:
"""Checks whether a function body only consists of a single Ellipsis"""
return (
Expand Down
1 change: 1 addition & 0 deletions pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,7 @@ def visit_global(self, node: nodes.Global) -> None:
)
if (
not utils.is_reassigned_after_current(node, name)
and not utils.is_deleted_after_current(node, name)
and not_defined_locally_by_import
):
self.add_message("global-variable-not-assigned", args=name, node=node)
Expand Down
7 changes: 7 additions & 0 deletions tests/functional/g/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ def global_no_assign():
print(CONSTANT)


def global_del():
"""Deleting the global name prevents `global-variable-not-assigned`"""
global CONSTANT # [global-statement]
print(CONSTANT)
del CONSTANT


def global_operator_assign():
"""Operator assigns should only throw a global statement error"""
global CONSTANT # [global-statement]
Expand Down
9 changes: 5 additions & 4 deletions tests/functional/g/globals.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ undefined-variable:22:10:22:13:other:Undefined variable 'HOP':UNDEFINED
global-variable-undefined:27:4:27:18:define_constant:Global variable 'SOMEVAR' undefined at the module level:UNDEFINED
global-statement:33:4:33:14:global_with_import:Using the global statement:UNDEFINED
global-variable-not-assigned:39:4:39:19:global_no_assign:Using global for 'CONSTANT' but no assignment is done:UNDEFINED
global-statement:45:4:45:19:global_operator_assign:Using the global statement:UNDEFINED
global-statement:52:4:52:19:global_function_assign:Using the global statement:UNDEFINED
global-statement:62:4:62:15:override_func:Using the global statement:UNDEFINED
global-statement:71:4:71:14:func:Using the global statement:UNDEFINED
global-statement:45:4:45:19:global_del:Using the global statement:UNDEFINED
global-statement:52:4:52:19:global_operator_assign:Using the global statement:UNDEFINED
global-statement:59:4:59:19:global_function_assign:Using the global statement:UNDEFINED
global-statement:69:4:69:15:override_func:Using the global statement:UNDEFINED
global-statement:78:4:78:14:func:Using the global statement:UNDEFINED