Skip to content

Remove Str From Fstring

Sourcery refactoring id: remove-str-from-fstring

Description

Remove unnecessary calls to str() within formatted values in f-strings

Before

def description(name: str, age: int):
    return f"Name: {str(name)}, Age: {str(age)}"

After

def description(name: str, age: int):
    return f"Name: {name}, Age: {age}"

Explanation

Calls to str() are normally unnecessary within the formatted values of f-strings, since this conversion is already implicitly made. Removing them tidies up the code slightly and will yield a small performance improvement. You do need to call str() on some types with custom __repr__() methods, so this refactoring only triggers where Sourcery has inferred the type to be suitable.