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

Feature Request : C0103/invalid-name: Allow specifying valid variable and constant patterns with regular expressions. #10629

Open
astrowonk opened this issue Mar 27, 2024 · 5 comments
Labels
needs-decision Awaiting a decision from a maintainer rule Implementing or modifying a lint rule

Comments

@astrowonk
Copy link

This is similar to the closed #7660. As noted in that issue, Pylint lets you *define your own regex for acceptable variable and constant names (enforcing snake_case for example, or variable length,or both):

variable-rgx=[a-z_][a-z0-9_]{2,40}$

While C0103 in pylint is checked off in #970, one can not define explicit regex for what constitutes an acceptable naming convention. #7660 was closed with updated documentation on ignore-names globbing, but I don't think that is similar functionality to pylint that I'm requesting. I would like parity with pylint to allow regex for variable and constant names, so users can enforce snake_case or pascalcase, etc. and then mismatches with that regex triggering invalid-name, like pylint does now. Thanks!

@MichaReiser
Copy link
Member

Is the main goal to enforce snake, pascal, or another predefined casing standard? Iˋm asking because Ruff canˋt offer autofixes when we allow a custom regex (and error message would probably also be somewhat cryptic).

Another reason for not allowing a regex is that ruff has other rules to enforce a certain variable lenght and we want to avoid overlapping rules

@astrowonk
Copy link
Author

Is the main goal to enforce snake, pascal, or another predefined casing standard? Iˋm asking because Ruff canˋt offer autofixes when we allow a custom regex (and error message would probably also be somewhat cryptic).

Another reason for not allowing a regex is that ruff has other rules to enforce a certain variable lenght and we want to avoid overlapping rules

My particular use case is pretty much in the regex above; with it pylint flags non-compliant variable names: anything that isn't snake case or outside the min/max variable length.

Obviously autofix is super useful but I think it's fine to have some rules that can't be autofixed. For this example, Pylint would say something like invalid-name, doesn't match pattern [a-z_][a-z0-9_]{2,40}

@MichaReiser
Copy link
Member

Thanks for explaining. That makes sense. I suspect that the fact that the rule overlaps with other existing and proposed rules will make it challenging to have this rule accepted. I'll keep this open as an alternative to consider when deciding whether Ruff should have more variable-length restricting rules.

@MichaReiser MichaReiser added rule Implementing or modifying a lint rule needs-design Needs further design before implementation needs-decision Awaiting a decision from a maintainer and removed needs-design Needs further design before implementation labels Mar 28, 2024
@astrowonk
Copy link
Author

Thanks. Sorry, I keep searching the rule list but haven't been able to find these: what are the other ruff rules that enforce variable length?

@MichaReiser
Copy link
Member

Yeah sorry. I should have linked to those. There was a PR for too-short-name that I closed. The rule overlaps with what you suggest and we may want to support the rule once #1774 is complete.

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