forked from pylint-dev/pylint
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds a checker for normal strings which are formatted with `.format()` or '%'. The message is a convention to nudge users towards using f-strings. This closes pylint-dev#3592
- Loading branch information
1 parent
441ec27
commit f93b21f
Showing
5 changed files
with
211 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
"""Test to see if a f-string would be possible and consider-using-f-string should be raised""" | ||
# pylint: disable=unused-variable, invalid-name, missing-function-docstring, pointless-statement | ||
# pylint: disable=expression-not-assigned, repeated-keyword | ||
|
||
PARAM_1 = PARAM_2 = PARAM_3 = 1 | ||
PARAM_LIST = [PARAM_1, PARAM_2, PARAM_3] | ||
PARAM_LIST_SINGLE = [PARAM_1] | ||
PARAM_DICT = {"Param_1": PARAM_1, "Param_2": PARAM_2, "Param_3": PARAM_3} | ||
PARAM_DICT_SINGLE = {"Param_1": PARAM_1} | ||
|
||
|
||
def return_parameter(): | ||
return PARAM_1 | ||
|
||
|
||
def return_list(): | ||
return PARAM_LIST | ||
|
||
|
||
def return_dict(): | ||
return PARAM_DICT | ||
|
||
|
||
def print_good(): | ||
print("String {}, {} or {}".format(*PARAM_LIST)) | ||
print("String {}, {}, {} or {}".format(*PARAM_LIST_SINGLE, *PARAM_LIST)) | ||
print("String {Param}, {}, {} or {}".format(Param=PARAM_1, *PARAM_LIST)) | ||
print("String {Param} {Param}".format(Param=PARAM_1)) | ||
print("{Param_1} {Param_2}".format(**PARAM_DICT)) | ||
print("{Param_1} {Param_2} {Param_3}".format(**PARAM_DICT_SINGLE, **PARAM_DICT)) | ||
print("{Param_1} {Param_2} {Param_3}".format(Param_1=PARAM_1, **PARAM_DICT)) | ||
print("{Param_1} {Param_2}".format(**PARAM_DICT)) | ||
print("{Param_1} {Param_2}".format(**return_dict())) | ||
print("%(Param_1)s %(Param_2)s" % PARAM_LIST) | ||
print("%(Param_1)s %(Param_2)s" % PARAM_DICT) | ||
print("%(Param_1)s %(Param_2)s" % return_dict()) | ||
print("{a[Param_1]}{a[Param_2]}".format(a=PARAM_DICT)) | ||
|
||
def print_bad(): | ||
print("String %f" % PARAM_1) # [consider-using-f-string] | ||
print("String {}".format(PARAM_1)) # [consider-using-f-string] | ||
print("String {Param_1}".format(Param_1=PARAM_1)) # [consider-using-f-string] | ||
print("{} {}".format(PARAM_1, PARAM_2)) # [consider-using-f-string] | ||
print("{Par_1}{Par_2}".format(Par_1=PARAM_1, Par_2=PARAM_2)) # [consider-using-f-string] | ||
print("{Param_1}".format(*PARAM_LIST_SINGLE)) # [consider-using-f-string] | ||
print("{Param_1}".format(**PARAM_DICT_SINGLE)) # [consider-using-f-string] | ||
print("String %s" % (PARAM_1)) # [consider-using-f-string] | ||
print("String %s %s" % (PARAM_1, PARAM_2)) # [consider-using-f-string] | ||
print("String %s" % (PARAM_LIST_SINGLE)) # [consider-using-f-string] | ||
|
||
|
||
def statement_good(): | ||
"String {}, {} or {}".format(*PARAM_LIST) | ||
"String {}, {}, {} or {}".format(*PARAM_LIST_SINGLE, *PARAM_LIST) | ||
"String {Param}, {}, {} or {}".format(Param=PARAM_1, *PARAM_LIST) | ||
"String {Param} {Param}".format(Param=PARAM_1) | ||
"{Param_1} {Param_2}".format(**PARAM_DICT) | ||
"{Param_1} {Param_2} {Param_3}".format(**PARAM_DICT_SINGLE, **PARAM_DICT) | ||
"{Param_1} {Param_2} {Param_3}".format(Param_1=PARAM_1, **PARAM_DICT) | ||
"{Param_1} {Param_2}".format(**PARAM_DICT) | ||
"{Param_1} {Param_2}".format(**return_dict()) | ||
"%(Param_1)s %(Param_2)s" % PARAM_LIST | ||
"%(Param_1)s %(Param_2)s" % PARAM_DICT | ||
"%(Param_1)s %(Param_2)s" % return_dict() | ||
"{a[Param_1]}{a[Param_2]}".format(a=PARAM_DICT) | ||
|
||
def statement_bad(): | ||
"String %f" % PARAM_1 # [consider-using-f-string] | ||
"String {}".format(PARAM_1) # [consider-using-f-string] | ||
"String {Param_1}".format(Param_1=PARAM_1) # [consider-using-f-string] | ||
"{} {}".format(PARAM_1, PARAM_2) # [consider-using-f-string] | ||
"{Par_1}{Par_2}".format(Par_1=PARAM_1, Par_2=PARAM_2) # [consider-using-f-string] | ||
"{Param_1}".format(*PARAM_LIST_SINGLE) # [consider-using-f-string] | ||
"{Param_1}".format(**PARAM_DICT_SINGLE) # [consider-using-f-string] | ||
"String %s" % (PARAM_1) # [consider-using-f-string] | ||
"String %s %s" % (PARAM_1, PARAM_2) # [consider-using-f-string] | ||
"String %s" % (PARAM_LIST_SINGLE) # [consider-using-f-string] | ||
|
||
|
||
def assignment_good(): | ||
A = "String {}, {} or {}".format(*PARAM_LIST) | ||
B = "String {}, {}, {} or {}".format(*PARAM_LIST_SINGLE, *PARAM_LIST) | ||
C = "String {Param}, {}, {} or {}".format(Param=PARAM_1, *PARAM_LIST) | ||
D = "String {Param} {Param}".format(Param=PARAM_1) | ||
E = "{Param_1} {Param_2}".format(**PARAM_DICT) | ||
F = "{Param_1} {Param_2} {Param_3}".format(**PARAM_DICT_SINGLE, **PARAM_DICT) | ||
G = "{Param_1} {Param_2} {Param_3}".format(Param_1=PARAM_1, **PARAM_DICT) | ||
H = "{Param_1} {Param_2}".format(**PARAM_DICT) | ||
I = "{Param_1} {Param_2}".format(**return_dict()) | ||
J = "%(Param_1)s %(Param_2)s" % PARAM_LIST | ||
K = "%(Param_1)s %(Param_2)s" % PARAM_DICT | ||
L = "%(Param_1)s %(Param_2)s" % return_dict() | ||
M = "{a[Param_1]}{a[Param_2]}".format(a=PARAM_DICT) | ||
|
||
|
||
def assignment_bad(): | ||
a = "String %f" % PARAM_1 # [consider-using-f-string] | ||
b = "String {}".format(PARAM_1) # [consider-using-f-string] | ||
c = "String {Param_1}".format(Param_1=PARAM_1) # [consider-using-f-string] | ||
d = "{} {}".format(PARAM_1, PARAM_2) # [consider-using-f-string] | ||
e = "{Par_1}{Par_2}".format(Par_1=PARAM_1, Par_2=PARAM_2) # [consider-using-f-string] | ||
f = "{Param_1}".format(*PARAM_LIST_SINGLE) # [consider-using-f-string] | ||
g = "{Param_1}".format(**PARAM_DICT_SINGLE) # [consider-using-f-string] | ||
h = "String %s" % (PARAM_1) # [consider-using-f-string] | ||
i = "String %s %s" % (PARAM_1, PARAM_2) # [consider-using-f-string] | ||
j = "String %s" % (PARAM_LIST_SINGLE) # [consider-using-f-string] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
consider-using-f-string:40:10::"Formatting a regular string which could be a f-string":HIGH | ||
consider-using-f-string:41:10::"Formatting a regular string which could be a f-string":HIGH | ||
consider-using-f-string:42:10::"Formatting a regular string which could be a f-string":HIGH | ||
consider-using-f-string:43:10::"Formatting a regular string which could be a f-string":HIGH | ||
consider-using-f-string:44:10::"Formatting a regular string which could be a f-string":HIGH | ||
consider-using-f-string:45:10::"Formatting a regular string which could be a f-string":HIGH | ||
consider-using-f-string:46:10::"Formatting a regular string which could be a f-string":HIGH | ||
consider-using-f-string:47:10::"Formatting a regular string which could be a f-string":HIGH | ||
consider-using-f-string:48:10::"Formatting a regular string which could be a f-string":HIGH | ||
consider-using-f-string:49:10::"Formatting a regular string which could be a f-string":HIGH | ||
consider-using-f-string:68:4::"Formatting a regular string which could be a f-string":HIGH | ||
consider-using-f-string:69:4::"Formatting a regular string which could be a f-string":HIGH | ||
consider-using-f-string:70:4::"Formatting a regular string which could be a f-string":HIGH | ||
consider-using-f-string:71:4::"Formatting a regular string which could be a f-string":HIGH | ||
consider-using-f-string:72:4::"Formatting a regular string which could be a f-string":HIGH | ||
consider-using-f-string:73:4::"Formatting a regular string which could be a f-string":HIGH | ||
consider-using-f-string:74:4::"Formatting a regular string which could be a f-string":HIGH | ||
consider-using-f-string:75:4::"Formatting a regular string which could be a f-string":HIGH | ||
consider-using-f-string:76:4::"Formatting a regular string which could be a f-string":HIGH | ||
consider-using-f-string:77:4::"Formatting a regular string which could be a f-string":HIGH | ||
consider-using-f-string:97:8::"Formatting a regular string which could be a f-string":HIGH | ||
consider-using-f-string:98:8::"Formatting a regular string which could be a f-string":HIGH | ||
consider-using-f-string:99:8::"Formatting a regular string which could be a f-string":HIGH | ||
consider-using-f-string:100:8::"Formatting a regular string which could be a f-string":HIGH | ||
consider-using-f-string:101:8::"Formatting a regular string which could be a f-string":HIGH | ||
consider-using-f-string:102:8::"Formatting a regular string which could be a f-string":HIGH | ||
consider-using-f-string:103:8::"Formatting a regular string which could be a f-string":HIGH | ||
consider-using-f-string:104:8::"Formatting a regular string which could be a f-string":HIGH | ||
consider-using-f-string:105:8::"Formatting a regular string which could be a f-string":HIGH | ||
consider-using-f-string:106:8::"Formatting a regular string which could be a f-string":HIGH |