11

I've written a small python module where I use a couple of namedtuples to pass info around because I find them very expressive. I considered these types and named them following the PEP8 convention for class names (CamelCased). However pylint sees the line:

PersonData = collections.namedtuple('PersonData', 'name surname age')

at the module's global scope and goes: Invalid constant name 'PersonData'.

Am I misusing namedtuples? What's the pythonic recommendation? I can only think of suppressing the warning, renaming the structure to PERSON_DATA, or making it a full class. Note that, in my case, it wouldn't make sense for it to have methods though.

If the answer is to suppress the warning. Wouldn't this be a recurring problem with pylint vs named tuples?

(using pylint-0.26.0, python-2.7.4)

Luis
  • 859
  • 2
  • 9
  • 20

4 Answers4

6

This issue has been solved in newer versions of pylint.

My system was picking the version from the ubuntu (13.04) repositories (pylint-0.26.0). Using a virtualenv I could pip install pylint-1.0.0 which recognizes named tuples and will actually enforce using the same format as classes.

Note that it won't pick it up if you're doing string manipulation on the fields string. For example:

PersonData = collections.namedtuple('PersonData', 'name ' + 'surname age')

will still cause pylint-1.0.0 to spit out the error code for invalid constant name. In this case the only workaround is to disable the warning as per Martijn Pieters suggestion.

Community
  • 1
  • 1
Luis
  • 859
  • 2
  • 9
  • 20
  • 8
    "solved in newer versions of pylint" What? I'm at the latest Pylint version (1.4.0) and I'm still getting this error. – Meredith Jan 10 '15 at 13:28
  • I just tried in different virtualenvs with 0.26.0, 1.0.0 and 1.4.0 and it still shows the same results for me: the oldest version wants CAPITAL_CASE, and the two newer CamelCase. I suggest you try again (watch out for custom .pylintrc in your homedir) and register a regression bug report with the devs. – Luis Jan 12 '15 at 12:53
  • 3
    I too am getting this error with pylint 1.4.0 and the default configuration – simon Apr 03 '15 at 22:44
  • I still see it in pylint3 1.6.5. – ʇsәɹoɈ Nov 15 '17 at 22:58
5

You can ignore pylint here, you are using the camel case naming convention exactly right.

You can suppress the warning:

PersonData = collections.namedtuple('PersonData', 'name surname age')  # pylint: disable-msg=C0103

namedtuple is a class factory, so use the naming conventions for a class.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 3
    Just one small thing: i prefer pylint inline comments in this format: # pylint: disable=invalid-name More readable and obvious about what whas overriden. – kert Jul 21 '15 at 01:36
1

This has been fixed in pylint-1.0.0 (see Pylint's Changelog):

For toplevel name assignment, the class name regex will be used if pylint can detect that value on the right-hand side is a class (like collections.namedtuple()).

Note that the namedtuple must be defined on the toplevel, if defined e.g. within a function pylint will still give a invalid-name warning.

dsandbrink
  • 11
  • 2
0

in such case pylint should detect the name is assigned to a class an use the class name regexp.

Please submit a ticket on http://bitbucket.org/logilab/pylint

sthenault
  • 14,397
  • 5
  • 38
  • 32