Beta builds, refactorings and bug fixes
Beta build sign-up
You can now sign up on the website to get access to Sourcery’s beta updates. Just log in, go to your profile page and check the box under the ‘Nightly builds’ heading.
This will let you try out our latest refactorings and fixes the day they become available, rather than waiting for our plugin releases. Each morning when a build is available it will get automatically downloaded, then will prompt you to restart your IDE to get the new features.
New refactorings
Remove redundant f-string
Sourcery refactoring id: remove-redundant-fstring
Description:
Replace f-string with no interpolated values with string.
Before:
description = f"This is a totally normal string"
After:
description = "This is a totally normal string"
Explanation:
This refactoring spots cases where use of an f-string is unnecessary since there
are no interpolated values. It’s easy to forget to remove the f
if you have
such a value and then remove it later.
Simplify f-string formatting
Sourcery refactoring id: simplify-fstring-formatting
Description:
Simplify unnecessary nesting, casting and constant values in f-strings
Before:
description = f"This {1} thing is {f'{str(adjective)}'}"
After:
description = f"This 1 thing is {adjective}"
Explanation:
When writing an f-string it is not necessary to interpolate the values of constants or cast variables to strings - these will be done automatically. It is also not necessary to nest f-strings when adding the contents of the nested string directly achieves the same result.
Ternary to If Expression
Sourcery refactoring id: ternary-to-if-expression
Description:
Replace boolean ternary with inline if expression
Before:
protocol_type = uses_ssl() and "https" or "http"
After:
protocol_type = "https" if uses_ssl() else "http"
Explanation:
In some cases, especially in old codebases that were written in Python 2, boolean expressions are used as ternary operators. However, replacing boolean ternary expressions with inline ifs where appropriate better portrays the code’s intention and therefore makes it easier for others to read it.
Enhancements
- Sourcery can now provide refactorings inside f-string format values
- Extracted methods will now be prefixed with a TODO comment reminding you to
rename them in each place they are used:
# TODO Rename this here and in [first_usage] and [second_usage]
- Extended
assign-if-exp
to cover the case where the default value is assigned first
Before:
x = 2
if condition:
x = 1
After:
x = 1 if condition else 2
default-get
refactorings now work for if expressions- Sourcery will now show a notification when you first successfully log in.
Bug fixes
- Sourcery will now provide metrics and refactorings on functions containing named expressions
use-any
will no longer trigger if it introduces a short-circuit where global state is writtenmerge-repeated-if
will no longer trigger if the firstif
writes global state that could affect the seconduse-any
will no longer trigger if the for loop target is used later on