diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index a49a0443f..9251cea34 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -252,7 +252,8 @@ There are a few coding conventions we use in beets: Transaction objects help control concurrent access to the database and assist in debugging conflicting accesses. -- ``str.format()`` should be used instead of the ``%`` operator +- f-strings should be used instead of the ``%`` operator and ``str.format()`` + calls. - Never ``print`` informational messages; use the `logging `__ module instead. In particular, we have our own logging shim, so you’ll see ``from beets import diff --git a/beets/dbcore/query.py b/beets/dbcore/query.py index 1f4fff1c0..5242ea026 100644 --- a/beets/dbcore/query.py +++ b/beets/dbcore/query.py @@ -848,8 +848,6 @@ class DateQuery(FieldQuery[str]): date = datetime.fromtimestamp(timestamp) return self.interval.contains(date) - _clause_tmpl = "{0} {1} ?" - def col_clause(self) -> tuple[str, Sequence[SQLiteType]]: clause_parts = [] subvals = [] @@ -857,11 +855,11 @@ class DateQuery(FieldQuery[str]): # Convert the `datetime` objects to an integer number of seconds since # the (local) Unix epoch using `datetime.timestamp()`. if self.interval.start: - clause_parts.append(self._clause_tmpl.format(self.field, ">=")) + clause_parts.append(f"{self.field} >= ?") subvals.append(int(self.interval.start.timestamp())) if self.interval.end: - clause_parts.append(self._clause_tmpl.format(self.field, "<")) + clause_parts.append(f"{self.field} < ?") subvals.append(int(self.interval.end.timestamp())) if clause_parts: diff --git a/beets/ui/commands.py b/beets/ui/commands.py index 509b0e70b..91420a5c4 100755 --- a/beets/ui/commands.py +++ b/beets/ui/commands.py @@ -486,7 +486,6 @@ class ChangeRepresentation: """Format colored track indices.""" cur_track = self.format_index(item) new_track = self.format_index(track_info) - templ = "(#{})" changed = False # Choose color based on change. if cur_track != new_track: @@ -498,10 +497,8 @@ class ChangeRepresentation: else: highlight_color = "text_faint" - cur_track = templ.format(cur_track) - new_track = templ.format(new_track) - lhs_track = ui.colorize(highlight_color, cur_track) - rhs_track = ui.colorize(highlight_color, new_track) + lhs_track = ui.colorize(highlight_color, f"(#{cur_track})") + rhs_track = ui.colorize(highlight_color, f"(#{new_track})") return lhs_track, rhs_track, changed @staticmethod diff --git a/beetsplug/duplicates.py b/beetsplug/duplicates.py index ea7abaaff..73fdee6a4 100644 --- a/beetsplug/duplicates.py +++ b/beetsplug/duplicates.py @@ -150,7 +150,7 @@ class DuplicatesPlugin(BeetsPlugin): count = self.config["count"].get(bool) delete = self.config["delete"].get(bool) remove = self.config["remove"].get(bool) - fmt = self.config["format"].get(str) + fmt_tmpl = self.config["format"].get(str) full = self.config["full"].get(bool) keys = self.config["keys"].as_str_seq() merge = self.config["merge"].get(bool) @@ -175,15 +175,14 @@ class DuplicatesPlugin(BeetsPlugin): return if path: - fmt = "$path" + fmt_tmpl = "$path" # Default format string for count mode. - if count and not fmt: + if count and not fmt_tmpl: if album: - fmt = "$albumartist - $album" + fmt_tmpl = "$albumartist - $album" else: - fmt = "$albumartist - $album - $title" - fmt += ": {0}" + fmt_tmpl = "$albumartist - $album - $title" if checksum: for i in items: @@ -207,7 +206,7 @@ class DuplicatesPlugin(BeetsPlugin): delete=delete, remove=remove, tag=tag, - fmt=fmt.format(obj_count), + fmt=f"{fmt_tmpl}: {obj_count}", ) self._command.func = _dup diff --git a/beetsplug/info.py b/beetsplug/info.py index c4d5aacbf..69e35184f 100644 --- a/beetsplug/info.py +++ b/beetsplug/info.py @@ -117,7 +117,6 @@ def print_data(data, item=None, fmt=None): return maxwidth = max(len(key) for key in formatted) - lineformat = f"{{0:>{maxwidth}}}: {{1}}" if path: ui.print_(displayable_path(path)) @@ -126,7 +125,7 @@ def print_data(data, item=None, fmt=None): value = formatted[field] if isinstance(value, list): value = "; ".join(value) - ui.print_(lineformat.format(field, value)) + ui.print_(f"{field:>{maxwidth}}: {value}") def print_data_keys(data, item=None): @@ -139,12 +138,11 @@ def print_data_keys(data, item=None): if len(formatted) == 0: return - line_format = "{0}{{0}}".format(" " * 4) if path: ui.print_(displayable_path(path)) for field in sorted(formatted): - ui.print_(line_format.format(field)) + ui.print_(f" {field}") class InfoPlugin(BeetsPlugin): diff --git a/beetsplug/inline.py b/beetsplug/inline.py index 3c728bf8d..00907577a 100644 --- a/beetsplug/inline.py +++ b/beetsplug/inline.py @@ -36,7 +36,8 @@ def _compile_func(body): """Given Python code for a function body, return a compiled callable that invokes that code. """ - body = "def {}():\n {}".format(FUNC_NAME, body.replace("\n", "\n ")) + body = body.replace("\n", "\n ") + body = f"def {FUNC_NAME}():\n {body}" code = compile(body, "inline", "exec") env = {} eval(code, env) diff --git a/test/test_ui.py b/test/test_ui.py index 63d88f668..fb166e690 100644 --- a/test/test_ui.py +++ b/test/test_ui.py @@ -1261,7 +1261,6 @@ class ShowChangeTest(IOMixin, unittest.TestCase): msg = self._show_change( cur_artist=long_name, cur_album="another album" ) - # _common.log.info("Message:{}".format(msg)) assert "artist: another artist" in msg assert " -> the artist" in msg assert "another album -> the album" not in msg