sqlakit 0.5.1__tar.gz → 0.6.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (26) hide show
  1. {sqlakit-0.5.1 → sqlakit-0.6.0}/PKG-INFO +1 -1
  2. {sqlakit-0.5.1 → sqlakit-0.6.0}/pyproject.toml +1 -1
  3. {sqlakit-0.5.1 → sqlakit-0.6.0}/pyproject.toml.orig +1 -1
  4. {sqlakit-0.5.1 → sqlakit-0.6.0}/sqlakit/__init__.py +10 -1
  5. {sqlakit-0.5.1 → sqlakit-0.6.0}/sqlakit/_query.py +54 -16
  6. {sqlakit-0.5.1 → sqlakit-0.6.0}/sqlakit/asyncio/orm.py +11 -1
  7. {sqlakit-0.5.1 → sqlakit-0.6.0}/sqlakit/orm.py +13 -1
  8. {sqlakit-0.5.1 → sqlakit-0.6.0}/LICENSE +0 -0
  9. {sqlakit-0.5.1 → sqlakit-0.6.0}/README.md +0 -0
  10. {sqlakit-0.5.1 → sqlakit-0.6.0}/sqlakit/_base.py +0 -0
  11. {sqlakit-0.5.1 → sqlakit-0.6.0}/sqlakit/_db.py +0 -0
  12. {sqlakit-0.5.1 → sqlakit-0.6.0}/sqlakit/_discovery.py +0 -0
  13. {sqlakit-0.5.1 → sqlakit-0.6.0}/sqlakit/_model.py +0 -0
  14. {sqlakit-0.5.1 → sqlakit-0.6.0}/sqlakit/_recording.py +0 -0
  15. {sqlakit-0.5.1 → sqlakit-0.6.0}/sqlakit/_registry.py +0 -0
  16. {sqlakit-0.5.1 → sqlakit-0.6.0}/sqlakit/_routing.py +0 -0
  17. {sqlakit-0.5.1 → sqlakit-0.6.0}/sqlakit/_sql.py +0 -0
  18. {sqlakit-0.5.1 → sqlakit-0.6.0}/sqlakit/asyncio/__init__.py +0 -0
  19. {sqlakit-0.5.1 → sqlakit-0.6.0}/sqlakit/asyncio/_db.py +0 -0
  20. {sqlakit-0.5.1 → sqlakit-0.6.0}/sqlakit/asyncio/_registry.py +0 -0
  21. {sqlakit-0.5.1 → sqlakit-0.6.0}/sqlakit/asyncio/sql.py +0 -0
  22. {sqlakit-0.5.1 → sqlakit-0.6.0}/sqlakit/exceptions.py +0 -0
  23. {sqlakit-0.5.1 → sqlakit-0.6.0}/sqlakit/py.typed +0 -0
  24. {sqlakit-0.5.1 → sqlakit-0.6.0}/sqlakit/sql.py +0 -0
  25. {sqlakit-0.5.1 → sqlakit-0.6.0}/sqlakit/testing.py +0 -0
  26. {sqlakit-0.5.1 → sqlakit-0.6.0}/sqlakit/types.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sqlakit
3
- Version: 0.5.1
3
+ Version: 0.6.0
4
4
  Summary: A toolkit for SQLAlchemy applications.
5
5
  Keywords: sqlalchemy,database,orm,sql,asyncio
6
6
  Author: Anton Ruhlov
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "sqlakit"
3
- version = "0.5.1"
3
+ version = "0.6.0"
4
4
  description = "A toolkit for SQLAlchemy applications."
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "sqlakit"
3
- version = "0.5.1"
3
+ version = "0.6.0"
4
4
  description = "A toolkit for SQLAlchemy applications."
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -1,7 +1,14 @@
1
1
  from ._base import DEFAULT_ENGINE_ARGS, DEFAULT_SESSION_ARGS
2
2
  from ._db import Database, RetryingTransaction, Transaction
3
3
  from ._discovery import import_models, import_string
4
- from ._query import CASE_INSENSITIVE_COLLATIONS, CursorPage, OrderBy, Page
4
+ from ._query import (
5
+ CASE_INSENSITIVE_COLLATIONS,
6
+ CursorPage,
7
+ OrderBy,
8
+ Page,
9
+ UncountedPage,
10
+ orderable_columns,
11
+ )
5
12
  from ._recording import Recording, Statement
6
13
  from ._registry import Databases, db
7
14
  from ._routing import Router
