Skip to content

Commit 8d61227

Browse files
authored
feat: deprecate list()/list_and_count() (#706)
## Summary - Renames `list()` → `get_many()` and `list_and_count()` → `get_many_and_count()` across repository, service, memory repo, query repo, and cache manager layers - Old names preserved as deprecation wrappers using `warn_deprecation()`, scheduled for removal in 2.0
1 parent e1e16b1 commit 8d61227

42 files changed

Lines changed: 1378 additions & 300 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ with db.get_session() as db_session:
124124
print(f"Created {len(objs)} new objects.")
125125

126126
# 2) Select paginated data and total row count. Pass additional filters as kwargs
127-
created_objs, total_objs = repo.list_and_count(LimitOffset(limit=10, offset=0), name="Cody")
127+
created_objs, total_objs = repo.get_many_and_count(LimitOffset(limit=10, offset=0), name="Cody")
128128
print(f"Selected {len(created_objs)} records out of a total of {total_objs}.")
129129

130130
# 3) Let's remove the batch of records selected.
@@ -190,7 +190,7 @@ with db.get_session() as db_session:
190190
print(f"Created {len(objs)} new objects.")
191191

192192
# 2) Select paginated data and total row count. Pass additional filters as kwargs
193-
created_objs, total_objs = service.list_and_count(LimitOffset(limit=10, offset=0), name="Cody")
193+
created_objs, total_objs = service.get_many_and_count(LimitOffset(limit=10, offset=0), name="Cody")
194194
print(f"Selected {len(created_objs)} records out of a total of {total_objs}.")
195195

196196
# 3) Let's remove the batch of records selected.

advanced_alchemy/alembic/utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def _dump_table_sync(session: "AbstractContextManager[Session]") -> None:
115115
(SQLAlchemySyncRepository,),
116116
exec_body=lambda ns, model=model: ns.setdefault("model_type", model), # type: ignore[misc]
117117
)
118-
json_path.write_text(encode_json([model_to_dict(row) for row in repo(session=_session).list()]))
118+
json_path.write_text(encode_json([model_to_dict(row) for row in repo(session=_session).get_many()]))
119119

120120
async def _dump_table_async(session: "AbstractAsyncContextManager[AsyncSession]") -> None:
121121
from advanced_alchemy.repository import SQLAlchemyAsyncRepository
@@ -133,7 +133,9 @@ async def _dump_table_async(session: "AbstractAsyncContextManager[AsyncSession]"
133133
(SQLAlchemyAsyncRepository,),
134134
exec_body=lambda ns, model=model: ns.setdefault("model_type", model), # type: ignore[misc]
135135
)
136-
json_path.write_text(encode_json([model_to_dict(row) for row in await repo(session=_session).list()]))
136+
json_path.write_text(
137+
encode_json([model_to_dict(row) for row in await repo(session=_session).get_many()])
138+
)
137139

138140
await async_(dump_dir.mkdir)(exist_ok=True)
139141

advanced_alchemy/cache/manager.py

Lines changed: 133 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from advanced_alchemy.cache._null import NO_VALUE, NullRegion, SyncCacheRegionProtocol
1414
from advanced_alchemy.cache.serializers import default_deserializer, default_serializer
15+
from advanced_alchemy.utils.deprecation import warn_deprecation
1516
from advanced_alchemy.utils.sync_tools import async_
1617

1718
if TYPE_CHECKING:
@@ -437,7 +438,7 @@ def get_model_version_sync(self, model_name: str) -> str:
437438

438439
return "0"
439440

440-
def get_list_sync(self, key: str, model_class: type[T]) -> Optional[list[T]]:
441+
def get_many_sync(self, key: str, model_class: type[T]) -> Optional[list[T]]:
441442
"""Get a cached list of entities (sync).
442443
443444
The list is stored as base64-encoded serialized entity payloads.
@@ -470,7 +471,7 @@ def get_list_sync(self, key: str, model_class: type[T]) -> Optional[list[T]]:
470471
return None
471472
return results
472473

473-
def set_list_sync(self, key: str, items: list[Any]) -> None:
474+
def set_many_sync(self, key: str, items: list[Any]) -> None:
474475
"""Cache a list of entities (sync).
475476
476477
Args:
@@ -484,7 +485,7 @@ def set_list_sync(self, key: str, items: list[Any]) -> None:
484485
except Exception:
485486
logger.exception("Failed to serialize cached list for key %s", key)
486487

