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

N816 doesn't flag lower-case variable names #14905

Closed
isaacag opened this issue Dec 11, 2024 · 5 comments
Closed

N816 doesn't flag lower-case variable names #14905

isaacag opened this issue Dec 11, 2024 · 5 comments
Labels
needs-decision Awaiting a decision from a maintainer rule Implementing or modifying a lint rule

Comments

@isaacag
Copy link

isaacag commented Dec 11, 2024

Context managers are not taken into consideration for variable naming conventions, despite PEP8 not making exceptions for them. The only way to trigger variable rules is to wrap them in other structures like a def.

def example_function():
    Variable = 10 # N806 triggered
    with open('file.txt') as file:
        Variable = file.read() # N806 triggered
with open('file.txt') as file:
    Variable = file.read() # No rule triggered
@MichaReiser
Copy link
Member

The current behavior makes sense to me, considering that the rule is about variable names in functions and doesn't enforce lower case variable names in general.

Checks for the use of non-lowercase variable names in functions.

And Variable in the second example is a variable in the module scope.

@MichaReiser MichaReiser added the question Asking for support or clarification label Dec 11, 2024
@isaacag
Copy link
Author

isaacag commented Dec 11, 2024

If possible I think it would be nice to have an additional rule covering those cases, even if is another scope.

@MichaReiser
Copy link
Member

MichaReiser commented Dec 11, 2024

If possible I think it would be nice to have an additional rule covering those cases, even if is another scope.

There's https://docs.astral.sh/ruff/rules/mixed-case-variable-in-global-scope/ but it doesn't forbid using variable names that start with an uppercase character. I'm not sure why is but the rule matches the behavior of the upstream rule PyCQA/pep8-naming#69

@isaacag
Copy link
Author

isaacag commented Dec 11, 2024

That is odd, docs leave the impression that any non snake case variables will trigger the rule.

@MichaReiser MichaReiser changed the title PEP8 inconsistent variable rules for context managers N816 doesn't flag lower-case variable names Dec 16, 2024
@MichaReiser MichaReiser added rule Implementing or modifying a lint rule needs-decision Awaiting a decision from a maintainer and removed question Asking for support or clarification labels Dec 16, 2024
@MichaReiser
Copy link
Member

I merge this with #15069 that lists a few reasons

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-decision Awaiting a decision from a maintainer rule Implementing or modifying a lint rule
Projects
None yet
Development

No branches or pull requests

2 participants