Reputation: 3971
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
Upvotes: 80
Views: 98744
Reputation: 29434
I found this behavior annoying, but there's a way to configure pylint to avoid this!
Merge the following ini-style declaration into your .pylintrc
file:
[BASIC]
variable-rgx=((([a-z_][a-z0-9_]{2,})|(_[a-z0-9_]*)|(__[a-z][a-z0-9_]+__))$)|([A-Z_][A-Z0-9_]+$)
I built this regex by taking
the default snake_case regex taken from pylint's source at this line,
the default CONST_VAR regex taken from pylint's source at this line
and joining them by |
and some parentheses.
Theoretically, you could also just take .*
, but this would allow even invalid names like mixed_CASE
.
Upvotes: 1
Reputation: 3731
Few simple rules :
UPPER_CASE
letters only and should be defined at the module levelCamelCase
letterslower_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.
Upvotes: 1
Reputation: 12214
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.
Upvotes: 110