If a function name is 32 characters or longer, Pylint will display a snake_case warning.
Define a function with a name that is 32 characters or longer.
def this_is_a_very_lengthy_function_name():
pass
Run Pylint.
Pylint reports a snake_case warning:
C: 1, 0: Function name "this_is_a_very_lengthy_function_name" doesn't conform to snake_case naming style (invalid-name)
Pylint should not report a snake_case warning for a function name if it is properly capitalized.
No config file found, using default configuration
pylint 1.8.3,
astroid 1.6.2
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)]
Output
C0103:Method name "test_test_test_test_test_test_te" doesn't conform to snake_case naming style
~
def test_test_test_test_test_test_te():
pass
~
Output:
C0103:Function name "test_test_test_test_test_test_te" doesn't conform to snake_case naming style
~
No config file found, using default configuration
pylint 1.8.4,
astroid 1.6.3
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)]
~
Thanks for creating this issue! The problem here is two-fold:
As a solution, I think we can drop the maximum length from the regex, as long as a minimum length is respected, and we can do the length check separately so that we could augment the message by providing the actual reason of why the name failed the check.
Should unit-test functions be excluded from the length check?
A function name like test_logon_with_incorrect_password() seems perfectly reasonable, despite being 34 characters long.
I will be taking a look at this as part of Pycon2018
This issue is up for grabs.
If the expression is updated, this reference may need to be changed as well.
https://github.com/PyCQA/pylint/blob/9acb65af4c1072f16bbd8f60bd92c0b05c93be6e/man/pylint.1#L159-L160
In case it saves someone else a few minutes:
I overrode the default regex that is used to validate method names in my .pylintrc like this:
# Regular expression matching correct method names. Overrides method-naming-
# style
method-rgx=(([a-z_][a-z0-9_]{2,})|(_[a-z0-9_]*)|(__[a-z][a-z0-9_]+__))$
I got the regex from https://github.com/PyCQA/pylint/pull/2149/files. Thanks!
For anyone looking at this, the lines that need to be changed are in pylint/checkers/base.py.
There are 4 style classes: SnakeCaseStyle, CamelCaseStyle, PascalCaseStyle, and UpperCaseStyle. The Regex expressions for DEFAULT_NAME_RGX and CLASS_ATTRIBUTE_RGX both define a maximum length.
I'l like to take this.
Question is, if we're going to remove/increase upper limit for func names, probably the same changes should be done to attributes and especially arguments.
If everyone is ok with this, I'll work on PR
@3lnc Feel free to tackle this! Regarding your question, I'm not sure we have an upper limit for attributes or arguments.
@PCManticore Class attributes have the same character limit as method names.
class TestClass:
def __init__(self):
self.attribute_test_test_test_test_test = "d"
C: 3, 8: Attribute name "attribute_test_test_test_test_test" doesn't conform to snake_case naming style (invalid-name)
This limit is defined in CLASS_ATTRIBUTE_RGX.
Parameters do not appear to have any length limit.
class TestClass:
def __init__(self, param_test_test_test_test_test_test_test):
print(param_test_test_test_test_test_test_test)
@stevoisiak Yep, thanks! In that case we should also remove the limit for class attributes.
Is there a chance this fix will be ported back to the older versions of pylint, specifically those versions that still support python 2?
Unless someone backports the patch, most likely no.
Unless someone backports the patch, most likely no.
Anyone?
Most helpful comment
Should unit-test functions be excluded from the length check?
A function name like
test_logon_with_incorrect_password()seems perfectly reasonable, despite being 34 characters long.