80

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
1

3 Answers 3

109

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.

5
  • 8
    If it's constant, why do you want to reassign its value for each call to this method? It could be defined outside this method. Aug 7, 2014 at 14:29
  • 3
    @Maxime my idea is to have the method independent (not needing outside constants), even though it may take a performance tow because of reassignment of constants. Good point though.
    – gcamargo
    Aug 8, 2014 at 16:30
  • @user2457899 Actually, I also use constants in my methods. I was just trying to express the idea behind this pylint rule. :) Aug 9, 2014 at 19:02
  • 1
    I don't know, I think I'd rather have constants outside of functions. If it wasn't really a global constant, it should be a variable. Nov 27, 2016 at 2:55
  • @MaximeChéramy Can you express the idea behind why func_name = make_func(1,2,3) could be considered wrong? Current use case is vlen = numpy.vectorize(len) in utils module to work with ragged arrays in h5py.
    – AnnanFay
    Nov 5, 2017 at 13:40
1

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

and joining them by | and some parentheses.

Theoretically, you could also just take .*, but this would allow even invalid names like mixed_CASE.

0

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

1
  • What are these rules? In Python we have constants which are not at the module level and we have variables which are at module level. --- Because of the wrong assumptions in the "rules" Pylint shows wrong messages and encourages users to not follow PEP 8. Nov 12, 2022 at 11:16

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.