Why is using len (SEQUENCE) in condition values ​​considered incorrect Pylint?

Given this piece of code:

from os import walk files = [] for (dirpath, _, filenames) in walk(mydir): # more code that modifies files if len(files) == 0: # <-- C1801 return None 

I was alarmed by Pilint with this message regarding a line with an if statement:

[pylint] C1801: Do not use len(SEQUENCE) as a condition value

Rule C1801, at first glance, seemed to me not very reasonable, and the definition in the reference guide does not explain why this is a problem. In fact, he expressly calls it misuse.

len-as-condition (C1801) : Do not use len(SEQUENCE) as a condition value. Used when Pylint detects improper use of len (sequence) inside conditions.

My search attempts also did not give me a deeper explanation. I understand that the length property of a sequence can be lazily evaluated and that __len__ can be programmed for side effects, but it is doubtful whether this is enough enough for Pilint to call this use incorrect. Therefore, before I simply configure my project to ignore the rule, I would like to know if I am missing something in my reasoning.

When is using len(SEQ) as a condition value problematic? What are some key situations Pylint is trying to avoid with the C1801?

+157
python conditional pylint
Mar 30 '17 at 14:52
source share
4 answers

When is using len(SEQ) as a condition value problematic? What are the main situations Pylint is trying to avoid with the C1801?

Using len(SEQUENCE) is not very problematic - although this may not be as effective (see the comment for chats ). Regardless, Pylint checks the code for compliance with the PEP 8 style guide , which states that

For sequences (rows, lists, tuples), use the fact that empty sequences are false.

 Yes: if not seq: if seq: No: if len(seq): if not len(seq): 

As an occasional Python programmer that moves between languages, Id considers the len(SEQUENCE) construct to be more readable and explicit ("Explicit is better than implicit"). However, using the fact that an empty sequence evaluates to False in a Boolean context is considered more "Pythonic".

+206
Apr 18 '17 at 15:49
source share
— -

Note that using len (seq) is actually required (instead of checking the bool seq value) when using NumPy arrays.

 a = numpy.array(range(10)) if a: print "a is not empty" 

throws an exception: ValueError: The true value of an array with more than one element is ambiguous. Use a.any () or a.all ()

And therefore, for code that uses both Python lists and NumPy arrays, message C1801 is less useful.

+32
May 8 '17 at 20:46
source share

Pylint failed for my code, and research led me to this post:

 ../filename.py:49:11: C1801: Do not use 'len(SEQUENCE)' to determine if a sequence is empty (len-as-condition) ../filename.py:49:34: C1801: Do not use 'len(SEQUENCE)' to determine if a sequence is empty (len-as-condition) 

This has been my code before:

 def list_empty_folders(directory): """The Module Has Been Build to list empty Mac Folders.""" for (fullpath, dirnames, filenames) in os.walk(directory): if len(dirnames) == 0 and len(filenames) == 0: print("Exists: {} : Absolute Path: {}".format( os.path.exists(fullpath), os.path.abspath(fullpath))) 

This was after fixing my code. Using attribute int() , I seem to satisfy Pep8 / Pylint and does not seem to have a negative effect on my code:

 def list_empty_folders(directory): """The Module Has Been Build to list empty Mac Folders.""" for (fullpath, dirnames, filenames) in os.walk(directory): if len(dirnames).__trunc__() == 0 and len(filenames).__trunc__() == 0: print("Exists: {} : Absolute Path: {}".format( os.path.exists(fullpath), os.path.abspath(fullpath))) 

My fix

By adding .__trunc__() to the sequence, it seems to have exhausted the need.

I do not see differences in behavior, but if someone knows the features that I am missing, please let me know.

0
Apr 3 '19 at 19:31
source share

In the next version of Pylint, you no longer need to complain after fixing https://github.com/PyCQA/pylint/issues/2684 and https://github.com/PyCQA/pylint/issues/1405

Thanks to PaulRenvoise , PCManticore and adhearn for their work in fixing this error !

For example, if len(files) == 0 no longer causes a Pylint complaint.

0
Apr 16 '19 at 7:21
source share



All Articles