Default Rules¶
Sourcery ships with more than 160 default rules. These are divided into three categories:
- refactorings
- suggestions
- comments / no-replacement rules
Use the navigation menu to browse the complete list of all the default rules.
Refactorings¶
Refactorings are rules which change code structure without changing its behaviour. For example, Sourcery will recommend you Refactor this code
my_list = [i for i in range(10)]
into this code
my_list = list(range(10))
This is a Refactoring because the code's behaviour will not change, but is
simpler and therefore easier to understand. Sourcery will automatically apply
this Refactoring when it can. This refactoring is called
inline-variable
.
Suggestions¶
Suggestions are rules which change code structure, but may change its behaviour. Suggestions are used to guide best practices, but, by default, Sourcery will not automatically fix them, and you must apply the fix manually. For example, Sourcery will make a Suggestion to change
def func(hats: list = []):
change(hats)
into
def func(hats: list = None):
if hats is None:
hats = []
change(hats)
This change is a Suggestion because using mutable defaults in function
definitions is likely an error, but is not applied automatically because doing
so may change your code's behaviour. This suggestion is called
default-mutable-arg
.
Comments / No-Replacement Rules¶
No-replacement rules are rules for specific code quality issues which have no clear or conventional solution. There may be no solution for several reasons, such as because the issue is complicated, or because it may be necessary to rename variables (for which some application knowledge is necessary). For example, Sourcery will flag an issue without providing a replacement on the following code:
list = [1, 1, 2, 3, 5, 8]
This code is problematic, because assigning to the built-in variable list
is confusing
and likely to lead to bugs. But Sourcery doesn't know what to call the variable
instead. This rule is known as
avoid-builtin-shadow
.
Hint
Each rule is automatically tagged with its type - you can run only that type of rule
using the --enable
option.