-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuse-fstring.yaml
47 lines (41 loc) · 1.44 KB
/
use-fstring.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# This file contains optional rules
# that spot when f-string should be used
# instead of an older string formatting mechanism.
#
# Related default rules:
#
# https://docs.sourcery.ai/Reference/Default-Rules/refactorings/use-fstring-for-concatenation/
# https://docs.sourcery.ai/Reference/Default-Rules/refactorings/use-fstring-for-formatting/
rules:
# You can use this rule in the Sourcery CLI with the --enable option:
#
# sourcery review --enable no-str-format .
#
# To review with this rule and the related default f-string rules:
#
# sourcery review --enable use-fstring .
#
- id: no-str-format
description: Use f-string instead of `str.format`
explanation: |
f-string is less verbose and faster than `str.format()`
f-string was introduced to use the same syntax and machinery as `str.format()`, but solve some of its issues:
* Verbosity.
* The placeholder and the replacement value are often far away from each other.
See the [Rationale section of PEP-498](https://peps.python.org/pep-0498/#rationale).
pattern: ${text}.format(...)
condition: text.has_type("str")
tests:
- match: "'{0}. place'.format(placement)"
- match: "'{0}. chapter {1}'.format(nr, title)"
- match: |
template = "{0}. place"
template.format(placement)
- no-match: |
sth = CustomObject()
sth.format(placement)
rule_tags:
use-fstring:
- no-str-format
- use-fstring-for-concatenation
- use-fstring-for-formatting