From cd2dbd813ab7538f7792c10926ffbd40c9adf6be Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Thu, 2 Apr 2020 00:59:01 +1300 Subject: [PATCH] Include None in AdaptContext Using Union[None...] instead of Optional[... is a choice. None has a meaning, it means global, not optional. --- psycopg3/adaptation.py | 31 +++++++++---------------------- psycopg3/connection.py | 13 ++----------- psycopg3/utils/queries.py | 14 ++------------ 3 files changed, 13 insertions(+), 45 deletions(-) diff --git a/psycopg3/adaptation.py b/psycopg3/adaptation.py index 5ba57a649..3db374ce5 100644 --- a/psycopg3/adaptation.py +++ b/psycopg3/adaptation.py @@ -5,18 +5,9 @@ Entry point into the adaptation system. # Copyright (C) 2020 The Psycopg Team import codecs -from typing import ( - Any, - Callable, - cast, - Dict, - Generator, - List, - Optional, - Sequence, - Tuple, - Union, -) +from typing import cast +from typing import Any, Callable, Dict, Generator, List, Optional, Sequence +from typing import Tuple, Union from . import exceptions as exc from .pq import Format, PGresult @@ -28,7 +19,7 @@ from .utils.typing import DecodeFunc, Oid # Type system -AdaptContext = Union[BaseConnection, BaseCursor] +AdaptContext = Union[None, BaseConnection, BaseCursor] MaybeOid = Union[Optional[bytes], Tuple[Optional[bytes], Oid]] AdapterFunc = Callable[[Any], MaybeOid] @@ -54,7 +45,7 @@ class Adapter: def register( cls: type, adapter: AdapterType, - context: Optional[AdaptContext] = None, + context: AdaptContext = None, format: Format = Format.TEXT, ) -> AdapterType: if not isinstance(cls, type): @@ -85,9 +76,7 @@ class Adapter: @staticmethod def register_binary( - cls: type, - adapter: AdapterType, - context: Optional[AdaptContext] = None, + cls: type, adapter: AdapterType, context: AdaptContext = None, ) -> AdapterType: return Adapter.register(cls, adapter, context, format=Format.BINARY) @@ -122,7 +111,7 @@ class Typecaster: def register( oid: Oid, caster: TypecasterType, - context: Optional[AdaptContext] = None, + context: AdaptContext = None, format: Format = Format.TEXT, ) -> TypecasterType: if not isinstance(oid, int): @@ -153,9 +142,7 @@ class Typecaster: @staticmethod def register_binary( - oid: Oid, - caster: TypecasterType, - context: Optional[AdaptContext] = None, + oid: Oid, caster: TypecasterType, context: AdaptContext = None, ) -> TypecasterType: return Typecaster.register(oid, caster, context, format=Format.BINARY) @@ -188,7 +175,7 @@ class Transformer: connection: Optional[BaseConnection] cursor: Optional[BaseCursor] - def __init__(self, context: Optional[AdaptContext]): + def __init__(self, context: AdaptContext): if context is None: self.connection = None self.cursor = None diff --git a/psycopg3/connection.py b/psycopg3/connection.py index 8c68b7a73..cf62cc31b 100644 --- a/psycopg3/connection.py +++ b/psycopg3/connection.py @@ -8,17 +8,8 @@ import codecs import logging import asyncio import threading -from typing import ( - cast, - Any, - Generator, - List, - Optional, - Tuple, - Type, - TypeVar, - TYPE_CHECKING, -) +from typing import Any, Generator, List, Optional, Tuple, Type, TypeVar +from typing import cast, TYPE_CHECKING from . import pq from . import exceptions as exc diff --git a/psycopg3/utils/queries.py b/psycopg3/utils/queries.py index 52f2ece05..6ca0b83ab 100644 --- a/psycopg3/utils/queries.py +++ b/psycopg3/utils/queries.py @@ -6,18 +6,8 @@ Utility module to manipulate queries import re from codecs import CodecInfo -from typing import ( - Any, - Dict, - List, - Mapping, - Match, - NamedTuple, - Optional, - Sequence, - Tuple, - Union, -) +from typing import Any, Dict, List, Mapping, Match, NamedTuple, Optional +from typing import Sequence, Tuple, Union from .. import exceptions as exc from ..pq import Format -- 2.47.3