@staticmethod
PYL-R0201The method doesn't use its bound instance. Decorate this method with @staticmethod
decorator, so that Python does not have to instantiate a bound method for every instance of this class thereby saving memory and computation. Read more about staticmethods here.
The preferred way is to add a new DB column with null=True
because it will be created instantly and then possibly populate the table with the desired default values. Adding a default value will lead to a performance issue if the existing table has a large number of rows.
An expression that is not a function call is assigned to nothing. Probably something else was intended here. We recommend to review this.
Using the literal syntax can give minor performance bumps compared to using function calls to create dict
, list
and tuple
.
len
used as condition PYL-C1802Using the len
function to check if a sequence is empty is not idiomatic and can be less performant than checking the truthiness of the object. len
doesn't know the context in which it is called, so if computing the length means traversing the entire sequence, it must; it doesn't know that the result is just being compared to 0.
The logging statement has the call of the form logging.(format_string % (format_args...))
. For such calls, it is recommended to leave string interpolation to the logging method itself and be written as logging.(format_string, format_args...)
so that the program may avoid incurring the cost of the interpolation in those cases in which no message will be logged. For more details, see PEP 282.
The built-in function being used does not require comprehension and can work directly with a generator expression.
join
PYL-R1713Consider using str.join(sequence)
instead of joining the elements of a sequence using for
loop iteration.
str.join(sequence)
is faster, uses less memory and increases readability compared to a for
loop iteration.
in
PYL-R1714To check if a variable is equal to one of many values, combine the values into a tuple and check if the variable is contained in
it instead of checking for equality against each of the values.
This is faster, less verbose, and more readable.
Although there is nothing syntactically wrong with this code, it is hard to read and can be simplified to a dict comprehension. Using dictionary comprehension is more performant since there is no need to create a transient list.
Although there is nothing syntactically wrong with this code, it is hard to read and can be simplified to a set comprehension. Using set comprehension is more performant since there is no need to create a transient list.
It is unnecessary to use a comprehension just to loop over the iterable
and create a list
/set
/dict
out of it. Python has a specialized set of tools for this task: the list
/set
/dict
constructors, which are faster and more readable.
len(seq) - 1
to get last element of an iterable PTC-W0044There’s no need to calculate length of an iterable in order to fetch the last element of the iterable. You can provide a negative index -1
to it directly in orger to get the last element. In this way, you don't have to iterate over the sequence using len
to get the last index when your purpose is only to get the last element.
Using a container in place of a generator for a calls that can accept both, slows down the performance. Consider using generators for all function calls which accept both containers and genertors.
f-strings are the fastest way to format strings as compared to the following methods: * using format specifiers %
Formatting the message manually before passing it to a logging
call does unnecessary work if logging in disabled. Consider using the logging
module's built-in formatting features to avoid that.
Formatting the message manually before passing it to a logging
call does unnecessary work if logging is disabled. Consider using the logging
module's built-in formatting features to avoid that.