Skip to content

Commit 87bc26b

Browse files
committed
refactor: use f-strings more
1 parent 7ea1535 commit 87bc26b

File tree

7 files changed

+13
-14
lines changed

7 files changed

+13
-14
lines changed

coverage/config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ def _set_attr_from_config_option(
449449
"""
450450
section, option = where.split(":")
451451
if cp.has_option(section, option):
452-
method = getattr(cp, "get" + type_)
452+
method = getattr(cp, f"get{type_}")
453453
setattr(self, attr, method(section, option))
454454
return True
455455
return False

coverage/context.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from __future__ import annotations
77

88
from types import FrameType
9-
from typing import cast
109
from collections.abc import Sequence
1110

1211
from coverage.types import TShouldStartContextFn
@@ -65,11 +64,11 @@ def qualname_from_frame(frame: FrameType) -> str | None:
6564
func = frame.f_globals.get(fname)
6665
if func is None:
6766
return None
68-
return cast(str, func.__module__ + "." + fname)
67+
return f"{func.__module__}.{fname}"
6968

7069
func = getattr(method, "__func__", None)
7170
if func is None:
7271
cls = self.__class__
73-
return cast(str, cls.__module__ + "." + cls.__name__ + "." + fname)
72+
return f"{cls.__module__}.{cls.__name__}.{fname}"
7473

75-
return cast(str, func.__module__ + "." + func.__qualname__)
74+
return f"{func.__module__}.{func.__qualname__}"

coverage/debug.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ def filter(self, text: str) -> str:
362362
"""Add a cwd message for each new cwd."""
363363
cwd = os.getcwd()
364364
if cwd != self.cwd:
365-
text = f"cwd is now {cwd!r}\n" + text
365+
text = f"cwd is now {cwd!r}\n{text}"
366366
self.cwd = cwd
367367
return text
368368

@@ -404,7 +404,7 @@ def filter(self, text: str) -> str:
404404
"""Add a message when the pytest test changes."""
405405
test_name = os.getenv("PYTEST_CURRENT_TEST")
406406
if test_name != self.test_name:
407-
text = f"Pytest context: {test_name}\n" + text
407+
text = f"Pytest context: {test_name}\n{text}"
408408
self.test_name = test_name
409409
return text
410410

coverage/execfile.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def _prepare2(self) -> None:
142142
# Running a directory means running the __main__.py file in that
143143
# directory.
144144
for ext in [".py", ".pyc", ".pyo"]:
145-
try_filename = os.path.join(self.arg0, "__main__" + ext)
145+
try_filename = os.path.join(self.arg0, f"__main__{ext}")
146146
# 3.8.10 changed how files are reported when running a
147147
# directory.
148148
try_filename = os.path.abspath(try_filename)

coverage/files.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ def _glob_to_regex(pattern: str) -> str:
332332
# Turn all backslashes into slashes to simplify the tokenizer.
333333
pattern = pattern.replace("\\", "/")
334334
if "/" not in pattern:
335-
pattern = "**/" + pattern
335+
pattern = f"**/{pattern}"
336336
path_rx = []
337337
pos = 0
338338
while pos < len(pattern):

coverage/parser.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ def analyze(self) -> None:
741741
"""Examine the AST tree from `self.root_node` to determine possible arcs."""
742742
for node in ast.walk(self.root_node):
743743
node_name = node.__class__.__name__
744-
code_object_handler = getattr(self, "_code_object__" + node_name, None)
744+
code_object_handler = getattr(self, f"_code_object__{node_name}", None)
745745
if code_object_handler is not None:
746746
code_object_handler(node)
747747

@@ -832,7 +832,7 @@ def line_for_node(self, node: ast.AST) -> TLineNo:
832832
node_name = node.__class__.__name__
833833
handler = cast(
834834
Optional[Callable[[ast.AST], TLineNo]],
835-
getattr(self, "_line__" + node_name, None),
835+
getattr(self, f"_line__{node_name}", None),
836836
)
837837
if handler is not None:
838838
line = handler(node)
@@ -913,7 +913,7 @@ def node_exits(self, node: ast.AST) -> set[ArcStart]:
913913
node_name = node.__class__.__name__
914914
handler = cast(
915915
Optional[Callable[[ast.AST], set[ArcStart]]],
916-
getattr(self, "_handle__" + node_name, None),
916+
getattr(self, f"_handle__{node_name}", None),
917917
)
918918
if handler is not None:
919919
arc_starts = handler(node)
@@ -989,7 +989,7 @@ def find_non_missing_node(self, node: ast.AST) -> ast.AST | None:
989989

990990
missing_fn = cast(
991991
Optional[Callable[[ast.AST], Optional[ast.AST]]],
992-
getattr(self, "_missing__" + node.__class__.__name__, None),
992+
getattr(self, f"_missing__{node.__class__.__name__}", None),
993993
)
994994
if missing_fn is not None:
995995
ret_node = missing_fn(node)

coverage/sqldata.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def _choose_filename(self) -> None:
266266
self._filename = self._basename
267267
suffix = filename_suffix(self._suffix)
268268
if suffix:
269-
self._filename += "." + suffix
269+
self._filename += f".{suffix}"
270270

271271
def _reset(self) -> None:
272272
"""Reset our attributes."""

0 commit comments

Comments
 (0)