Pylint: Long function names detected as "snake_case" warning

Created on 26 Apr 2018  路  15Comments  路  Source: PyCQA/pylint

If a function name is 32 characters or longer, Pylint will display a snake_case warning.

Steps to reproduce

  1. Define a function with a name that is 32 characters or longer.

    def this_is_a_very_lengthy_function_name():
    pass

  2. Run Pylint.

Current behavior

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)

Expected behavior

Pylint should not report a snake_case warning for a function name if it is properly capitalized.

pylint --version output

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)]
bug contributor friendly

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.

All 15 comments

Confirmation of above, and with a method.

To reproduce

  1. >= 32 character method name
    ~
    class Test:
    def test_test_test_test_test_test_te(self):
    pass
    ~
  2. Run pylint

Current Behavior

Output

C0103:Method name "test_test_test_test_test_test_te" doesn't conform to snake_case naming style

And confirmation with a function:

~
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

pylint --version output:

~
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:

  • first is that we show a confusing message saying that a name doesn't conform to a style case, while in fact it does. We should say that it doesn't respect an implicit regular expression, which in this case is actually the length of the function
  • second is the arbitrary function length of 32 characters.

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.

https://github.com/PyCQA/pylint/blob/07aad4930fcf1f6683c0f467f20eef29966c10c5/pylint/checkers/base.py#L88-L94

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.

https://github.com/PyCQA/pylint/blob/07aad4930fcf1f6683c0f467f20eef29966c10c5/pylint/checkers/base.py#L94


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?

Was this page helpful?
0 / 5 - 0 ratings