diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e89f5ad..1e7dfa7 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -11,8 +11,3 @@ repos: entry: ruff format language: system types: [python] - - id: pyupgrade - name: pyupgrade - entry: pyupgrade --exit-zero-even-if-changed - language: system - types: [python] diff --git a/.ruff.toml b/.ruff.toml index edd3313..6c49746 100644 --- a/.ruff.toml +++ b/.ruff.toml @@ -11,6 +11,7 @@ extend-select = [ "E", # pycodestyle errors "F", # pyflakes "I", # isort + "UP", # Pyupgrade "W", # pycodestyle warnings ] ignore = [ diff --git a/pgtoolkit/conf.py b/pgtoolkit/conf.py index 1401cfe..a2648b2 100644 --- a/pgtoolkit/conf.py +++ b/pgtoolkit/conf.py @@ -286,7 +286,7 @@ def serialize_value(value: Value) -> str: # done everywhere in the string or nowhere. if "''" not in value and r"\'" not in value: value = value.replace("'", "''") - value = "'%s'" % value + value = f"'{value}'" elif isinstance(value, timedelta): seconds = value.days * _day + value.seconds if value.microseconds: @@ -340,7 +340,7 @@ def serialize(self) -> str: return serialize_value(self.value) def __str__(self) -> str: - line = "%(name)s = %(value)s" % dict(name=self.name, value=self.serialize()) + line = "{name} = {value}".format(**dict(name=self.name, value=self.serialize())) if self.comment: line += " # " + self.comment if self.commented: @@ -497,7 +497,7 @@ def parse(self, fo: Iterable[str]) -> Iterator[tuple[pathlib.Path, IncludeType]] else: m = self._parameter_re.match(line) if not m: - raise ValueError("Bad line: %r." % raw_line) + raise ValueError(f"Bad line: {raw_line!r}.") kwargs = m.groupdict() name = kwargs.pop("name") value = parse_value(kwargs.pop("value")) diff --git a/pgtoolkit/errors.py b/pgtoolkit/errors.py index 60afdd7..68f151a 100644 --- a/pgtoolkit/errors.py +++ b/pgtoolkit/errors.py @@ -8,15 +8,7 @@ def __init__(self, lineno: int, line: str, message: str) -> None: self.line = line def __repr__(self) -> str: - return "<%s at line %d: %.32s>" % ( - self.__class__.__name__, - self.lineno, - self.args[0], - ) + return f"<{self.__class__.__name__} at line {self.lineno}: {self.args[0]:.32}>" def __str__(self) -> str: - return "Bad line #{} '{:.32}': {}".format( - self.lineno, - self.line.strip(), - self.args[0], - ) + return f"Bad line #{self.lineno} '{self.line.strip():.32}': {self.args[0]}" diff --git a/pgtoolkit/hba.py b/pgtoolkit/hba.py index 8c57d19..b23c0b5 100644 --- a/pgtoolkit/hba.py +++ b/pgtoolkit/hba.py @@ -158,7 +158,7 @@ def parse(cls, line: str) -> HBARecord: comment = " ".join(comments[1:]) if values[0] not in cls.CONNECTION_TYPES: - raise ValueError("Unknown connection type '%s'" % values[0]) + raise ValueError(f"Unknown connection type '{values[0]}'") if "local" != values[0]: record_fields.append("address") common_values = [v for v in values if "=" not in v] @@ -220,14 +220,14 @@ def __str__(self) -> str: continue if width: - fmt += "%%(%s)-%ds " % (field, width - 1) + fmt += f"%%({field})-%ds " % (width - 1) else: fmt += f"%({field})s " # Serialize database and user list using property. values = dict(self.__dict__, databases=self.database, users=self.user) line = fmt.rstrip() % values - auth_options = ['%s="%s"' % i for i in self.auth_options] + auth_options = ['{}="{}"'.format(*i) for i in self.auth_options] if auth_options: line += " " + " ".join(auth_options) @@ -290,7 +290,7 @@ def matches(self, **attrs: str) -> bool: # Provided attributes should be comparable to HBARecord attributes for k in attrs.keys(): if k not in self.COMMON_FIELDS + ["database", "user"]: - raise AttributeError("%s is not a valid attribute" % k) + raise AttributeError(f"{k} is not a valid attribute") for k, v in attrs.items(): if getattr(self, k, None) != v: @@ -326,7 +326,7 @@ def __init__(self, entries: Iterable[HBAComment | HBARecord] | None = None) -> N :param entries: A list of HBAComment or HBARecord. Optional. """ if entries and not isinstance(entries, list): - raise ValueError("%s should be a list" % entries) + raise ValueError(f"{entries} should be a list") self.lines = list(entries) if entries is not None else [] self.path = None diff --git a/pgtoolkit/log/parser.py b/pgtoolkit/log/parser.py index aeb3a85..1fafe86 100644 --- a/pgtoolkit/log/parser.py +++ b/pgtoolkit/log/parser.py @@ -118,11 +118,11 @@ def parse_isodatetime(raw: str) -> datetime: int(raw[20:23]) if raw[19] == "." else 0, ) except ValueError: - raise ValueError("%s is not a known date" % raw) + raise ValueError(f"{raw} is not a known date") if raw[-3:] != "UTC": # We need tzdata for that. - raise ValueError("%s not in UTC." % raw) + raise ValueError(f"{raw} not in UTC.") return datetime(*infos) diff --git a/pgtoolkit/pgpass.py b/pgtoolkit/pgpass.py index 47ae1e3..bda6e72 100644 --- a/pgtoolkit/pgpass.py +++ b/pgtoolkit/pgpass.py @@ -244,13 +244,7 @@ def __lt__(self, other: PassComment | PassEntry) -> bool: return NotImplemented def __repr__(self) -> str: - return "<{} {}@{}:{}/{}>".format( - self.__class__.__name__, - self.username, - self.hostname, - self.port, - self.database, - ) + return f"<{self.__class__.__name__} {self.username}@{self.hostname}:{self.port}/{self.database}>" def __str__(self) -> str: return ":".join( @@ -284,7 +278,7 @@ def matches(self, **attrs: int | str) -> bool: expected_attributes = self.__dict__.keys() for k in attrs.keys(): if k not in expected_attributes: - raise AttributeError("%s is not a valid attribute" % k) + raise AttributeError(f"{k} is not a valid attribute") for k, v in attrs.items(): if getattr(self, k) != v: @@ -328,7 +322,7 @@ def __init__( :param entries: A list of PassEntry or PassComment. Optional. """ if entries and not isinstance(entries, list): - raise ValueError("%s should be a list" % entries) + raise ValueError(f"{entries} should be a list") self.lines = entries or [] self.path = path diff --git a/pgtoolkit/service.py b/pgtoolkit/service.py index 2ec32bd..fd731e3 100644 --- a/pgtoolkit/service.py +++ b/pgtoolkit/service.py @@ -179,7 +179,7 @@ def __init__(self) -> None: ) def __repr__(self) -> str: - return "<%s>" % (self.__class__.__name__) + return f"<{self.__class__.__name__}>" def __getitem__(self, key: str) -> Service: parameters = { diff --git a/tox.ini b/tox.ini index 4bdd2d4..db7b2dd 100644 --- a/tox.ini +++ b/tox.ini @@ -7,12 +7,9 @@ isolated_build = true commands= ruff check ruff format --check - pre-commit run --all-files --show-diff-on-failure pyupgrade check-manifest deps = check-manifest - pre-commit - pyupgrade ruff skip_install = true