487-
def get_list_and_count_sync(self, key: str, model_class: type[T]) -> Optional[tuple[list[T], int]]:
488+
def get_many_and_count_sync(self, key: str, model_class: type[T]) -> Optional[tuple[list[T], int]]:
488489
"""Get a cached list+count payload (sync)."""
489490
cached = self.get_sync(key)
490491
if cached is DOGPILE_NO_VALUE or cached is NO_VALUE:
@@ -513,7 +514,7 @@ def get_list_and_count_sync(self, key: str, model_class: type[T]) -> Optional[tu
513514
return None
514515
return results, count_raw
515516

516-
def set_list_and_count_sync(self, key: str, items: list[Any], count: int) -> None:
517+
def set_many_and_count_sync(self, key: str, items: list[Any], count: int) -> None:
517518
"""Cache a list+count payload (sync)."""
518519
serializer = self.config.serializer or default_serializer
519520
try:
@@ -525,6 +526,66 @@ def set_list_and_count_sync(self, key: str, items: list[Any], count: int) -> Non
525526
except Exception:
526527
logger.exception("Failed to serialize cached list_and_count for key %s", key)
527528

529+
def get_list_sync(self, key: str, model_class: type[T]) -> Optional[list[T]]:
530+
"""Get a cached list of entities (sync).
531+
532+
.. deprecated:: 1.10.0
533+
Use :meth:`get_many_sync` instead.
534+
"""
535+
warn_deprecation(
536+
version="1.10.0",
537+
deprecated_name="get_list_sync",
538+
kind="method",
539+
removal_in="2.0.0",
540+
alternative="get_many_sync",
541+
)
542+
return self.get_many_sync(key, model_class)
543+
544+
def set_list_sync(self, key: str, items: list[Any]) -> None:
545+
"""Cache a list of entities (sync).
546+
547+
.. deprecated:: 1.10.0
548+
Use :meth:`set_many_sync` instead.
549+
"""
550+
warn_deprecation(
551+
version="1.10.0",
552+
deprecated_name="set_list_sync",
553+
kind="method",
554+
removal_in="2.0.0",
555+
alternative="set_many_sync",
556+
)
557+
self.set_many_sync(key, items)
558+
559+
def get_list_and_count_sync(self, key: str, model_class: type[T]) -> Optional[tuple[list[T], int]]:
560+
"""Get a cached list+count payload (sync).
561+
562+
.. deprecated:: 1.10.0
563+
Use :meth:`get_many_and_count_sync` instead.
564+
"""
565+
warn_deprecation(
566+
version="1.10.0",
567+
deprecated_name="get_list_and_count_sync",
568+
kind="method",
569+
removal_in="2.0.0",
570+
alternative="get_many_and_count_sync",
571+
)
572+
return self.get_many_and_count_sync(key, model_class)
573+
574+
def set_list_and_count_sync(self, key: str, items: list[Any], count: int) -> None:
575+
"""Cache a list+count payload (sync).
576+
577+
.. deprecated:: 1.10.0
578+
Use :meth:`set_many_and_count_sync` instead.
579+
"""
580+
warn_deprecation(
581+
version="1.10.0",
582+
deprecated_name="set_list_and_count_sync",
583+
kind="method",
584+
removal_in="2.0.0",
585+
alternative="set_many_and_count_sync",
586+
)
587+
self.set_many_and_count_sync(key, items, count)
588+
528589
def singleflight_sync(self, key: str, creator: Callable[[], T]) -> T:
529590
"""Coalesce concurrent sync cache misses per-process.
530591
@@ -713,21 +774,81 @@ async def get_model_version_async(self, model_name: str) -> str:
713774
"""
714775
return await async_(self.get_model_version_sync)(model_name)
715776

716-
async def get_list_async(self, key: str, model_class: type[T]) -> Optional[list[T]]:
777+
async def get_many_async(self, key: str, model_class: type[T]) -> Optional[list[T]]:
717778
"""Get a cached list of entities (async)."""
718-
return await async_(self.get_list_sync)(key, model_class)
779+
return await async_(self.get_many_sync)(key, model_class)
719780

720-
async def set_list_async(self, key: str, items: list[Any]) -> None:
781+
async def set_many_async(self, key: str, items: list[Any]) -> None:
721782
"""Cache a list of entities (async)."""
722-
await async_(self.set_list_sync)(key, items)
783+
await async_(self.set_many_sync)(key, items)
723784

724-
async def get_list_and_count_async(self, key: str, model_class: type[T]) -> Optional[tuple[list[T], int]]:
785+
async def get_many_and_count_async(self, key: str, model_class: type[T]) -> Optional[tuple[list[T], int]]:
725786
"""Get a cached list+count payload (async)."""
726-
return await async_(self.get_list_and_count_sync)(key, model_class)
787+
return await async_(self.get_many_and_count_sync)(key, model_class)
727788

728-
async def set_list_and_count_async(self, key: str, items: list[Any], count: int) -> None:
789+
async def set_many_and_count_async(self, key: str, items: list[Any], count: int) -> None:
729790
"""Cache a list+count payload (async)."""
730-
await async_(self.set_list_and_count_sync)(key, items, count)
791+
await async_(self.set_many_and_count_sync)(key, items, count)
792+
793+
async def get_list_async(self, key: str, model_class: type[T]) -> Optional[list[T]]:
794+
"""Get a cached list of entities (async).
795+
796+
.. deprecated:: 1.10.0
797+
Use :meth:`get_many_async` instead.
798+
"""
799+
warn_deprecation(
800+
version="1.10.0",
801+
deprecated_name="get_list_async",
802+
kind="method",
803+
removal_in="2.0.0",
804+
alternative="get_many_async",
805+
)
806+
return await self.get_many_async(key, model_class)
807+
808+
async def set_list_async(self, key: str, items: list[Any]) -> None:
809+
"""Cache a list of entities (async).
810+
811+
.. deprecated:: 1.10.0
812+
Use :meth:`set_many_async` instead.
813+
"""
814+
warn_deprecation(
815+
version="1.10.0",
816+
deprecated_name="set_list_async",
817+
kind="method",
818+
removal_in="2.0.0",
819+
alternative="set_many_async",
820+
)
821+
await self.set_many_async(key, items)
822+
823+
async def get_list_and_count_async(self, key: str, model_class: type[T]) -> Optional[tuple[list[T], int]]:
824+
"""Get a cached list+count payload (async).
825+
826+
.. deprecated:: 1.10.0
827+
Use :meth:`get_many_and_count_async` instead.
828+
"""
829+
warn_deprecation(
830+
version="1.10.0",
831+
deprecated_name="get_list_and_count_async",
832+
kind="method",
833+
removal_in="2.0.0",
834+
alternative="get_many_and_count_async",
835+
)
836+
return await self.get_many_and_count_async(key, model_class)
837+
838+
async def set_list_and_count_async(self, key: str, items: list[Any], count: int) -> None:
839+
"""Cache a list+count payload (async).
840+
841+
.. deprecated:: 1.10.0
842+
Use :meth:`set_many_and_count_async` instead.
843+
"""
844+
warn_deprecation(
845+
version="1.10.0",
846+
deprecated_name="set_list_and_count_async",
847+
kind="method",
848+
removal_in="2.0.0",
849+
alternative="set_many_and_count_async",
850+
)
851+
await self.set_many_and_count_async(key, items, count)
731852

732853
async def singleflight_async(self, key: str, creator: Callable[[], Coroutine[Any, Any, T]]) -> T:
733854
"""Coalesce concurrent async cache misses per-process.

advanced_alchemy/filters.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ class NullFilter(StatementFilter):
397397
398398
# Find records where email_verified_at is NULL
399399
null_filter = NullFilter("email_verified_at")
400-
unverified = await repo.list(null_filter)
400+
unverified = await repo.get_many(null_filter)
401401
402402
With multiple filters::
403403
@@ -411,7 +411,7 @@ class NullFilter(StatementFilter):
411411
NullFilter("email_verified_at"),
412412
CollectionFilter("role", ["admin", "moderator"]),
413413
]
414-
results = await repo.list(*filters)
414+
results = await repo.get_many(*filters)
415415
416416
See Also:
417417
- :class:`NotNullFilter`: Filter for NOT NULL values
@@ -450,7 +450,7 @@ class NotNullFilter(StatementFilter):
450450
451451
# Find records where email_verified_at is NOT NULL
452452
not_null_filter = NotNullFilter("email_verified_at")
453-
verified = await repo.list(not_null_filter)
453+
verified = await repo.get_many(not_null_filter)
454454
455455
With multiple filters::
456456
@@ -464,7 +464,7 @@ class NotNullFilter(StatementFilter):
464464
NotNullFilter("email_verified_at"),
465465
CollectionFilter("role", ["admin", "moderator"]),
466466
]
467-
results = await repo.list(*filters)
467+
results = await repo.get_many(*filters)
468468
469469
See Also:
470470
- :class:`NullFilter`: Filter for NULL values

0 commit comments

Comments
 (0)