pylint icon indicating copy to clipboard operation
pylint copied to clipboard

Don't consider `__future__` imports and `__all__` unused

Open Syndace opened this issue 1 year ago • 5 comments

Bug description

from __future__ import annotations

__all__ = [ "foo" ]

def foo():
    pass

Command used

pylint foo.py --allow-global-unused-variables n

Pylint output

************* Module foo
...
foo.py:3:0: W0612: Unused variable '__all__' (unused-variable)
foo.py:1:0: W0612: Unused variable 'annotations' (unused-variable)

Expected behavior

Imports from __future__ and the __all__ variable are implicitly used by the interpreter and should as such not be considered unused, even with --allow-global-unused-variables n.

Pylint version

All versions

Syndace avatar Oct 14 '24 08:10 Syndace

I can't reproduce with the latest version. Could you install pylint 3.3.1 and confirm the issue please ?

Pierre-Sassoulas avatar Oct 14 '24 15:10 Pierre-Sassoulas

Yes, I'm on the latest version:

pylint 3.3.1
astroid 3.3.5
Python 3.12.7 (main, Oct  1 2024, 11:15:50) [GCC 14.2.1 20240910]

Syndace avatar Oct 14 '24 15:10 Syndace

Specifically, I'm on Arch and used the following commands:

$ python3 -m venv env
$ source env/bin/activate
$ pip install --upgrade pylint
$ pylint foo.py --allow-global-unused-variables n

Syndace avatar Oct 14 '24 15:10 Syndace

Thank you, it only happens with --allow-global-unused-variables n

Pierre-Sassoulas avatar Oct 14 '24 15:10 Pierre-Sassoulas

I encountered another false positive I believe. Re-exports from __init__.py with --allow-reexport-from-package y should not be considered unused globals. Here is an updated example that reproduces all three cases:

__init__.py:

from .test import test as test

test.py:

from __future__ import annotations

__all__ = [ "test" ]

def test():
    pass

Command:

pylint --allow-global-unused-variables n --allow-reexport-from-package y .

Output (docstring warnings omitted):

************* Module global_unused
__init__.py:1:0: W0612: Unused variable 'test' (unused-variable)
************* Module global_unused.test
test.py:3:0: W0612: Unused variable '__all__' (unused-variable)
test.py:1:0: W0612: Unused variable 'annotations' (unused-variable)

Syndace avatar Oct 15 '24 04:10 Syndace

Regarding: --allow-reexport-from-package - It appears to only affect useless-import-alias. See the source and this comment for more info.

mbyrnepr2 avatar Oct 12 '25 17:10 mbyrnepr2