]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
fix: Don't rely on f-string converting str enums in their values
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 15 May 2022 17:46:34 +0000 (19:46 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 16 May 2022 01:04:23 +0000 (03:04 +0200)
The behaviour seems changed in Python 3.11: now f"%{PyFormat.AUTO}" is
evaluated as `%PyFormat.AUTO` instead of `%s`, for the general hilarity.

psycopg/psycopg/sql.py

index c111e2b74fe1c8ca237051bf1eb26505cfba103a..fcb67b8741f8712bad7c15a43e0625a6a8d36f17 100644 (file)
@@ -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: