Skip to content

Commit fe3436e

Browse files
mbyrnepr2jacobtylerwalls
authored andcommitted
False positive global-variable-not-assigned (#7479)
* Fix false positive for ``global-variable-not-assigned`` when a global variable is re-assigned via a ``ImportFrom`` node. Closes #4809 Co-authored-by: Jacob Walls <[email protected]>
1 parent 52cf631 commit fe3436e

File tree

6 files changed

+22
-18
lines changed

6 files changed

+22
-18
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix false positive for ``global-variable-not-assigned`` when a global variable is re-assigned via an ``ImportFrom`` node.
2+
3+
Closes #4809

pylint/checkers/variables.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1311,7 +1311,8 @@ def visit_global(self, node: nodes.Global) -> None:
13111311
assign_nodes = []
13121312

13131313
not_defined_locally_by_import = not any(
1314-
isinstance(local, nodes.Import) for local in locals_.get(name, ())
1314+
isinstance(local, (nodes.Import, nodes.ImportFrom))
1315+
for local in locals_.get(name, ())
13151316
)
13161317
if (
13171318
not utils.is_reassigned_after_current(node, name)

tests/functional/g/globals.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,15 @@ def define_constant():
3131

3232

3333
def global_with_import():
34-
"""should only warn for global-statement"""
34+
"""should only warn for global-statement when using `Import` node"""
3535
global sys # [global-statement]
36-
import sys # pylint: disable=import-outside-toplevel
36+
import sys
37+
38+
39+
def global_with_import_from():
40+
"""should only warn for global-statement when using `ImportFrom` node"""
41+
global namedtuple # [global-statement]
42+
from collections import namedtuple
3743

3844

3945
def global_no_assign():
@@ -75,11 +81,6 @@ def FUNC():
7581

7682
FUNC()
7783

78-
def func():
79-
"""Overriding a global with an import should only throw a global statement error"""
80-
global sys # [global-statement]
81-
82-
import sys
8384

8485
def override_class():
8586
"""Overriding a class should only throw a global statement error"""

tests/functional/g/globals.txt

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ global-variable-not-assigned:23:4:23:14:other:Using global for 'HOP' but no assi
55
undefined-variable:24:10:24:13:other:Undefined variable 'HOP':UNDEFINED
66
global-variable-undefined:29:4:29:18:define_constant:Global variable 'SOMEVAR' undefined at the module level:UNDEFINED
77
global-statement:35:4:35:14:global_with_import:Using the global statement:UNDEFINED
8-
global-variable-not-assigned:41:4:41:19:global_no_assign:Using global for 'CONSTANT' but no assignment is done:UNDEFINED
9-
global-statement:47:4:47:19:global_del:Using the global statement:UNDEFINED
10-
global-statement:54:4:54:19:global_operator_assign:Using the global statement:UNDEFINED
11-
global-statement:61:4:61:19:global_function_assign:Using the global statement:UNDEFINED
12-
global-statement:71:4:71:15:override_func:Using the global statement:UNDEFINED
13-
global-statement:80:4:80:14:func:Using the global statement:UNDEFINED
14-
global-statement:86:4:86:16:override_class:Using the global statement:UNDEFINED
8+
global-statement:41:4:41:21:global_with_import_from:Using the global statement:UNDEFINED
9+
global-variable-not-assigned:47:4:47:19:global_no_assign:Using global for 'CONSTANT' but no assignment is done:UNDEFINED
10+
global-statement:53:4:53:19:global_del:Using the global statement:UNDEFINED
11+
global-statement:60:4:60:19:global_operator_assign:Using the global statement:UNDEFINED
12+
global-statement:67:4:67:19:global_function_assign:Using the global statement:UNDEFINED
13+
global-statement:77:4:77:15:override_func:Using the global statement:UNDEFINED
14+
global-statement:87:4:87:16:override_class:Using the global statement:UNDEFINED

tests/functional/u/unused/unused_variable.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def test_global():
9494
variables through imports.
9595
"""
9696
# pylint: disable=redefined-outer-name
97-
global PATH, OS, collections, deque # [global-variable-not-assigned, global-variable-not-assigned]
97+
global PATH, OS, collections, deque # [global-statement]
9898
from os import path as PATH
9999
import os as OS
100100
import collections

tests/functional/u/unused/unused_variable.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ unused-import:55:4:55:38:unused_import_from:Unused namedtuple imported from coll
1313
unused-import:59:4:59:40:unused_import_in_function:Unused hexdigits imported from string:UNDEFINED
1414
unused-variable:64:4:64:10:hello:Unused variable 'my_var':UNDEFINED
1515
unused-variable:75:4:75:8:function:Unused variable 'aaaa':UNDEFINED
16-
global-variable-not-assigned:97:4:97:39:test_global:Using global for 'PATH' but no assignment is done:UNDEFINED
17-
global-variable-not-assigned:97:4:97:39:test_global:Using global for 'deque' but no assignment is done:UNDEFINED
16+
global-statement:97:4:97:39:test_global:Using the global statement:UNDEFINED
1817
unused-import:103:4:103:28:test_global:Unused platform imported from sys:UNDEFINED
1918
unused-import:104:4:104:38:test_global:Unused version imported from sys as VERSION:UNDEFINED
2019
unused-import:105:4:105:15:test_global:Unused import this:UNDEFINED

0 commit comments

Comments
 (0)