]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
refactor: use re objects for typing of Match and Pattern
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 27 Dec 2024 23:32:55 +0000 (00:32 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 27 Dec 2024 23:46:48 +0000 (00:46 +0100)
psycopg/psycopg/_copy_base.py
psycopg/psycopg/_queries.py
psycopg/psycopg/types/array.py
tests/_test_cursor.py

index 21608c9728d80d7f002111ffadcf1059d5e799a5..2c0e9207a505eff7443bb1ee46fb20cd7bfca1cd 100644 (file)
@@ -10,7 +10,8 @@ import re
 import sys
 import struct
 from abc import ABC, abstractmethod
-from typing import Any, Generic, Match, Sequence, TYPE_CHECKING
+from typing import Any, Generic, TYPE_CHECKING
+from collections.abc import Sequence
 
 from . import pq
 from . import adapt
@@ -415,7 +416,7 @@ _dump_repl = {
 }
 
 
-def _dump_sub(m: Match[bytes], __map: dict[bytes, bytes] = _dump_repl) -> bytes:
+def _dump_sub(m: re.Match[bytes], __map: dict[bytes, bytes] = _dump_repl) -> bytes:
     return __map[m.group(0)]
 
 
@@ -423,7 +424,7 @@ _load_re = re.compile(b"\\\\[btnvfr\\\\]")
 _load_repl = {v: k for k, v in _dump_repl.items()}
 
 
-def _load_sub(m: Match[bytes], __map: dict[bytes, bytes] = _load_repl) -> bytes:
+def _load_sub(m: re.Match[bytes], __map: dict[bytes, bytes] = _load_repl) -> bytes:
     return __map[m.group(0)]
 
 
index 3368e79d1ad01cb0a3927b1724670a26908af3d4..31522100ba85de0f667621440e5ec5fe5947f658 100644 (file)
@@ -7,8 +7,8 @@ Utility module to manipulate queries
 from __future__ import annotations
 
 import re
-from typing import Any, Callable, Mapping, Match, NamedTuple
-from typing import Sequence, TYPE_CHECKING
+from typing import Any, Callable, NamedTuple, TYPE_CHECKING
+from collections.abc import Mapping, Sequence
 from functools import lru_cache
 
 from . import pq
@@ -347,7 +347,7 @@ _re_placeholder = re.compile(
 def _split_query(
     query: bytes, encoding: str = "ascii", collapse_double_percent: bool = True
 ) -> list[QueryPart]:
-    parts: list[tuple[bytes, Match[bytes] | None]] = []
+    parts: list[tuple[bytes, re.Match[bytes] | None]] = []
     cur = 0
 
     # pairs [(fragment, match], with the last match None
index 1da8f43169d9b621dc853bd43adccb64e6b7bdcc..a50d3bf586091a133246cfa8b9885fdcc152e3cb 100644 (file)
@@ -9,7 +9,7 @@ from __future__ import annotations
 import re
 import struct
 from math import prod
-from typing import Any, cast, Callable, Pattern
+from typing import Any, cast, Callable
 
 from .. import pq
 from .. import errors as e
@@ -199,7 +199,7 @@ class ListDumper(BaseListDumper):
 
 
 @cache
-def _get_needs_quotes_regexp(delimiter: bytes) -> Pattern[bytes]:
+def _get_needs_quotes_regexp(delimiter: bytes) -> re.Pattern[bytes]:
     """Return a regexp to recognise when a value needs quotes
 
     from https://www.postgresql.org/docs/current/arrays.html#ARRAYS-IO
@@ -385,7 +385,7 @@ def _load_text(
     data: Buffer,
     loader: Loader,
     delimiter: bytes = b",",
-    __re_unescape: Pattern[bytes] = re.compile(rb"\\(.)"),
+    __re_unescape: re.Pattern[bytes] = re.compile(rb"\\(.)"),
 ) -> list[Any]:
     rv = None
     stack: list[Any] = []
@@ -434,7 +434,7 @@ def _load_text(
 
 
 @cache
-def _get_array_parse_regexp(delimiter: bytes) -> Pattern[bytes]:
+def _get_array_parse_regexp(delimiter: bytes) -> re.Pattern[bytes]:
     """
     Return a regexp to tokenize an array representation into item and brackets
     """
index f2f643e519176e2ebae513574637e694295469e9..9258e4641d611289d4fdef2766ad9f7fa58b4aed 100644 (file)
@@ -5,7 +5,7 @@ Support module for test_cursor[_async].py
 from __future__ import annotations
 
 import re
-from typing import Any, Match
+from typing import Any
 
 import pytest
 import psycopg
@@ -41,7 +41,7 @@ def ph(cur: Any, query: str) -> str:
 
     n = 1
 
-    def s(m: Match[str]) -> str:
+    def s(m: re.Match[str]) -> str:
         nonlocal n
         rv = f"${n}"
         n += 1