You can now use Sourcery straight away on installation, with no need to create an account or enter a token. This gives you access to our Free tier. To try out our Pro features you can create an account to get a 30 day free trial.
equality-identity
Simplify equality comparisons that are always True
or False
if 1 == 1:
always_do_this()
if 1 != 1:
never_do_this()
if True:
always_do_this()
if False:
never_do_this()
When comparing a value to itself, the outcome will always be True
, as long as
the equality operator has not been overridden, and this also holds for the
opposite comparison and False
. It is more readable to use a direct comparison
to the boolean value.
identity-comprehension
Convert list/set/tuple comprehensions that do not change the input elements into
# List comprehensions
[item for item in coll]
[item for item in friends.names()]
# Dict comprehensions
{k: v for k, v in coll}
{k: v for k, v in coll.items()} # Only if we know coll is a `dict`
# Unneeded call to `.items()`
dict(coll.items()) # Only if we know coll is a `dict`
# Set comprehensions
{item for item in coll}
# List comprehensions
list(iter(coll))
list(iter(friends.names()))
# Dict comprehensions
dict(coll)
dict(coll)
# Unneeded call to `.items()`
dict(coll)
# Set comprehensions
set(coll)
All these comprehensions are just creating a copy of the original collection. They can all be simplified by simply constructing a new collection directly. The resulting code is easier to read and shows the intent more clearly.
merge-assign-and-aug-assign
Replaces an assignment and an augmented assignment with a single assignment.
other_value = 33
number = 42
number += other_value
other_value = 33
number = 42 + other_value
When we mutate a variable multiple times without reading or writing its value in between, it's more readable and more efficient to change its value only once. This way, it's clearer which values this variable can have at various points.
This refactoring works with all 4 augmented assignment operators:
+=
-=
*=
/=
remove-str-from-print
Removes unnecessary calls to str()
from within print()
print(str(1))
print(1)
Objects passed into calls to the print()
function already have str()
called
on them, so it is not required to do so yourself.
simplify-division
Use Python's built-in feature for succinct division syntax.
result = int(42 / 10)
result = 42 // 10
result = 42 // 10
remainder = 42 % 10
result, remainder = divmod(42, 10)
Python has some great features to simplify division expressions. If you're
interested only in the whole number component of the quotient, you can use the
//
integer division operator. If you want to have the whole number component
and the remainder in separate variables, the built-in divmod
function comes
handy.
simplify-substring-search
Simplify finding if substrings are present in strings by using in
my_str = "Hello world"
if my_str.find("ello") == -1:
print("Not Found!")
my_str = "Hello world"
if "ello" not in my_str:
print("Not Found!")
my_str = "Hello world"
if my_str.count("ello") > 0:
print("Found!")
my_str = "Hello world"
if "ello" in my_str:
print("Found!")
Making use of Python's in
operator for detecting if a substring is present in
a string is more readable than using the find
or count
methods, and is also
suggested in the
documentation. These
methods are more suitable for cases where you need more information about the
substring's location or the number of times it appears.
if
conditions with named expresssions could
change behaviour due to missing brackets.hoist-if-from-if
proposal from hoisting ifs that would then be
incorrectly entered