Sourcery can now propose refactorings to extract blocks of code into new functions. This currently proposes in two circumstances:
This feature will be available in the upcoming Pro subscription.
Select the project or a folder within a project in the IDE plugins, and then have Sourcery scan every file within it. All refactoring suggestions will be displayed as they are found in the Problems window.
This makes it easy to refactor a large portion of the codebase quickly without manually having to visit every file to see the Sourcery suggestions.
This feature will be available in the upcoming Pro subscription.
Previously when aggregating metrics we simply calculated metrics for each function and then averaged them. This did not account for the differing size of functions - a 100 line function has more impact on quality than a 10 line one.
We now weight each metric (apart from method length) by the number of lines in each method when averaging them. This gives a better view of the actual file and project-level quality.
Hoist code from an else
after a guard clause. This will happen where the main
body of an if
contains only a raise or return statement, and there are
multiple statements in the else
.
def f(a=None):
if a is None:
return 42
else:
# some long statement
var = (i**2 for i in range(a))
return sum(var)
is converted into:
def f(a=None):
if a is None:
return 42
# some long statement
var = (i**2 for i in range(a))
return sum(var)
Where a value is set on each branch of an if
and then immediately returned,
instead return it directly from each branch.
def f():
if condition:
val = 42
else:
val = 0
return val
is converted into:
def f():
if condition:
return 42
else:
return 0
Use with sourcery refactor --in-place
to backup changed files with given
suffix:
sourcery refactor --in-place --backup .bak main.py
If any refactorings are found in main.py
a copy will be made at main.py.bak
before it is updated in place.
for-index-replacement
for lists, as it's possible __len__
and
__getitem__
are implemented but __iter__
is not.swap-if-else refactoring
. This will now also be
triggered where it can enable the above remove-else-after-guard
refactoring.
The description has also changed to reflect this.