From: Daniele Varrazzo Date: Sun, 15 May 2022 17:46:34 +0000 (+0200) Subject: fix: Don't rely on f-string converting str enums in their values X-Git-Tag: 3.1~94 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ebcf9af552d7423b1f617aa6431b02be64771293;p=thirdparty%2Fpsycopg.git fix: Don't rely on f-string converting str enums in their values The behaviour seems changed in Python 3.11: now f"%{PyFormat.AUTO}" is evaluated as `%PyFormat.AUTO` instead of `%s`, for the general hilarity. --- diff --git a/psycopg/psycopg/sql.py b/psycopg/psycopg/sql.py index c111e2b74..fcb67b874 100644 --- a/psycopg/psycopg/sql.py +++ b/psycopg/psycopg/sql.py @@ -422,7 +422,7 @@ class Placeholder(Composable): """ - def __init__(self, name: str = "", format: PyFormat = PyFormat.AUTO): + def __init__(self, name: str = "", format: Union[str, PyFormat] = PyFormat.AUTO): super().__init__(name) if not isinstance(name, str): raise TypeError(f"expected string as name, got {name!r}") @@ -430,19 +430,26 @@ class Placeholder(Composable): if ")" in name: raise ValueError(f"invalid name: {name!r}") - self._format = format + if type(format) is str: + format = PyFormat(format) + if not isinstance(format, PyFormat): + raise TypeError( + f"expected PyFormat as format, got {type(format).__name__!r}" + ) + + self._format: PyFormat = format def __repr__(self) -> str: parts = [] if self._obj: parts.append(repr(self._obj)) - if self._format != PyFormat.AUTO: - parts.append(f"format={PyFormat(self._format).name}") + if self._format is not PyFormat.AUTO: + parts.append(f"format={self._format.name}") return f"{self.__class__.__name__}({', '.join(parts)})" def as_string(self, context: Optional[AdaptContext]) -> str: - code = self._format + code = self._format.value return f"%({self._obj}){code}" if self._obj else f"%{code}" def as_bytes(self, context: Optional[AdaptContext]) -> bytes: