| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 7f96290 commit 210a14a
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,7 +15,14 @@ | |||
| 15 | 15 | """Helpers for applying Google Cloud Firestore changes in a transaction.""" | |
| 16 | 16 | from __future__ import annotations | |
| 17 | 17 | ||
| 18 | - from typing import TYPE_CHECKING, Any, AsyncGenerator, Callable, Coroutine, Optional | ||
| 18 | + from typing import ( | ||
| 19 | + TYPE_CHECKING, | ||
| 20 | + Any, | ||
| 21 | + AsyncGenerator, | ||
| 22 | + Awaitable, | ||
| 23 | + Callable, | ||
| 24 | + Optional, | ||
| 25 | + ) | ||
| 19 | 26 | ||
| 20 | 27 | from google.api_core import exceptions, gapic_v1 | |
| 21 | 28 | from google.api_core import retry_async as retries | |
@@ -37,11 +44,15 @@ | |||
| 37 | 44 | # Types needed only for Type Hints | |
| 38 | 45 | if TYPE_CHECKING: # pragma: NO COVER | |
| 39 | 46 | import datetime | |
| 47 | + from typing_extensions import TypeVar, ParamSpec, Concatenate | ||
| 40 | 48 | ||
| 41 | 49 | from google.cloud.firestore_v1.async_stream_generator import AsyncStreamGenerator | |
| 42 | 50 | from google.cloud.firestore_v1.base_document import DocumentSnapshot | |
| 43 | 51 | from google.cloud.firestore_v1.query_profile import ExplainOptions | |
| 44 | 52 | ||
| 53 | + T = TypeVar("T") | ||
| 54 | + P = ParamSpec("P") | ||
| 55 | + | ||
| 45 | 56 | ||
| 46 | 57 | class AsyncTransaction(async_batch.AsyncWriteBatch, BaseTransaction): | |
| 47 | 58 | """Accumulate read-and-write operations to be sent in a transaction. | |
@@ -253,12 +264,14 @@ class _AsyncTransactional(_BaseTransactional): | |||
| 253 | 264 | A coroutine that should be run (and retried) in a transaction. | |
| 254 | 265 | """ | |
| 255 | 266 | ||
| 256 | - def __init__(self, to_wrap) -> None: | ||
| 267 | + def __init__( | ||
| 268 | + self, to_wrap: Callable[Concatenate[AsyncTransaction, P], Awaitable[T]] | ||
| 269 | + ) -> None: | ||
| 257 | 270 | super(_AsyncTransactional, self).__init__(to_wrap) | |
| 258 | 271 | ||
| 259 | 272 | async def _pre_commit( | |
| 260 | - self, transaction: AsyncTransaction, *args, **kwargs | ||
| 261 | - ) -> Coroutine: | ||
| 273 | + self, transaction: AsyncTransaction, *args: P.args, **kwargs: P.kwargs | ||
| 274 | + ) -> T: | ||
| 262 | 275 | """Begin transaction and call the wrapped coroutine. | |
| 263 | 276 | ||
| 264 | 277 | Args: | |
@@ -271,7 +284,7 @@ async def _pre_commit( | |||
| 271 | 284 | along to the wrapped coroutine. | |
| 272 | 285 | ||
| 273 | 286 | Returns: | |
| 274 | - Any: result of the wrapped coroutine. | ||
| 287 | + T: result of the wrapped coroutine. | ||
| 275 | 288 | ||
| 276 | 289 | Raises: | |
| 277 | 290 | Exception: Any failure caused by ``to_wrap``. | |
@@ -286,20 +299,22 @@ async def _pre_commit( | |||
| 286 | 299 | self.retry_id = self.current_id | |
| 287 | 300 | return await self.to_wrap(transaction, *args, **kwargs) | |
| 288 | 301 | ||
| 289 | - async def __call__(self, transaction, *args, **kwargs): | ||
| 302 | + async def __call__( | ||
| 303 | + self, transaction: AsyncTransaction, *args: P.args, **kwargs: P.kwargs | ||
| 304 | + ) -> T: | ||
| 290 | 305 | """Execute the wrapped callable within a transaction. | |
| 291 | 306 | ||
| 292 | 307 | Args: | |
| 293 | 308 | transaction | |
| 294 | - (:class:`~google.cloud.firestore_v1.transaction.Transaction`): | ||
| 309 | + (:class:`~google.cloud.firestore_v1.async_transaction.AsyncTransaction`): | ||
| 295 | 310 | A transaction to execute the callable within. | |
| 296 | 311 | args (Tuple[Any, ...]): The extra positional arguments to pass | |
| 297 | 312 | along to the wrapped callable. | |
| 298 | 313 | kwargs (Dict[str, Any]): The extra keyword arguments to pass | |
| 299 | 314 | along to the wrapped callable. | |
| 300 | 315 | ||
| 301 | 316 | Returns: | |
| 302 | - Any: The result of the wrapped callable. | ||
| 317 | + T: The result of the wrapped callable. | ||
| 303 | 318 | ||
| 304 | 319 | Raises: | |
| 305 | 320 | ValueError: If the transaction does not succeed in | |
@@ -313,7 +328,7 @@ async def __call__(self, transaction, *args, **kwargs): | |||
| 313 | 328 | ||
| 314 | 329 | try: | |
| 315 | 330 | for attempt in range(transaction._max_attempts): | |
| 316 | - result = await self._pre_commit(transaction, *args, **kwargs) | ||
| 331 | + result: T = await self._pre_commit(transaction, *args, **kwargs) | ||
| 317 | 332 | try: | |
| 318 | 333 | await transaction._commit() | |
| 319 | 334 | return result | |
@@ -338,17 +353,17 @@ async def __call__(self, transaction, *args, **kwargs): | |||
| 338 | 353 | ||
| 339 | 354 | ||
| 340 | 355 | def async_transactional( | |
| 341 | - to_wrap: Callable[[AsyncTransaction], Any] | ||
| 342 | - ) -> _AsyncTransactional: | ||
| 356 | + to_wrap: Callable[Concatenate[AsyncTransaction, P], Awaitable[T]] | ||
| 357 | + ) -> Callable[Concatenate[AsyncTransaction, P], Awaitable[T]]: | ||
| 343 | 358 | """Decorate a callable so that it runs in a transaction. | |
| 344 | 359 | ||
| 345 | 360 | Args: | |
| 346 | 361 | to_wrap | |
| 347 | - (Callable[[:class:`~google.cloud.firestore_v1.transaction.Transaction`, ...], Any]): | ||
| 362 | + (Callable[[:class:`~google.cloud.firestore_v1.async_transaction.AsyncTransaction`, ...], Awaitable[Any]]): | ||
| 348 | 363 | A callable that should be run (and retried) in a transaction. | |
| 349 | 364 | ||
| 350 | 365 | Returns: | |
| 351 | - Callable[[:class:`~google.cloud.firestore_v1.transaction.Transaction`, ...], Any]: | ||
| 366 | + Callable[[:class:`~google.cloud.firestore_v1.transaction.Transaction`, ...], Awaitable[Any]]: | ||
| 352 | 367 | the wrapped callable. | |
| 353 | 368 | """ | |
| 354 | 369 | return _AsyncTransactional(to_wrap) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,3 +1,3 @@ | |||
| 1 | 1 | [mypy] | |
| 2 | - python_version = 3.6 | ||
| 2 | + python_version = 3.8 | ||
| 3 | 3 | namespace_packages = True | |
| Back | FazBrowse Home | New Git URL |
0 commit comments