From 9248e3d78c6aaa3129c2f4efd91d9a88f69fe42c Mon Sep 17 00:00:00 2001 From: "Arav K." Date: Fri, 28 Jun 2024 20:05:28 +0200 Subject: [PATCH] Use 'repr' and 'str' in place of '!r' and '!s' See: --- beets/autotag/mb.py | 4 +++- beets/dbcore/db.py | 6 +++--- beets/dbcore/query.py | 16 +++++++++------- beets/library.py | 6 +++--- beets/test/_common.py | 12 ++++++------ beets/ui/commands.py | 2 +- beets/util/__init__.py | 2 +- beets/util/functemplate.py | 2 +- beetsplug/bpd/__init__.py | 4 ++-- beetsplug/edit.py | 4 +++- beetsplug/replaygain.py | 8 ++++---- beetsplug/thumbnails.py | 4 +++- test/plugins/test_lyrics.py | 6 +++--- test/plugins/test_player.py | 6 +++--- test/plugins/test_thumbnails.py | 2 +- 15 files changed, 46 insertions(+), 38 deletions(-) diff --git a/beets/autotag/mb.py b/beets/autotag/mb.py index 91751f1d27..859889c857 100644 --- a/beets/autotag/mb.py +++ b/beets/autotag/mb.py @@ -65,7 +65,9 @@ def __init__(self, reason, verb, query, tb=None): super().__init__(reason, verb, tb) def get_message(self): - return f"{self._reasonstr()} in {self.verb} with query {self.query!r}" + return ( + f"{self._reasonstr()} in {self.verb} with query {repr(self.query)}" + ) log = logging.getLogger("beets") diff --git a/beets/dbcore/db.py b/beets/dbcore/db.py index 55ba6f1105..5aa75aa593 100755 --- a/beets/dbcore/db.py +++ b/beets/dbcore/db.py @@ -397,7 +397,7 @@ def _awaken( def __repr__(self) -> str: name = type(self).__name__ - fields = ", ".join(f"{k}={v!r}" for k, v in dict(self).items()) + fields = ", ".join(f"{k}={repr(v)}" for k, v in dict(self).items()) return f"{name}({fields})" def clear_dirty(self): @@ -558,12 +558,12 @@ def __iter__(self) -> Iterator[str]: def __getattr__(self, key): if key.startswith("_"): - raise AttributeError(f"model has no attribute {key!r}") + raise AttributeError(f"model has no attribute {repr(key)}") else: try: return self[key] except KeyError: - raise AttributeError(f"no such field {key!r}") + raise AttributeError(f"no such field {repr(key)}") def __setattr__(self, key, value): if key.startswith("_"): diff --git a/beets/dbcore/query.py b/beets/dbcore/query.py index 357b568575..6e94ddd51b 100644 --- a/beets/dbcore/query.py +++ b/beets/dbcore/query.py @@ -171,7 +171,7 @@ def match(self, obj: Model) -> bool: def __repr__(self) -> str: return ( - f"{self.__class__.__name__}({self.field_name!r}, {self.pattern!r}, " + f"{self.__class__.__name__}({repr(self.field_name)}, {repr(self.pattern)}, " f"fast={self.fast})" ) @@ -210,7 +210,9 @@ def match(self, obj: Model) -> bool: return obj.get(self.field_name) is None def __repr__(self) -> str: - return f"{self.__class__.__name__}({self.field_name!r}, {self.fast})" + return ( + f"{self.__class__.__name__}({repr(self.field_name)}, {self.fast})" + ) class StringFieldQuery(FieldQuery[P]): @@ -503,7 +505,7 @@ def clause_with_joiner( return clause, subvals def __repr__(self) -> str: - return f"{self.__class__.__name__}({self.subqueries!r})" + return f"{self.__class__.__name__}({repr(self.subqueries)})" def __eq__(self, other) -> bool: return super().__eq__(other) and self.subqueries == other.subqueries @@ -548,7 +550,7 @@ def match(self, obj: Model) -> bool: def __repr__(self) -> str: return ( - f"{self.__class__.__name__}({self.pattern!r}, {self.fields!r}, " + f"{self.__class__.__name__}({repr(self.pattern)}, {repr(self.fields)}, " f"{self.query_class.__name__})" ) @@ -619,7 +621,7 @@ def match(self, obj: Model) -> bool: return not self.subquery.match(obj) def __repr__(self) -> str: - return f"{self.__class__.__name__}({self.subquery!r})" + return f"{self.__class__.__name__}({repr(self.subquery)})" def __eq__(self, other) -> bool: return super().__eq__(other) and self.subquery == other.subquery @@ -975,7 +977,7 @@ def sort(self, items): return items def __repr__(self): - return f"{self.__class__.__name__}({self.sorts!r})" + return f"{self.__class__.__name__}({repr(self.sorts)})" def __hash__(self): return hash(tuple(self.sorts)) @@ -1015,7 +1017,7 @@ def key(obj: Model) -> Any: def __repr__(self) -> str: return ( f"{self.__class__.__name__}" - f"({self.field!r}, ascending={self.ascending!r})" + f"({repr(self.field)}, ascending={repr(self.ascending)})" ) def __hash__(self) -> int: diff --git a/beets/library.py b/beets/library.py index 6e4b7d1c25..97a91580f7 100644 --- a/beets/library.py +++ b/beets/library.py @@ -155,7 +155,7 @@ def col_clause(self): def __repr__(self) -> str: return ( - f"{self.__class__.__name__}({self.field!r}, {self.pattern!r}, " + f"{self.__class__.__name__}({repr(self.field)}, {repr(self.pattern)}, " f"fast={self.fast}, case_sensitive={self.case_sensitive})" ) @@ -729,7 +729,7 @@ def __repr__(self): # can even deadlock due to the database lock. name = type(self).__name__ keys = self.keys(with_album=False) - fields = (f"{k}={self[k]!r}" for k in keys) + fields = (f"{k}={repr(self[k])}" for k in keys) return f"{name}({', '.join(fields)})" def keys(self, computed=False, with_album=True): @@ -1572,7 +1572,7 @@ def parse_query_string(s, model_cls): The string is split into components using shell-like syntax. """ - message = f"Query is not unicode: {s!r}" + message = f"Query is not unicode: {repr(s)}" assert isinstance(s, str), message try: parts = shlex.split(s) diff --git a/beets/test/_common.py b/beets/test/_common.py index a2f89373df..7643af306d 100644 --- a/beets/test/_common.py +++ b/beets/test/_common.py @@ -159,26 +159,26 @@ class Assertions: def assertExists(self, path): # noqa self.assertTrue( - os.path.exists(syspath(path)), f"file does not exist: {path!r}" + os.path.exists(syspath(path)), f"file does not exist: {repr(path)}" ) def assertNotExists(self, path): # noqa self.assertFalse( - os.path.exists(syspath(path)), f"file exists: {path!r}" + os.path.exists(syspath(path)), f"file exists: {repr(path)}" ) def assertIsFile(self, path): # noqa self.assertExists(path) self.assertTrue( os.path.isfile(syspath(path)), - f"path exists, but is not a regular file: {path!r}", + f"path exists, but is not a regular file: {repr(path)}", ) def assertIsDir(self, path): # noqa self.assertExists(path) self.assertTrue( os.path.isdir(syspath(path)), - f"path exists, but is not a directory: {path!r}", + f"path exists, but is not a directory: {repr(path)}", ) def assert_equal_path(self, a, b): @@ -186,7 +186,7 @@ def assert_equal_path(self, a, b): self.assertEqual( util.normpath(a), util.normpath(b), - f"paths are not equal: {a!r} and {b!r}", + f"paths are not equal: {repr(a)} and {repr(b)}", ) @@ -294,7 +294,7 @@ def __init__(self, output=None): def __str__(self): msg = "Attempt to read with no input provided." if self.output is not None: - msg += f" Output: {self.output!r}" + msg += f" Output: {repr(self.output)}" return msg diff --git a/beets/ui/commands.py b/beets/ui/commands.py index 4086a6ed7e..a00d9749dc 100755 --- a/beets/ui/commands.py +++ b/beets/ui/commands.py @@ -214,7 +214,7 @@ def get_singleton_disambig_fields(info: hooks.TrackInfo) -> Sequence[str]: out = [] chosen_fields = config["match"]["singleton_disambig_fields"].as_str_seq() calculated_values = { - "index": f"Index {info.index!s}", + "index": f"Index {str(info.index)}", "track_alt": f"Track {info.track_alt}", "album": ( f"[{info.album}]" diff --git a/beets/util/__init__.py b/beets/util/__init__.py index fb704b4f93..c47a24f746 100644 --- a/beets/util/__init__.py +++ b/beets/util/__init__.py @@ -99,7 +99,7 @@ def _reasonstr(self): elif hasattr(self.reason, "strerror"): # i.e., EnvironmentError return self.reason.strerror else: - return f'"{self.reason!s}"' + return f'"{str(self.reason)}"' def get_message(self): """Create the human-readable description of the error, sans diff --git a/beets/util/functemplate.py b/beets/util/functemplate.py index 35f60b7d40..f149d370c2 100644 --- a/beets/util/functemplate.py +++ b/beets/util/functemplate.py @@ -166,7 +166,7 @@ def __init__(self, ident, args, original): self.original = original def __repr__(self): - return f"Call({self.ident!r}, {self.args!r}, {self.original!r})" + return f"Call({repr(self.ident)}, {repr(self.args)}, {repr(self.original)})" def evaluate(self, env): """Evaluate the function call in the environment, returning a diff --git a/beetsplug/bpd/__init__.py b/beetsplug/bpd/__init__.py index d6c75380a1..3336702c22 100644 --- a/beetsplug/bpd/__init__.py +++ b/beetsplug/bpd/__init__.py @@ -1142,7 +1142,7 @@ def _item_info(self, item): pass for tagtype, field in self.tagtype_map.items(): - info_lines.append(f"{tagtype}: {getattr(item, field)!s}") + info_lines.append(f"{tagtype}: {str(getattr(item, field))}") return info_lines @@ -1301,7 +1301,7 @@ def cmd_status(self, conn): yield ( f"bitrate: {item.bitrate / 1000}", - f"audio: {item.samplerate!s}:{item.bitdepth!s}:{item.channels!s}", + f"audio: {str(item.samplerate)}:{str(item.bitdepth)}:{str(item.channels)}", ) (pos, total) = self.player.time() diff --git a/beetsplug/edit.py b/beetsplug/edit.py index 61f2020ada..204302556a 100644 --- a/beetsplug/edit.py +++ b/beetsplug/edit.py @@ -47,7 +47,9 @@ def edit(filename, log): try: subprocess.call(cmd) except OSError as exc: - raise ui.UserError(f"could not run editor command {cmd[0]!r}: {exc}") + raise ui.UserError( + f"could not run editor command {repr(cmd[0])}: {exc}" + ) def dump(arg): diff --git a/beetsplug/replaygain.py b/beetsplug/replaygain.py index 78cce1e6ea..1c8aaaa9f0 100644 --- a/beetsplug/replaygain.py +++ b/beetsplug/replaygain.py @@ -534,7 +534,7 @@ def _find_line( if output[i].startswith(search): return i raise ReplayGainError( - f"ffmpeg output: missing {search!r} after line {start_line}" + f"ffmpeg output: missing {repr(search)} after line {start_line}" ) def _parse_float(self, line: bytes) -> float: @@ -547,7 +547,7 @@ def _parse_float(self, line: bytes) -> float: parts = line.split(b":", 1) if len(parts) < 2: raise ReplayGainError( - f"ffmpeg output: expected key value pair, found {line!r}" + f"ffmpeg output: expected key value pair, found {repr(line)}" ) value = parts[1].lstrip() # strip unit @@ -557,7 +557,7 @@ def _parse_float(self, line: bytes) -> float: return float(value) except ValueError: raise ReplayGainError( - f"ffmpeg output: expected float value, found {value!r}" + f"ffmpeg output: expected float value, found {repr(value)}" ) @@ -886,7 +886,7 @@ def _on_error(self, bus, message): f = self._src.get_property("location") # A GStreamer error, either an unsupported format or a bug. self._error = ReplayGainError( - f"Error {err!r} - {debug!r} on file {f!r}" + f"Error {repr(err)} - {repr(debug)} on file {repr(f)}" ) def _on_tag(self, bus, message): diff --git a/beetsplug/thumbnails.py b/beetsplug/thumbnails.py index acca413d01..0cde56c7d6 100644 --- a/beetsplug/thumbnails.py +++ b/beetsplug/thumbnails.py @@ -292,4 +292,6 @@ def uri(self, path): try: return uri.decode(util._fsencoding()) except UnicodeDecodeError: - raise RuntimeError(f"Could not decode filename from GIO: {uri!r}") + raise RuntimeError( + f"Could not decode filename from GIO: {repr(uri)}" + ) diff --git a/test/plugins/test_lyrics.py b/test/plugins/test_lyrics.py index 11a8ff4183..4c77924179 100644 --- a/test/plugins/test_lyrics.py +++ b/test/plugins/test_lyrics.py @@ -237,9 +237,9 @@ def assertLyricsContentOk(self, title, text, msg=""): # noqa: N802 if not keywords <= words: details = ( - f"{keywords!r} is not a subset of {words!r}." - f" Words only in expected set {keywords - words!r}," - f" Words only in result set {words - keywords!r}." + f"{repr(keywords)} is not a subset of {repr(words)}." + f" Words only in expected set {repr(keywords - words)}," + f" Words only in result set {repr(words - keywords)}." ) self.fail(f"{details} : {msg}") diff --git a/test/plugins/test_player.py b/test/plugins/test_player.py index 8a1795f72d..b250170f35 100644 --- a/test/plugins/test_player.py +++ b/test/plugins/test_player.py @@ -132,7 +132,7 @@ def _parse_status(self, status): cmd, rest = rest[2:].split("}") return False, (int(code), int(pos), cmd, rest[1:]) else: - raise RuntimeError(f"Unexpected status: {status!r}") + raise RuntimeError(f"Unexpected status: {repr(status)}") def _parse_body(self, body): """Messages are generally in the format "header: content". @@ -145,7 +145,7 @@ def _parse_body(self, body): if not line: continue if ":" not in line: - raise RuntimeError(f"Unexpected line: {line!r}") + raise RuntimeError(f"Unexpected line: {repr(line)}") header, content = line.split(":", 1) content = content.lstrip() if header in repeated_headers: @@ -191,7 +191,7 @@ def get_response(self, force_multi=None): responses.append(MPCResponse(response)) response = b"" elif not line: - raise RuntimeError(f"Unexpected response: {line!r}") + raise RuntimeError(f"Unexpected response: {repr(line)}") def serialise_command(self, command, *args): cmd = [command.encode("utf-8")] diff --git a/test/plugins/test_thumbnails.py b/test/plugins/test_thumbnails.py index 951fc6e8ce..00172153d0 100644 --- a/test/plugins/test_thumbnails.py +++ b/test/plugins/test_thumbnails.py @@ -76,7 +76,7 @@ def exists(path): return False if path == syspath(LARGE_DIR): return True - raise ValueError(f"unexpected path {path!r}") + raise ValueError(f"unexpected path {repr(path)}") mock_os.path.exists = exists plugin = ThumbnailsPlugin()