Skip to content

allow-global-unused-variables doesn't respect dummy-variables-rgx #8174

Closed as not planned
@swirle13

Description

@swirle13

Bug description

Pylint will throw a warning, unused-variable, for any unused variables/functions when allow-global-unused-variables is enabled, even if the variable/function matches dummy-variables-rgx.

# test1.py
# default settings
def _test_func():           # will not trigger `unused-variable`
    test_var = "unused"     # WILL trigger `unused-variable`
    _test_var = "unused"    # will not trigger `unused-variable`

    def test_method():      # WILL trigger `unused-variable`
        print("unused")

    def _test_method():     # WILL trigger `unused-variable`
        print("unused")


def test_func():            # will not trigger `unused-variable`
    print("unused")


invalid_unused_global_var = "unused"   # WILL trigger `unused-variable`, SHOULD
unused_global_var = "unused"          # WILL trigger `unused-variable`, SHOULDN'T
_unused_global_var = "unused"         # WILL trigger `unused-variable`, SHOULDN'T
# test2.py
# allow-global-unused-variables = false
def _test_func():           # WILL trigger `unused-variable`, SHOULDN'T
    test_var = "unused"     # WILL trigger `unused-variable`
    _test_var = "unused"    # will not trigger `unused-variable`

    def test_method():      # WILL trigger `unused-variable`
        print("unused")

    def _test_method():     # will not trigger `unused-variable`
        print("unused")


def test_func():            # WILL trigger `unused-variable`, SHOULD
    print("unused")


invalid_unused_global_var = "unused"   # WILL trigger `unused-variable`, SHOULD
unused_global_var = "unused"          # WILL trigger `unused-variable`, SHOULDN'T
_unused_global_var = "unused"         # WILL trigger `unused-variable`, SHOULDN'T

The main use case for this is disallowing global variables but allowing unused top level functions. Specifically, I'm running into this issue in AWS Lambda functions, which always have an "unused" global function, def lambda_handler(event, context):. I'm running into this issue in a few different scenarios, and our team rules dictate we can't use any globally defined variables, but require top level functions for other packages to use, so this isn't specifically restricted to AWS Lambda code.

I am aware of using # pylint: disable=unused-variable but the team has strict rules about what is left in the code, and we'd rather not have these in here. We'd prefer to be able to control what values do/don't trip the unused-variable warning via our control file via a regex, which dummy-variables-rgx seemed to be the solution.

I really wish there was a distinction between unused variables and unused functions, either by option or separate rule.

Configuration

No response

Command used

pylint test1.py --allow-global-unused-variables=yes
pylint test2.py --allow-global-unused-variables=no

Pylint output

************* Module test1
test1.py:4:4: W0612: Unused variable 'test_var' (unused-variable)
test1.py:7:4: W0612: Unused variable 'test_method' (unused-variable)

...

************* Module test2
test2.py:4:4: W0612: Unused variable 'test_var' (unused-variable)
test2.py:7:4: W0612: Unused variable 'test_method' (unused-variable)
test2.py:3:0: W0612: Unused variable '_test_func' (unused-variable)
test2.py:14:0: W0612: Unused variable 'test_func' (unused-variable)
test2.py:18:0: W0612: Unused variable 'invalid_unused_global_var' (unused-variable)
test2.py:19:0: W0612: Unused variable 'unused_global_var' (unused-variable)
test2.py:20:0: W0612: Unused variable '_unused_global_var' (unused-variable)

Expected behavior

I would expect any global variables that match the dummy-variables-rgx would be excluded from disallowed global variables.

Pylint version

pylint 2.16.1
astroid 2.14.1
Python 3.8.16 (default, Jan 23 2023, 10:45:02) 
[Clang 14.0.0 (clang-1400.0.29.202)]

OS / Environment

macOS Monterey v12.6.3
zsh 5.8.1 (x86_64-apple-darwin21.0)

Additional dependencies

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Duplicate 🐫Duplicate of an already existing issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions