From d4568467d66e38ebfbf3b8fd7ec191ed940c1176 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Thu, 30 Sep 2021 20:58:43 +0200 Subject: [PATCH] Upgrade flake to 3.9 This version reports an error if a name is declared but not defined, so set the lazy imported objects to None and deal with mypy complaining. --- psycopg/psycopg/types/net.py | 25 +++++++++++-------------- psycopg/psycopg/types/uuid.py | 9 +++------ psycopg/setup.py | 2 +- 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/psycopg/psycopg/types/net.py b/psycopg/psycopg/types/net.py index 614fe566d..264f8dbf7 100644 --- a/psycopg/psycopg/types/net.py +++ b/psycopg/psycopg/types/net.py @@ -19,16 +19,15 @@ Interface = Union["ipaddress.IPv4Interface", "ipaddress.IPv6Interface"] Network = Union["ipaddress.IPv4Network", "ipaddress.IPv6Network"] # These objects will be imported lazily -imported = False -ip_address: Callable[[str], Address] -ip_interface: Callable[[str], Interface] -ip_network: Callable[[str], Network] -IPv4Address: "Type[ipaddress.IPv4Address]" -IPv6Address: "Type[ipaddress.IPv6Address]" -IPv4Interface: "Type[ipaddress.IPv4Interface]" -IPv6Interface: "Type[ipaddress.IPv6Interface]" -IPv4Network: "Type[ipaddress.IPv4Network]" -IPv6Network: "Type[ipaddress.IPv6Network]" +ip_address: Callable[[str], Address] = None # type: ignore[assignment] +ip_interface: Callable[[str], Interface] = None # type: ignore[assignment] +ip_network: Callable[[str], Network] = None # type: ignore[assignment] +IPv4Address: "Type[ipaddress.IPv4Address]" = None # type: ignore[assignment] +IPv6Address: "Type[ipaddress.IPv6Address]" = None # type: ignore[assignment] +IPv4Interface: "Type[ipaddress.IPv4Interface]" = None # type: ignore[assignment] +IPv6Interface: "Type[ipaddress.IPv6Interface]" = None # type: ignore[assignment] +IPv4Network: "Type[ipaddress.IPv4Network]" = None # type: ignore[assignment] +IPv6Network: "Type[ipaddress.IPv6Network]" = None # type: ignore[assignment] PGSQL_AF_INET = 2 PGSQL_AF_INET6 = 3 @@ -38,18 +37,16 @@ IPV6_PREFIXLEN = 128 class _LazyIpaddress: def _ensure_module(self) -> None: - global imported, ip_address, ip_interface, ip_network + global ip_address, ip_interface, ip_network global IPv4Address, IPv6Address, IPv4Interface, IPv6Interface global IPv4Network, IPv6Network - if not imported: + if not ip_address: from ipaddress import ip_address, ip_interface, ip_network from ipaddress import IPv4Address, IPv6Address from ipaddress import IPv4Interface, IPv6Interface from ipaddress import IPv4Network, IPv6Network - imported = True - class InterfaceDumper(Dumper): diff --git a/psycopg/psycopg/types/uuid.py b/psycopg/psycopg/types/uuid.py index a3b962d83..3ff43b937 100644 --- a/psycopg/psycopg/types/uuid.py +++ b/psycopg/psycopg/types/uuid.py @@ -15,8 +15,7 @@ if TYPE_CHECKING: import uuid # Importing the uuid module is slow, so import it only on request. -imported = False -UUID: Callable[..., "uuid.UUID"] +UUID: Callable[..., "uuid.UUID"] = None # type: ignore[assignment] class UUIDDumper(Dumper): @@ -38,12 +37,10 @@ class UUIDBinaryDumper(UUIDDumper): class UUIDLoader(Loader): def __init__(self, oid: int, context: Optional[AdaptContext] = None): super().__init__(oid, context) - global imported, UUID - if not imported: + global UUID + if not UUID: from uuid import UUID - imported = True - def load(self, data: Buffer) -> "uuid.UUID": if isinstance(data, memoryview): data = bytes(data) diff --git a/psycopg/setup.py b/psycopg/setup.py index e71b75738..04d415632 100644 --- a/psycopg/setup.py +++ b/psycopg/setup.py @@ -47,7 +47,7 @@ extras_require = { # Requirements needed for development "dev": [ "black", - "flake8 >= 3.8, < 3.9", + "flake8 ~= 3.9.2", "mypy >= 0.812", "wheel", ], -- 2.47.2