From: Yurii Karabas <1998uriyyo@gmail.com> Date: Tue, 14 Nov 2023 17:04:38 +0000 (+0200) Subject: update typing X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=283e2d65187431f19b927eb499fc954f6ecaecfb;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git update typing --- diff --git a/lib/sqlalchemy/engine/result.py b/lib/sqlalchemy/engine/result.py index 579528d966..239af98951 100644 --- a/lib/sqlalchemy/engine/result.py +++ b/lib/sqlalchemy/engine/result.py @@ -66,7 +66,7 @@ _KeyMapRecType = Any _KeyMapType = Mapping[_KeyType, _KeyMapRecType] -_RowData = Union[Row[Any], RowMapping, Any] +_RowData = Union[Row[Unpack[Tuple[Any, ...]]], RowMapping, Any] """A generic form of "row" that accommodates for the different kinds of "rows" that different result objects return, including row, row mapping, and scalar values""" @@ -85,7 +85,7 @@ across all the result types """ -_InterimSupportsScalarsRowType = Union[Row[Any], Any] +_InterimSupportsScalarsRowType = Union[Row[Unpack[_RawRowType]], Any] _ProcessorsType = Sequence[Optional["_ResultProcessorType[Any]"]] _TupleGetterType = Callable[[Sequence[Any]], Sequence[Any]] @@ -171,7 +171,7 @@ class ResultMetaData: def _getter( self, key: Any, raiseerr: bool = True - ) -> Optional[Callable[[Row[Any]], Any]]: + ) -> Optional[Callable[[Row[Unpack[Tuple[Any, ...]]]], Any]]: index = self._index_for_key(key, raiseerr) if index is not None: @@ -332,7 +332,7 @@ class SimpleResultMetaData(ResultMetaData): _tuplefilter=_tuplefilter, ) - def _contains(self, value: Any, row: Row[Any]) -> bool: + def _contains(self, value: Any, row: Row[Unpack[_RawRowType]]) -> bool: return value in row._data def _index_for_key(self, key: Any, raiseerr: bool = True) -> int: @@ -429,20 +429,24 @@ class ResultInternal(InPlaceGenerative, Generic[_R]): _source_supports_scalars: bool - def _fetchiter_impl(self) -> Iterator[_InterimRowType[Row[Any]]]: + def _fetchiter_impl( + self, + ) -> Iterator[_InterimRowType[Row[Unpack[Tuple[Any, ...]]]]]: raise NotImplementedError() def _fetchone_impl( self, hard_close: bool = False - ) -> Optional[_InterimRowType[Row[Any]]]: + ) -> Optional[_InterimRowType[Row[Unpack[Tuple[Any, ...]]]]]: raise NotImplementedError() def _fetchmany_impl( self, size: Optional[int] = None - ) -> List[_InterimRowType[Row[Any]]]: + ) -> List[_InterimRowType[Row[Unpack[Tuple[Any, ...]]]]]: raise NotImplementedError() - def _fetchall_impl(self) -> List[_InterimRowType[Row[Any]]]: + def _fetchall_impl( + self, + ) -> List[_InterimRowType[Row[Unpack[Tuple[Any, ...]]]]]: raise NotImplementedError() def _soft_close(self, hard: bool = False) -> None: @@ -467,7 +471,7 @@ class ResultInternal(InPlaceGenerative, Generic[_R]): processors: Optional[_ProcessorsType], key_to_index: Mapping[_KeyType, int], scalar_obj: Any, - ) -> Row[Any]: + ) -> Row[Unpack[_RawRowType]]: return _proc( metadata, processors, key_to_index, (scalar_obj,) ) @@ -491,7 +495,9 @@ class ResultInternal(InPlaceGenerative, Generic[_R]): fixed_tf = tf - def make_row(row: _InterimRowType[Row[Any]]) -> _R: + def make_row( + row: _InterimRowType[Row[Unpack[Tuple[Any, ...]]]] + ) -> _R: return _make_row_orig(fixed_tf(row)) else: @@ -503,7 +509,9 @@ class ResultInternal(InPlaceGenerative, Generic[_R]): _log_row = real_result._row_logging_fn _make_row = make_row - def make_row(row: _InterimRowType[Row[Any]]) -> _R: + def make_row( + row: _InterimRowType[Row[Unpack[Tuple[Any, ...]]]] + ) -> _R: return _log_row(_make_row(row)) # type: ignore return make_row @@ -948,7 +956,9 @@ class Result(_WithKeys, ResultInternal[Row[Unpack[_Ts]]]): __slots__ = ("_metadata", "__dict__") - _row_logging_fn: Optional[Callable[[Row[Any]], Row[Any]]] = None + _row_logging_fn: Optional[ + Callable[[Row[Unpack[Tuple[Any, ...]]]], Row[Unpack[Tuple[Any, ...]]]] + ] = None _source_supports_scalars: bool = False @@ -1183,7 +1193,7 @@ class Result(_WithKeys, ResultInternal[Row[Unpack[_Ts]]]): def _getter( self, key: _KeyIndexType, raiseerr: bool = True - ) -> Optional[Callable[[Row[Any]], Any]]: + ) -> Optional[Callable[[Row[Unpack[Tuple[Any, ...]]]], Any]]: """return a callable that will retrieve the given key from a :class:`_engine.Row`. @@ -1696,20 +1706,24 @@ class FilterResult(ResultInternal[_R]): def _attributes(self) -> Dict[Any, Any]: return self._real_result._attributes - def _fetchiter_impl(self) -> Iterator[_InterimRowType[Row[Any]]]: + def _fetchiter_impl( + self, + ) -> Iterator[_InterimRowType[Row[Unpack[Tuple[Any, ...]]]]]: return self._real_result._fetchiter_impl() def _fetchone_impl( self, hard_close: bool = False - ) -> Optional[_InterimRowType[Row[Any]]]: + ) -> Optional[_InterimRowType[Row[Unpack[Tuple[Any, ...]]]]]: return self._real_result._fetchone_impl(hard_close=hard_close) - def _fetchall_impl(self) -> List[_InterimRowType[Row[Any]]]: + def _fetchall_impl( + self, + ) -> List[_InterimRowType[Row[Unpack[Tuple[Any, ...]]]]]: return self._real_result._fetchall_impl() def _fetchmany_impl( self, size: Optional[int] = None - ) -> List[_InterimRowType[Row[Any]]]: + ) -> List[_InterimRowType[Row[Unpack[Tuple[Any, ...]]]]]: return self._real_result._fetchmany_impl(size=size) @@ -2292,7 +2306,7 @@ class IteratorResult(Result[Unpack[_Ts]]): def _fetchone_impl( self, hard_close: bool = False - ) -> Optional[_InterimRowType[Row[Any]]]: + ) -> Optional[_InterimRowType[Row[Unpack[Tuple[Any, ...]]]]]: if self._hard_closed: self._raise_hard_closed() @@ -2303,7 +2317,9 @@ class IteratorResult(Result[Unpack[_Ts]]): else: return row - def _fetchall_impl(self) -> List[_InterimRowType[Row[Any]]]: + def _fetchall_impl( + self, + ) -> List[_InterimRowType[Row[Unpack[Tuple[Any, ...]]]]]: if self._hard_closed: self._raise_hard_closed() try: @@ -2313,7 +2329,7 @@ class IteratorResult(Result[Unpack[_Ts]]): def _fetchmany_impl( self, size: Optional[int] = None - ) -> List[_InterimRowType[Row[Any]]]: + ) -> List[_InterimRowType[Row[Unpack[Tuple[Any, ...]]]]]: if self._hard_closed: self._raise_hard_closed() @@ -2375,7 +2391,7 @@ class ChunkedIteratorResult(IteratorResult[Unpack[_Ts]]): def _fetchmany_impl( self, size: Optional[int] = None - ) -> List[_InterimRowType[Row[Any]]]: + ) -> List[_InterimRowType[Row[Unpack[Tuple[Any, ...]]]]]: if self.dynamic_yield_per: self.iterator = itertools.chain.from_iterable(self.chunks(size)) return super()._fetchmany_impl(size=size) diff --git a/lib/sqlalchemy/ext/asyncio/scoping.py b/lib/sqlalchemy/ext/asyncio/scoping.py index 3e1f7ecb33..0f7f93ecec 100644 --- a/lib/sqlalchemy/ext/asyncio/scoping.py +++ b/lib/sqlalchemy/ext/asyncio/scoping.py @@ -1594,7 +1594,7 @@ class async_scoped_session(Generic[_AS]): ident: Union[Any, Tuple[Any, ...]] = None, *, instance: Optional[Any] = None, - row: Optional[Union[Row[Any], RowMapping]] = None, + row: Optional[Union[Row[Unpack[Tuple[Any, ...]]], RowMapping]] = None, identity_token: Optional[Any] = None, ) -> _IdentityKeyType[Any]: r"""Return an identity key. diff --git a/lib/sqlalchemy/ext/asyncio/session.py b/lib/sqlalchemy/ext/asyncio/session.py index 36a73ef6aa..6a78a0978e 100644 --- a/lib/sqlalchemy/ext/asyncio/session.py +++ b/lib/sqlalchemy/ext/asyncio/session.py @@ -1591,7 +1591,7 @@ class AsyncSession(ReversibleProxy[Session]): ident: Union[Any, Tuple[Any, ...]] = None, *, instance: Optional[Any] = None, - row: Optional[Union[Row[Any], RowMapping]] = None, + row: Optional[Union[Row[Unpack[Tuple[Any, ...]]], RowMapping]] = None, identity_token: Optional[Any] = None, ) -> _IdentityKeyType[Any]: r"""Return an identity key. diff --git a/lib/sqlalchemy/orm/descriptor_props.py b/lib/sqlalchemy/orm/descriptor_props.py index c1fe9de85c..5880aad06a 100644 --- a/lib/sqlalchemy/orm/descriptor_props.py +++ b/lib/sqlalchemy/orm/descriptor_props.py @@ -56,6 +56,8 @@ from ..sql.elements import BindParameter from ..util.typing import is_fwd_ref from ..util.typing import is_pep593 from ..util.typing import typing_get_args +from ..util.typing import Unpack + if typing.TYPE_CHECKING: from ._typing import _InstanceDict @@ -714,10 +716,10 @@ class CompositeProperty( def create_row_processor( self, query: Select[Any], - procs: Sequence[Callable[[Row[Any]], Any]], + procs: Sequence[Callable[[Row[Unpack[Tuple[Any, ...]]]], Any]], labels: Sequence[str], - ) -> Callable[[Row[Any]], Any]: - def proc(row: Row[Any]) -> Any: + ) -> Callable[[Row[Unpack[Tuple[Any, ...]]]], Any]: + def proc(row: Row[Unpack[Tuple[Any, ...]]]) -> Any: return self.property.composite_class( *[proc(row) for proc in procs] ) diff --git a/lib/sqlalchemy/orm/mapper.py b/lib/sqlalchemy/orm/mapper.py index b686996370..1228931a74 100644 --- a/lib/sqlalchemy/orm/mapper.py +++ b/lib/sqlalchemy/orm/mapper.py @@ -89,6 +89,7 @@ from ..sql.selectable import LABEL_STYLE_TABLENAME_PLUS_COL from ..util import HasMemoized from ..util import HasMemoized_ro_memoized_attribute from ..util.typing import Literal +from ..util.typing import Unpack if TYPE_CHECKING: from ._typing import _IdentityKeyType @@ -3428,7 +3429,7 @@ class Mapper( def identity_key_from_row( self, - row: Optional[Union[Row[Any], RowMapping]], + row: Optional[Union[Row[Unpack[Tuple[Any, ...]]], RowMapping]], identity_token: Optional[Any] = None, adapter: Optional[ORMAdapter] = None, ) -> _IdentityKeyType[_O]: diff --git a/lib/sqlalchemy/orm/scoping.py b/lib/sqlalchemy/orm/scoping.py index 65ab1abef7..5b5dc655a0 100644 --- a/lib/sqlalchemy/orm/scoping.py +++ b/lib/sqlalchemy/orm/scoping.py @@ -2154,7 +2154,7 @@ class scoped_session(Generic[_S]): ident: Union[Any, Tuple[Any, ...]] = None, *, instance: Optional[Any] = None, - row: Optional[Union[Row[Any], RowMapping]] = None, + row: Optional[Union[Row[Unpack[Tuple[Any, ...]]], RowMapping]] = None, identity_token: Optional[Any] = None, ) -> _IdentityKeyType[Any]: r"""Return an identity key. diff --git a/lib/sqlalchemy/orm/session.py b/lib/sqlalchemy/orm/session.py index 45925a6d9f..eb4330b17c 100644 --- a/lib/sqlalchemy/orm/session.py +++ b/lib/sqlalchemy/orm/session.py @@ -226,7 +226,7 @@ class _SessionClassMethods: ident: Union[Any, Tuple[Any, ...]] = None, *, instance: Optional[Any] = None, - row: Optional[Union[Row[Any], RowMapping]] = None, + row: Optional[Union[Row[Unpack[Tuple[Any, ...]]], RowMapping]] = None, identity_token: Optional[Any] = None, ) -> _IdentityKeyType[Any]: """Return an identity key. diff --git a/lib/sqlalchemy/orm/state.py b/lib/sqlalchemy/orm/state.py index d9e1f854d7..d142f075c4 100644 --- a/lib/sqlalchemy/orm/state.py +++ b/lib/sqlalchemy/orm/state.py @@ -46,6 +46,7 @@ from .. import inspection from .. import util from ..util.typing import Literal from ..util.typing import Protocol +from ..util.typing import Unpack if TYPE_CHECKING: from ._typing import _IdentityKeyType @@ -93,7 +94,10 @@ class _InstallLoaderCallableProto(Protocol[_O]): """ def __call__( - self, state: InstanceState[_O], dict_: _InstanceDict, row: Row[Any] + self, + state: InstanceState[_O], + dict_: _InstanceDict, + row: Row[Unpack[Tuple[Any, ...]]], ) -> None: ... @@ -673,7 +677,9 @@ class InstanceState(interfaces.InspectionAttrInfo, Generic[_O]): fixed_impl = impl def _set_callable( - state: InstanceState[_O], dict_: _InstanceDict, row: Row[Any] + state: InstanceState[_O], + dict_: _InstanceDict, + row: Row[Unpack[Tuple[Any, ...]]], ) -> None: if "callables" not in state.__dict__: state.callables = {} @@ -685,7 +691,9 @@ class InstanceState(interfaces.InspectionAttrInfo, Generic[_O]): else: def _set_callable( - state: InstanceState[_O], dict_: _InstanceDict, row: Row[Any] + state: InstanceState[_O], + dict_: _InstanceDict, + row: Row[Unpack[Tuple[Any, ...]]], ) -> None: if "callables" not in state.__dict__: state.callables = {} diff --git a/lib/sqlalchemy/orm/util.py b/lib/sqlalchemy/orm/util.py index ea2f1a12e9..ae915ce3b2 100644 --- a/lib/sqlalchemy/orm/util.py +++ b/lib/sqlalchemy/orm/util.py @@ -93,6 +93,7 @@ from ..util.typing import is_origin_of_cls from ..util.typing import Literal from ..util.typing import Protocol from ..util.typing import typing_get_origin +from ..util.typing import Unpack if typing.TYPE_CHECKING: from ._typing import _EntityType @@ -426,7 +427,7 @@ def identity_key( ident: Union[Any, Tuple[Any, ...]] = None, *, instance: Optional[_T] = None, - row: Optional[Union[Row[Any], RowMapping]] = None, + row: Optional[Union[Row[Unpack[Tuple[Any, ...]]], RowMapping]] = None, identity_token: Optional[Any] = None, ) -> _IdentityKeyType[_T]: r"""Generate "identity key" tuples, as are used as keys in the @@ -1722,9 +1723,9 @@ class Bundle( def create_row_processor( self, query: Select[Any], - procs: Sequence[Callable[[Row[Any]], Any]], + procs: Sequence[Callable[[Row[Unpack[Tuple[Any, ...]]]], Any]], labels: Sequence[str], - ) -> Callable[[Row[Any]], Any]: + ) -> Callable[[Row[Unpack[Tuple[Any, ...]]]], Any]: """Produce the "row processing" function for this :class:`.Bundle`. May be overridden by subclasses to provide custom behaviors when @@ -1760,7 +1761,7 @@ class Bundle( """ keyed_tuple = result_tuple(labels, [() for l in labels]) - def proc(row: Row[Any]) -> Any: + def proc(row: Row[Unpack[Tuple[Any, ...]]]) -> Any: return keyed_tuple([proc(row) for proc in procs]) return proc