pylint
pylint copied to clipboard
Don't consider `__future__` imports and `__all__` unused
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
I can't reproduce with the latest version. Could you install pylint 3.3.1 and confirm the issue please ?
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]
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
Thank you, it only happens with --allow-global-unused-variables n
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)
Regarding: --allow-reexport-from-package - It appears to only affect useless-import-alias.
See the source and this comment for more info.