Skip to content

Commit 38d26aa

Browse files
authored
Remove unnecessary concatenated strings (#18044)
We have some strings which use implicit concatenation unnecessarily (where the whole string can fit on a single line without implicit concatenation). This PR removes them. For some of the changed lines here, I also did some driveby modernisations to use f-strings, since I was touching those lines anyway.
1 parent 52faccc commit 38d26aa

File tree

10 files changed

+45
-52
lines changed

10 files changed

+45
-52
lines changed

mypy/checkstrformat.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,10 @@ def check_specs_in_format_call(
393393
# If the explicit conversion is given, then explicit conversion is called _first_.
394394
if spec.conversion[1] not in "rsa":
395395
self.msg.fail(
396-
'Invalid conversion type "{}",'
397-
' must be one of "r", "s" or "a"'.format(spec.conversion[1]),
396+
(
397+
f'Invalid conversion type "{spec.conversion[1]}", '
398+
f'must be one of "r", "s" or "a"'
399+
),
398400
call,
399401
code=codes.STRING_FORMATTING,
400402
)
@@ -472,8 +474,7 @@ def find_replacements_in_call(self, call: CallExpr, keys: list[str]) -> list[Exp
472474
expr = self.get_expr_by_position(int(key), call)
473475
if not expr:
474476
self.msg.fail(
475-
"Cannot find replacement for positional"
476-
" format specifier {}".format(key),
477+
f"Cannot find replacement for positional format specifier {key}",
477478
call,
478479
code=codes.STRING_FORMATTING,
479480
)
@@ -654,8 +655,9 @@ class User(TypedDict):
654655
assert spec.key, "Call this method only after auto-generating keys!"
655656
assert spec.field
656657
self.msg.fail(
657-
"Invalid index expression in format field"
658-
' accessor "{}"'.format(spec.field[len(spec.key) :]),
658+
'Invalid index expression in format field accessor "{}"'.format(
659+
spec.field[len(spec.key) :]
660+
),
659661
ctx,
660662
code=codes.STRING_FORMATTING,
661663
)

mypy/inspections.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,8 +469,7 @@ def missing_type(self, expression: Expression) -> str:
469469

470470
def missing_node(self, expression: Expression) -> str:
471471
return (
472-
f'Cannot find definition for "{type(expression).__name__}"'
473-
f" at {expr_span(expression)}"
472+
f'Cannot find definition for "{type(expression).__name__}" at {expr_span(expression)}'
474473
)
475474

476475
def add_prefixes(self, result: str, expression: Expression) -> str:

mypy/main.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -552,15 +552,16 @@ def add_invertible_flag(
552552
)
553553
config_group.add_argument(
554554
"--config-file",
555-
help="Configuration file, must have a [mypy] section "
556-
"(defaults to {})".format(", ".join(defaults.CONFIG_FILES)),
555+
help=(
556+
f"Configuration file, must have a [mypy] section "
557+
f"(defaults to {', '.join(defaults.CONFIG_FILES)})"
558+
),
557559
)
558560
add_invertible_flag(
559561
"--warn-unused-configs",
560562
default=False,
561563
strict_flag=True,
562-
help="Warn about unused '[mypy-<pattern>]' or '[[tool.mypy.overrides]]' "
563-
"config sections",
564+
help="Warn about unused '[mypy-<pattern>]' or '[[tool.mypy.overrides]]' config sections",
564565
group=config_group,
565566
)
566567

@@ -589,8 +590,7 @@ def add_invertible_flag(
589590
"--python-executable",
590591
action="store",
591592
metavar="EXECUTABLE",
592-
help="Python executable used for finding PEP 561 compliant installed"
593-
" packages and stubs",
593+
help="Python executable used for finding PEP 561 compliant installed packages and stubs",
594594
dest="special-opts:python_executable",
595595
)
596596
imports_group.add_argument(
@@ -623,8 +623,7 @@ def add_invertible_flag(
623623
"--platform",
624624
action="store",
625625
metavar="PLATFORM",
626-
help="Type check special-cased code for the given OS platform "
627-
"(defaults to sys.platform)",
626+
help="Type check special-cased code for the given OS platform (defaults to sys.platform)",
628627
)
629628
platform_group.add_argument(
630629
"--always-true",
@@ -655,8 +654,7 @@ def add_invertible_flag(
655654
"--disallow-any-decorated",
656655
default=False,
657656
action="store_true",
658-
help="Disallow functions that have Any in their signature "
659-
"after decorator transformation",
657+
help="Disallow functions that have Any in their signature after decorator transformation",
660658
)
661659
disallow_any_group.add_argument(
662660
"--disallow-any-explicit",

mypy/message_registry.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,7 @@ def with_additional_msg(self, info: str) -> ErrorMessage:
196196
TYPEVAR_ARG_MUST_BE_TYPE: Final = '{} "{}" must be a type'
197197
TYPEVAR_UNEXPECTED_ARGUMENT: Final = 'Unexpected argument to "TypeVar()"'
198198
UNBOUND_TYPEVAR: Final = (
199-
"A function returning TypeVar should receive at least "
200-
"one argument containing the same TypeVar"
199+
"A function returning TypeVar should receive at least one argument containing the same TypeVar"
201200
)
202201
TYPE_PARAMETERS_SHOULD_BE_DECLARED: Final = (
203202
"All type parameters should be declared ({} not declared)"

mypy/messages.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -675,8 +675,7 @@ def incompatible_argument(
675675
arg_type, callee.arg_types[n - 1], options=self.options
676676
)
677677
info = (
678-
f" (expression has type {arg_type_str}, "
679-
f"target has type {callee_type_str})"
678+
f" (expression has type {arg_type_str}, target has type {callee_type_str})"
680679
)
681680
error_msg = (
682681
message_registry.INCOMPATIBLE_TYPES_IN_ASSIGNMENT.with_additional_msg(info)
@@ -1433,8 +1432,7 @@ def first_argument_for_super_must_be_type(self, actual: Type, context: Context)
14331432

14341433
def unsafe_super(self, method: str, cls: str, ctx: Context) -> None:
14351434
self.fail(
1436-
'Call to abstract method "{}" of "{}" with trivial body'
1437-
" via super() is unsafe".format(method, cls),
1435+
f'Call to abstract method "{method}" of "{cls}" with trivial body via super() is unsafe',
14381436
ctx,
14391437
code=codes.SAFE_SUPER,
14401438
)
@@ -1588,8 +1586,10 @@ def final_cant_override_writable(self, name: str, ctx: Context) -> None:
15881586

15891587
def cant_override_final(self, name: str, base_name: str, ctx: Context) -> None:
15901588
self.fail(
1591-
'Cannot override final attribute "{}"'
1592-
' (previously declared in base class "{}")'.format(name, base_name),
1589+
(
1590+
f'Cannot override final attribute "{name}" '
1591+
f'(previously declared in base class "{base_name}")'
1592+
),
15931593
ctx,
15941594
)
15951595

@@ -1676,15 +1676,16 @@ def overloaded_signatures_typevar_specific(self, index: int, context: Context) -
16761676

16771677
def overloaded_signatures_arg_specific(self, index: int, context: Context) -> None:
16781678
self.fail(
1679-
"Overloaded function implementation does not accept all possible arguments "
1680-
"of signature {}".format(index),
1679+
(
1680+
f"Overloaded function implementation does not accept all possible arguments "
1681+
f"of signature {index}"
1682+
),
16811683
context,
16821684
)
16831685

16841686
def overloaded_signatures_ret_specific(self, index: int, context: Context) -> None:
16851687
self.fail(
1686-
"Overloaded function implementation cannot produce return type "
1687-
"of signature {}".format(index),
1688+
f"Overloaded function implementation cannot produce return type of signature {index}",
16881689
context,
16891690
)
16901691

@@ -1707,8 +1708,7 @@ def operator_method_signatures_overlap(
17071708
context: Context,
17081709
) -> None:
17091710
self.fail(
1710-
'Signatures of "{}" of "{}" and "{}" of {} '
1711-
"are unsafely overlapping".format(
1711+
'Signatures of "{}" of "{}" and "{}" of {} are unsafely overlapping'.format(
17121712
reverse_method,
17131713
reverse_class.name,
17141714
forward_method,
@@ -1997,8 +1997,7 @@ def bad_proto_variance(
19971997
self, actual: int, tvar_name: str, expected: int, context: Context
19981998
) -> None:
19991999
msg = capitalize(
2000-
'{} type variable "{}" used in protocol where'
2001-
" {} one is expected".format(
2000+
'{} type variable "{}" used in protocol where {} one is expected'.format(
20022001
variance_string(actual), tvar_name, variance_string(expected)
20032002
)
20042003
)
@@ -2246,15 +2245,17 @@ def report_protocol_problems(
22462245
for name, subflags, superflags in conflict_flags[:MAX_ITEMS]:
22472246
if not class_obj and IS_CLASSVAR in subflags and IS_CLASSVAR not in superflags:
22482247
self.note(
2249-
"Protocol member {}.{} expected instance variable,"
2250-
" got class variable".format(supertype.type.name, name),
2248+
"Protocol member {}.{} expected instance variable, got class variable".format(
2249+
supertype.type.name, name
2250+
),
22512251
context,
22522252
code=code,
22532253
)
22542254
if not class_obj and IS_CLASSVAR in superflags and IS_CLASSVAR not in subflags:
22552255
self.note(
2256-
"Protocol member {}.{} expected class variable,"
2257-
" got instance variable".format(supertype.type.name, name),
2256+
"Protocol member {}.{} expected class variable, got instance variable".format(
2257+
supertype.type.name, name
2258+
),
22582259
context,
22592260
code=code,
22602261
)

mypy/modulefinder.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ def error_message_templates(self, daemon: bool) -> tuple[str, list[str]]:
7373
elif self is ModuleNotFoundReason.WRONG_WORKING_DIRECTORY:
7474
msg = 'Cannot find implementation or library stub for module named "{module}"'
7575
notes = [
76-
"You may be running mypy in a subpackage, "
77-
"mypy should be run on the package root"
76+
"You may be running mypy in a subpackage, mypy should be run on the package root"
7877
]
7978
elif self is ModuleNotFoundReason.FOUND_WITHOUT_TYPE_HINTS:
8079
msg = (

mypy/plugins/dataclasses.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,7 @@ def add_slots(
444444
# This means that version is lower than `3.10`,
445445
# it is just a non-existent argument for `dataclass` function.
446446
self._api.fail(
447-
'Keyword argument "slots" for "dataclass" '
448-
"is only valid in Python 3.10 and higher",
447+
'Keyword argument "slots" for "dataclass" is only valid in Python 3.10 and higher',
449448
self._reason,
450449
)
451450
return

mypy/semanal.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2636,8 +2636,7 @@ def calculate_class_mro(
26362636
calculate_mro(defn.info, obj_type)
26372637
except MroError:
26382638
self.fail(
2639-
"Cannot determine consistent method resolution "
2640-
'order (MRO) for "%s"' % defn.name,
2639+
f'Cannot determine consistent method resolution order (MRO) for "{defn.name}"',
26412640
defn,
26422641
)
26432642
self.set_dummy_mro(defn.info)

mypy/semanal_namedtuple.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,7 @@ def parse_namedtuple_args(
380380
rename = arg.name == "True"
381381
else:
382382
self.fail(
383-
'Boolean literal expected as the "rename" argument to '
384-
f"{type_name}()",
383+
f'Boolean literal expected as the "rename" argument to {type_name}()',
385384
arg,
386385
code=ARG_TYPE,
387386
)

mypy/typeanal.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -968,14 +968,12 @@ def analyze_unbound_type_without_type_info(
968968
message = 'Type variable "{}" is unbound'
969969
short = name.split(".")[-1]
970970
notes.append(
971-
(
972-
'(Hint: Use "Generic[{}]" or "Protocol[{}]" base class'
973-
' to bind "{}" inside a class)'
974-
).format(short, short, short)
971+
f'(Hint: Use "Generic[{short}]" or "Protocol[{short}]" base class'
972+
f' to bind "{short}" inside a class)'
975973
)
976974
notes.append(
977-
'(Hint: Use "{}" in function signature to bind "{}"'
978-
" inside a function)".format(short, short)
975+
f'(Hint: Use "{short}" in function signature '
976+
f'to bind "{short}" inside a function)'
979977
)
980978
else:
981979
message = 'Cannot interpret reference "{}" as a type'

0 commit comments

Comments
 (0)