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

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

Closed
swirle13 opened this issue Feb 2, 2023 · 2 comments · May be fixed by #9570
Closed

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

swirle13 opened this issue Feb 2, 2023 · 2 comments · May be fixed by #9570
Labels
Duplicate 🐫 Duplicate of an already existing issue

Comments

@swirle13
Copy link

swirle13 commented Feb 2, 2023

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

@swirle13 swirle13 added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Feb 2, 2023
@Pierre-Sassoulas
Copy link
Member

I think this is a duplicate of #8013

@Pierre-Sassoulas Pierre-Sassoulas closed this as not planned Won't fix, can't repro, duplicate, stale Feb 2, 2023
@Pierre-Sassoulas Pierre-Sassoulas added Duplicate 🐫 Duplicate of an already existing issue and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Feb 2, 2023
@com6056
Copy link

com6056 commented Apr 25, 2024

Think this is slightly different than #8013 and should hopefully be fixed by #9570, could we re-open this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate 🐫 Duplicate of an already existing issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants