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

False negative global-variable-not-assigned (while respective functional test passes correctly) #1375

Closed
eight04 opened this issue Mar 11, 2017 · 9 comments · Fixed by #4990
Labels
Bug 🪲 False Negative 🦋 No message is emitted but something is wrong with the code
Milestone

Comments

@eight04
Copy link

eight04 commented Mar 11, 2017

Steps to reproduce

Try to lint following code:

a = 1
def foo():
	global a
	print(a)

Current behavior

No config file found, using default configuration
************* Module test
W:  3, 0: Found indentation with tabs instead of spaces (mixed-indentation)
W:  4, 0: Found indentation with tabs instead of spaces (mixed-indentation)
C:  1, 0: Missing module docstring (missing-docstring)
C:  1, 0: Invalid constant name "a" (invalid-name)
C:  2, 0: Black listed name "foo" (blacklisted-name)
C:  2, 0: Missing function docstring (missing-docstring)
C:  3, 1: Invalid constant name "a" (invalid-name)
W:  3, 1: Using the global statement (global-statement)

Expected behavior

W:  3, 1: Using global for 'a' but no assignment is done (global-variable-not-as
signed)

pylint --version output

pylint 1.7.0,
astroid 1.5.0
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (In
tel)]

It is reported correctly in pylint 1.6.5.

No config file found, using default configuration
************* Module test
W:  3, 0: Found indentation with tabs instead of spaces (mixed-indentation)
W:  4, 0: Found indentation with tabs instead of spaces (mixed-indentation)
C:  1, 0: Missing module docstring (missing-docstring)
C:  1, 0: Invalid constant name "a" (invalid-name)
C:  2, 0: Black listed name "foo" (blacklisted-name)
C:  2, 0: Missing function docstring (missing-docstring)
W:  3, 1: Using global for 'a' but no assignment is done (global-variable-not-as
signed)
C:  3, 1: Invalid constant name "a" (invalid-name)
__main__.py 1.6.5,
astroid 1.4.9
Python 3.5.3 (v3.5.3:1880cb95a742, Jan 16 2017, 15:51:26) [MSC v.1900 32 bit (In
tel)]
@rogalski
Copy link
Contributor

rogalski commented Mar 11, 2017 via email

@rogalski
Copy link
Contributor

Closing as not-a-bug.

@eight04
Copy link
Author

eight04 commented Mar 12, 2017

Does it mean that global-variable-not-assigned has been merged into global-statement?

@rogalski
Copy link
Contributor

rogalski commented Mar 12, 2017

No, those are two independent checks, and each of them mean different thing.

global-variable-not-assigned means that used global statement is superfluous - standard name lookup rules would read a name from enclosing scope. global is needed only when you want to assign new value to this name. You are not doing it in included code snippet.

@rogalski
Copy link
Contributor

Code snippet shown below will correctly not emit global-variable-not-assigned message.

a = 1
def foo():
	global a
	print(a)
        a += 1

Code snippet shown in this bug correctly emits global-variable-not-assigned message.

@eight04
Copy link
Author

eight04 commented Mar 12, 2017

global-variable-not-assigned is not emitted on my machine. I'll install latest astroid and try again.

@eight04
Copy link
Author

eight04 commented Mar 12, 2017

pylint --version

No config file found, using default configuration
pylint 1.7.0,
astroid 1.5.0
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (In
tel)]

pip freeze

-e git+https://github.com/PyCQA/astroid.git@e93dd9919b263ff3e9782b64fe5f387acd8d
1bda#egg=astroid
-e git+https://github.com/PyCQA/pylint.git@ff4f0847b844a46afdddc018f46e2cb3cd6c5
dda#egg=pylint

test.py

a = 1
def foo():
	global a
	print(a)

pylint test

No config file found, using default configuration
************* Module test
W:  3, 0: Found indentation with tabs instead of spaces (mixed-indentation)
W:  4, 0: Found indentation with tabs instead of spaces (mixed-indentation)
C:  1, 0: Missing module docstring (missing-docstring)
C:  1, 0: Invalid constant name "a" (invalid-name)
C:  2, 0: Black listed name "foo" (blacklisted-name)
C:  2, 0: Missing function docstring (missing-docstring)
C:  3, 1: Invalid constant name "a" (invalid-name)
W:  3, 1: Using the global statement (global-statement)

----------------------------------------------------------------------
Your code has been rated at -10.00/10 (previous run: -10.00/10, +0.00)

global-variable-not-assigned is missing.

@rogalski
Copy link
Contributor

Ugh, it seems like I misread problem statement, sorry about that.

It's quite surprising, there is a very similar test in test suite that passes successfully.
Can you check if tox -e py36 -- -k test_functional[globals] passes?

@eight04
Copy link
Author

eight04 commented Mar 12, 2017

I got an error when installing editdistance. The log.

However, if I run pytest pylint/test -k test_functional[globals]

============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.0.6, py-1.4.32, pluggy-0.4.0
rootdir: D:\Dev\pylint, inifile: pytest.ini
collected 860 items

pylint\test\test_functional.py .

============================ 859 tests deselected =============================
================== 1 passed, 859 deselected in 1.96 seconds ===================

@rogalski rogalski reopened this Mar 12, 2017
@rogalski rogalski changed the title False negative global-variable-not-assigned False negative global-variable-not-assigned (while respective functional test passes correctly) Mar 12, 2017
DanielNoord added a commit to DanielNoord/pylint that referenced this issue Sep 10, 2021
This checker now checks whether the names after the global keyword
are reassigned in the local scope.
This closes pylint-dev#1375
DanielNoord added a commit to DanielNoord/pylint that referenced this issue Sep 10, 2021
This checker now checks whether the names after the global keyword
are reassigned in the local scope.
This closes pylint-dev#1375
DanielNoord added a commit to DanielNoord/pylint that referenced this issue Sep 10, 2021
This checker now checks whether the names after the global keyword
are reassigned in the local scope.
This closes pylint-dev#1375
@Pierre-Sassoulas Pierre-Sassoulas added the False Negative 🦋 No message is emitted but something is wrong with the code label Sep 10, 2021
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.11.0 milestone Sep 10, 2021
Pierre-Sassoulas added a commit that referenced this issue Sep 11, 2021
* Make ``global-variable-not-assigned`` check local scope
This checker now checks whether the names after the global keyword
are reassigned in the local scope.
This closes #1375

* Make ``global-variable-not-assigned`` check functions
This checker now also checks function defintions in the module and local scope
This closes #330

Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
return42 added a commit to searxng/searxng that referenced this issue Sep 17, 2021
If there is no write access, there is no need for global.  Remove global
statement if there is no assignment.

global-variable-not-assigned:
  Using global for names but no assignment is done Used when a variable is
  defined through the "global" statement but no assignment to this variable is
  done.

In Pylint 2.11 the global-variable-not-assigned checker now catches global
variables that are never reassigned in a local scope and catches (reassigned)
functions [1][2]

[1] https://pylint.pycqa.org/en/latest/whatsnew/2.11.html
[2] pylint-dev/pylint#1375

Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 False Negative 🦋 No message is emitted but something is wrong with the code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants