From: Daniele Varrazzo Date: Fri, 27 Dec 2024 23:32:55 +0000 (+0100) Subject: refactor: use re objects for typing of Match and Pattern X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=396d1174db3b5887a5ae2f9f2cd751520c6ce683;p=thirdparty%2Fpsycopg.git refactor: use re objects for typing of Match and Pattern --- diff --git a/psycopg/psycopg/_copy_base.py b/psycopg/psycopg/_copy_base.py index 21608c972..2c0e9207a 100644 --- a/psycopg/psycopg/_copy_base.py +++ b/psycopg/psycopg/_copy_base.py @@ -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)] diff --git a/psycopg/psycopg/_queries.py b/psycopg/psycopg/_queries.py index 3368e79d1..31522100b 100644 --- a/psycopg/psycopg/_queries.py +++ b/psycopg/psycopg/_queries.py @@ -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 diff --git a/psycopg/psycopg/types/array.py b/psycopg/psycopg/types/array.py index 1da8f4316..a50d3bf58 100644 --- a/psycopg/psycopg/types/array.py +++ b/psycopg/psycopg/types/array.py @@ -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 """ diff --git a/tests/_test_cursor.py b/tests/_test_cursor.py index f2f643e51..9258e4641 100644 --- a/tests/_test_cursor.py +++ b/tests/_test_cursor.py @@ -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