Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pylint invalid constant name

I'm receiving a Pylint error regarding my constant: MIN_SOIL_PARTICLE_DENS (invalid name). Any ideas why this constant is wrong? Here's my full function:

def bulk_density(clay, sand, organic_matter):     MIN_SOIL_PARTICLE_DENS = 2.65     x1 = (0.078 + 0.278 * sand + 0.034 * clay + 0.022 * organic_matter - 0.018           * sand * organic_matter - 0.027 * clay * organic_matter - 0.584 * sand           * clay)     x2 = -0.107 + 1.636 * x1     field_capacity = vol_water_content_33_j_kg(clay, sand, organic_matter)#m3/m3     sat_water_content = 0.043 + field_capacity + x2 - 0.097 * sand     return (1 - sat_water_content) * MIN_SOIL_PARTICLE_DENS 
like image 574
gcamargo Avatar asked Aug 07 '14 13:08

gcamargo


People also ask

What is the default naming style of Pylint?

By default, Pylint will enforce PEP8 -suggested names. Pylint provides set of predefined naming styles. Those predefined naming styles may be used to adjust Pylint configuration to coding style used in linted project. Pylint provides predefined naming patterns for some names.

What does Pylint c0103 mean?

PyLint C0103 is a really helpful message to catch some really simple, obvious naming convention violations. However, it doesn’t catch everything that is defined within the official python naming conventions. For example, at time of writing, PyLint confuses a TypeVar as a Class name, and raises the naming convention message.

When does Pylint use the convention used by default?

Used when the name doesn’t conform to naming rules associated to its type (constant, variable, class…). Pylint by default uses the convention for names from PEP 8. In the following example all the conventions are violated.

Why are my variables named with uppercase letters in Pylint?

Pylint considers every variable that's defined at the module level as "constants" and it expects for them to be named with uppercase letters. There are multiple ways you can avoid this: you can customize the const-rgx option with a particular regex, as in --const-rgx= [a-z]+,...


2 Answers

When checking names, Pylint differentiates between constants, variables, classes etc. Any name that is not inside a function/class will be considered a constant, anything else is a variable.

See http://docs.pylint.org/features.html#basic-checker

variable-rgx:
[a-z_][a-z0-9_]{2,30}$

const-rgx:
(([A-Z_][A-Z0-9_]*)|(__.*__))$

Because you're in a function, MIN_SOIL_PARTICLE_DENS is (according to pylint) supposed to be a variable, pylint however treats it as a constant and therefore complains.

This means you can't have any uppercase names inside functions without pylint complaining.


If you ask me, using uppercase inside functions is fine; not all constants are necessarily defined globally.

like image 123
Reiner Gerecke Avatar answered Sep 19 '22 09:09

Reiner Gerecke


Few simple rules :

  1. Constants should be defined with UPPER_CASE letters only and should be defined at the module level
  2. Class names should be defined with CamelCase letters
  3. Variables should be defined at lower_case and should be defined inside function, classes etc.

Now lets talk about your case,

MIN_SOIL_PARTICLE_DENS is defined inside a function and should have lower letters only. Thus instead of considering MIN_SOIL_PARTICLE_DENS as a constant, pylint considers it as a variable here and hence the pylint error.

Pylint Tutorial

like image 35
Vishvajit Pathak Avatar answered Sep 21 '22 09:09

Vishvajit Pathak