From: winklemad Date: Fri, 17 Jul 2026 01:09:00 +0000 (+0530) Subject: Fix missing f-string prefix in two DataError messages X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=0f14cfe7b932f1741992df08e22073638457e382;p=thirdparty%2Fpsycopg.git Fix missing f-string prefix in two DataError messages Two error messages were built from plain string literals containing {...} placeholders but missing the f prefix, so the literal placeholder text was shown to the user instead of the offending value: - types/datetime.py: "timestamp too small (before year 1): {s!r}" - types/json.py: "unknown jsonb binary format: {data[0]}" The sibling branches next to the datetime message are already f-strings, which is what makes the omission stand out. Add the missing prefix to both so the value is interpolated. Fixes #1372 --- diff --git a/docs/news.rst b/docs/news.rst index deffeb11c..2837b6966 100644 --- a/docs/news.rst +++ b/docs/news.rst @@ -19,6 +19,9 @@ Psycopg 3.3.5 (unreleased) no `!NoneType` dumper registered in python implementation (:ticket:`#1325`). - Fix `!wait_selector` wait function to not raise `!KeyError` (:ticket:`#1327`). +- Fix `!DataError` messages leaking the literal ``{...}`` placeholder instead + of the offending value when loading a pre-year-1 :sql:`timestamp` or a + malformed binary :sql:`jsonb` value (:ticket:`#1372`). Current release diff --git a/psycopg/psycopg/types/datetime.py b/psycopg/psycopg/types/datetime.py index 8bd4e0f24..3226935a9 100644 --- a/psycopg/psycopg/types/datetime.py +++ b/psycopg/psycopg/types/datetime.py @@ -689,7 +689,7 @@ def _get_timestamp_load_error( return len(s.split()[-1]) > 4 # year is last token if s == "-infinity" or s.endswith("BC"): - return DataError("timestamp too small (before year 1): {s!r}") + return DataError(f"timestamp too small (before year 1): {s!r}") elif s == "infinity" or is_overflow(s): return DataError(f"timestamp too large (after year 10K): {s!r}") else: diff --git a/psycopg/psycopg/types/json.py b/psycopg/psycopg/types/json.py index 6ebab6cbe..1d4a12648 100644 --- a/psycopg/psycopg/types/json.py +++ b/psycopg/psycopg/types/json.py @@ -277,7 +277,7 @@ class JsonbBinaryLoader(_JsonLoader): def load(self, data: Buffer) -> Any: if data and data[0] != 1: - raise DataError("unknown jsonb binary format: {data[0]}") + raise DataError(f"unknown jsonb binary format: {data[0]}") if not isinstance((data := data[1:]), bytes): data = bytes(data) return self.loads(data)