@@ -102,6 +109,7 @@ __all__ = [
102
109
  "Transaction",
103
110
  "TransactionRolledBackError",
104
111
  "UncomparableOrderingError",
112
+ "UncountedPage",
105
113
  "UnknownDatabaseError",
106
114
  "UnknownFieldError",
107
115
  "UnknownImportPathError",
@@ -111,4 +119,5 @@ __all__ = [
111
119
  "db",
112
120
  "import_models",
113
121
  "import_string",
122
+ "orderable_columns",
114
123
  ]
@@ -14,7 +14,7 @@ from typing import (
14
14
  NamedTuple,
15
15
  Protocol,
16
16
  Self,
17
- TypeVar,
17
+ TypeAlias,
18
18
  cast,
19
19
  )
20
20
 
@@ -31,6 +31,9 @@ from sqlalchemy.orm import (
31
31
  from sqlalchemy.orm.exc import MultipleResultsFound, NoResultFound
32
32
  from sqlalchemy.sql import operators
33
33
 
34
+ # `TypeVar` from here, for the default that keeps `Page[User]` the counted one.
35
+ from typing_extensions import TypeVar
36
+
34
37
  from ._model import resolve_alias, soft_delete_column
35
38
  from .exceptions import (
36
39
  BulkQueryError,
@@ -63,9 +66,11 @@ __all__ = [
63
66
  "NullsPlacement",
64
67
  "OrderBy",
65
68
  "Page",
69
+ "UncountedPage",
66
70
  "one_row",
67
71
  "one_row_or_none",
68
72
  "orderable",
73
+ "orderable_columns",
69
74
  "ordered",
70
75
  ]
71
76
 
@@ -100,6 +105,8 @@ ONLY = "only"
100
105
 
101
106
  ModelT = TypeVar("ModelT")
102
107
  OtherT = TypeVar("OtherT")
108
+ TotalT = TypeVar("TotalT", bound="int | None", default=int)
109
+ """What a page knows about its total: an `int`, or `None` when nobody counted."""
103
110
  RowT = TypeVar("RowT")
104
111
  RowT_co = TypeVar("RowT_co", covariant=True)
105
112
 
@@ -170,18 +177,19 @@ def _compile_nulls_for_mysql(
170
177
 
171
178
 
172
179
  @dataclass(frozen=True, slots=True)
173
- class Page(Generic[ModelT]):
174
- """One page of rows, and how many there are in total."""
180
+ class Page(Generic[ModelT, TotalT]):
181
+ """One page of rows, and how many there are in total.
175
182
 
176
- items: Sequence[ModelT]
177
- total: int | None
178
- """How many rows match, or None for a page read without counting them."""
183
+ ``Page[User]`` is the counted page, whose ``total`` is an `int`.
184
+ ``page(total=False)`` returns a `Page[User, None]` instead, so the code
185
+ reading it is not asked about a total nobody counted.
186
+ """
179
187
 
188
+ items: Sequence[ModelT]
189
+ total: TotalT
180
190
  limit: int
181
191
  offset: int
182
-
183
192
  has_next: bool = False
184
- """Whether a page follows this one."""
185
193
 
186
194
  def __post_init__(self) -> None:
187
195
  if self.total is not None:
@@ -189,7 +197,7 @@ class Page(Generic[ModelT]):
189
197
  self, "has_next", self.offset + len(self.items) < self.total
190
198
  )
191
199
 
192
- def map(self, transform: Callable[[ModelT], OtherT]) -> Page[OtherT]:
200
+ def map(self, transform: Callable[[ModelT], OtherT]) -> Page[OtherT, TotalT]:
193
201
  """Return the page with every row put through ``transform``.
194
202
 
195
203
  ```python
@@ -201,7 +209,7 @@ class Page(Generic[ModelT]):
201
209
  def map_all(
202
210
  self,
203
211
  transform: Callable[[Sequence[ModelT]], Sequence[OtherT]],
204
- ) -> Page[OtherT]:
212
+ ) -> Page[OtherT, TotalT]:
205
213
  """Return the page with the rows put through ``transform`` together.
206
214
 
207
215
  For work that reads better in one go than row by row: one query for
@@ -209,7 +217,7 @@ class Page(Generic[ModelT]):
209
217
  """
210
218
  return self.with_items(transform(self.items))
211
219
 
212
- def with_items(self, items: Sequence[OtherT]) -> Page[OtherT]:
220
+ def with_items(self, items: Sequence[OtherT]) -> Page[OtherT, TotalT]:
213
221
  """Return the page carrying these rows instead, counts unchanged.
214
222
 
215
223
  What an asynchronous transform needs: `page.with_items(await serialize(...))`.
@@ -229,16 +237,24 @@ class Page(Generic[ModelT]):
229
237
  )
230
238
 
231
239
 
240
+ UncountedPage: TypeAlias = Page[ModelT, None]
241
+ """A page read with ``total=False``, which counted nothing.
242
+
243
+ The same class, named for what a signature means by it:
244
+
245
+ ```python
246
+ def feed(page: UncountedPage[User]) -> Response: ...
247
+ ```
248
+ """
249
+
250
+
232
251
  @dataclass(frozen=True, slots=True)
233
252
  class CursorPage(Generic[ModelT]):
234
253
  """One page of rows, and the cursors that read the ones either side."""
235
254
 
236
255
  items: Sequence[ModelT]
237
256
  next_cursor: str | None = None
238
- """Hand it back as ``cursor`` to read on."""
239
-
240
257
  previous_cursor: str | None = None
241
- """Hand it back as ``cursor`` to read the page in front of this one."""
242
258
 
243
259
  @property
244
260
  def has_next(self) -> bool:
@@ -921,6 +937,29 @@ class BaseQuery(Generic[ModelT]):
921
937
  return _encode(values, backwards=backwards, ordering=ordering_key)
922
938
 
923
939
 
940
+ def orderable_columns(model: type[Any]) -> Mapping[str, Any]:
941
+ """Return every mapped column of a model, by name.
942
+
943
+ What a model orders by when it declares no ``__orderable__``, and what an
944
+ ``__orderable__`` that adds to the columns rather than replacing them starts
945
+ from:
946
+
947
+ ```python
948
+ @classmethod
949
+ def __orderable__(cls) -> Mapping[str, Any]:
950
+ return {
951
+ **orderable_columns(cls),
952
+ "team": OrderBy(Team.name, join=cls.team),
953
+ }
954
+ ```
955
+
956
+ Calling `orderable` there instead recurses: it reads the very
957
+ ``__orderable__`` that is running.
958
+ """
959
+ mapper = sa.inspect(model, raiseerr=True)
960
+ return {attr.key: getattr(model, attr.key) for attr in mapper.column_attrs}
961
+
962
+
924
963
  def orderable(model: type[Any]) -> Mapping[str, Any]:
925
964
  """Return the fields a model can be ordered by name.
926
965
 
@@ -930,8 +969,7 @@ def orderable(model: type[Any]) -> Mapping[str, Any]:
930
969
  """
931
970
  fields = getattr(model, "__orderable__", None)
932
971
  if fields is None:
933
- mapper = sa.inspect(model, raiseerr=True)
934
- return {attr.key: getattr(model, attr.key) for attr in mapper.column_attrs}
972
+ return orderable_columns(model)
935
973
  if callable(fields):
936
974
  return fields()
937
975
  return {name: _mapped_column(model, name) for name in fields}
@@ -197,9 +197,19 @@ class Query(BaseQuery[ModelT]):
197
197
  """Check whether any row matches."""
198
198
  return bool(await self.db.session.scalar(self._exists_statement()))
199
199
 
200
+ @overload
201
+ async def page(
202
+ self, *, limit: int, offset: int = 0, total: Literal[True] = True
203
+ ) -> Page[ModelT]: ...
204
+
205
+ @overload
206
+ async def page(
207
+ self, *, limit: int, offset: int = 0, total: Literal[False]
208
+ ) -> Page[ModelT, None]: ...
209
+
200
210
  async def page(
201
211
  self, *, limit: int, offset: int = 0, total: bool = True
202
- ) -> Page[ModelT]:
212
+ ) -> Page[ModelT] | Page[ModelT, None]:
203
213
  """Read one page of rows, with the total.
204
214
 
205
215
  The model's key is appended to the ordering, so a row that ties with another
@@ -190,7 +190,19 @@ class Query(BaseQuery[ModelT]):
190
190
  """Check whether any row matches."""
191
191
  return bool(self.db.session.scalar(self._exists_statement()))
192
192
 
193
- def page(self, *, limit: int, offset: int = 0, total: bool = True) -> Page[ModelT]:
193
+ @overload
194
+ def page(
195
+ self, *, limit: int, offset: int = 0, total: Literal[True] = True
196
+ ) -> Page[ModelT]: ...
197
+
198
+ @overload
199
+ def page(
200
+ self, *, limit: int, offset: int = 0, total: Literal[False]
201
+ ) -> Page[ModelT, None]: ...
202
+
203
+ def page(
204
+ self, *, limit: int, offset: int = 0, total: bool = True
205
+ ) -> Page[ModelT] | Page[ModelT, None]:
194
206
  """Read one page of rows, with the total.
195
207
 
196
208
  The model's key is appended to the ordering, so a row that ties with another
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes