Pregunta

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)

¿Fue útil?

Solución

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.

Otros consejos

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.

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.

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top