Search code examples
pythonpylintpep8namedtuple

Should namedtuples follow constant name conventions in python?


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)


Solution

  • 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.