Skip to content

Commit 53d32c1

Browse files
authored
feat: updated advanced_alchemy.base reference docs (#327)
* feat: docs updates * chore: ruff updates
1 parent d59a52e commit 53d32c1

File tree

10 files changed

+300
-192
lines changed

10 files changed

+300
-192
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ repos:
2222
- id: unasyncd
2323
additional_dependencies: ["ruff"]
2424
- repo: https://github.com/charliermarsh/ruff-pre-commit
25-
rev: "v0.8.6"
25+
rev: "v0.9.0"
2626
hooks:
2727
# Run the linter.
2828
- id: ruff

advanced_alchemy/base.py

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -163,16 +163,15 @@ def __getattr__(attr_name: str) -> object:
163163
def merge_table_arguments(cls: type[DeclarativeBase], table_args: TableArgsType | None = None) -> TableArgsType:
164164
"""Merge Table Arguments.
165165
166-
When using mixins that include their own table args, it is difficult to append info into the model such as a comment.
167-
168-
This function helps you merge the args together.
166+
This function helps merge table arguments when using mixins that include their own table args,
167+
making it easier to append additional information such as comments or constraints to the model.
169168
170169
Args:
171-
cls: :class:`sqlalchemy.orm.DeclarativeBase` This is the model that will get the table args
172-
table_args: :class:`TableArgsType` additional information to add to table_args
170+
cls (type[:class:`sqlalchemy.orm.DeclarativeBase`]): The model that will get the table args.
171+
table_args (:class:`TableArgsType`, optional): Additional information to add to table_args.
173172
174173
Returns:
175-
:class:`TableArgsType`
174+
:class:`TableArgsType`: Merged table arguments.
176175
"""
177176
args: list[Any] = []
178177
kwargs: dict[str, Any] = {}
@@ -200,7 +199,13 @@ def merge_table_arguments(cls: type[DeclarativeBase], table_args: TableArgsType
200199

201200
@runtime_checkable
202201
class ModelProtocol(Protocol):
203-
"""The base SQLAlchemy model protocol."""
202+
"""The base SQLAlchemy model protocol.
203+
204+
Attributes:
205+
__table__ (:class:`sqlalchemy.sql.FromClause`): The table associated with the model.
206+
__mapper__ (:class:`sqlalchemy.orm.Mapper`): The mapper for the model.
207+
__name__ (str): The name of the model.
208+
"""
204209

205210
if TYPE_CHECKING:
206211
__table__: FromClause
@@ -217,7 +222,13 @@ def to_dict(self, exclude: set[str] | None = None) -> dict[str, Any]:
217222

218223

219224
class BasicAttributes:
220-
"""Basic attributes for SQLALchemy tables and queries."""
225+
"""Basic attributes for SQLAlchemy tables and queries.
226+
227+
Provides a method to convert the model to a dictionary representation.
228+
229+
Methods:
230+
to_dict: Converts the model to a dictionary, excluding specified fields. :no-index:
231+
"""
221232

222233
if TYPE_CHECKING:
223234
__name__: str
@@ -239,10 +250,12 @@ def to_dict(self, exclude: set[str] | None = None) -> dict[str, Any]:
239250

240251

241252
class CommonTableAttributes(BasicAttributes):
242-
"""Common attributes for SQLALchemy tables.
253+
"""Common attributes for SQLAlchemy tables.
243254
244-
.. seealso::
245-
:class:`BasicAttributes`
255+
Inherits from :class:`BasicAttributes` and provides a mechanism to infer table names from class names.
256+
257+
Attributes:
258+
__tablename__ (str): The inferred table name.
246259
"""
247260

248261
if TYPE_CHECKING:
@@ -262,10 +275,10 @@ def create_registry(
262275
"""Create a new SQLAlchemy registry.
263276
264277
Args:
265-
custom_annotation_map: :class:`dict` of custom type annotations to use for the registry
278+
custom_annotation_map (dict, optional): Custom type annotations to use for the registry.
266279
267280
Returns:
268-
:class:`sqlalchemy.orm.registry`
281+
:class:`sqlalchemy.orm.registry`: A new SQLAlchemy registry with the specified type annotations.
269282
"""
270283
import uuid as core_uuid
271284

@@ -305,7 +318,14 @@ def create_registry(
305318

306319

307320
class MetadataRegistry:
308-
"""A registry for metadata."""
321+
"""A registry for metadata.
322+
323+
Provides methods to get and set metadata for different bind keys.
324+
325+
Methods:
326+
get: Retrieves the metadata for a given bind key.
327+
set: Sets the metadata for a given bind key.
328+
"""
309329

310330
_instance: MetadataRegistry | None = None
311331
_registry: dict[str | None, MetaData] = {None: orm_registry.metadata}
@@ -342,8 +362,12 @@ def __contains__(self, bind_key: str | None) -> bool:
342362
class AdvancedDeclarativeBase(DeclarativeBase):
343363
"""A subclass of declarative base that allows for overriding of the registry.
344364
345-
.. seealso::
346-
:class:`sqlalchemy.orm.DeclarativeBase`
365+
Inherits from :class:`sqlalchemy.orm.DeclarativeBase`.
366+
367+
Attributes:
368+
registry (:class:`sqlalchemy.orm.registry`): The registry for the declarative base.
369+
__metadata_registry__ (:class:`~advanced_alchemy.base.MetadataRegistry`): The metadata registry.
370+
__bind_key__ (Optional[:class:`str`]): The bind key for the metadata.
347371
"""
348372

349373
registry = orm_registry
@@ -364,8 +388,8 @@ class UUIDBase(_UUIDPrimaryKey, CommonTableAttributes, AdvancedDeclarativeBase,
364388
"""Base for all SQLAlchemy declarative models with UUID v4 primary keys.
365389
366390
.. seealso::
367-
:class:`advanced_alchemy.mixins.UUIDPrimaryKey`
368391
:class:`CommonTableAttributes`
392+
:class:`advanced_alchemy.mixins.UUIDPrimaryKey`
369393
:class:`AdvancedDeclarativeBase`
370394
:class:`AsyncAttrs`
371395
"""

advanced_alchemy/config/common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def engine_config_dict(self) -> dict[str, Any]:
207207
"""Return the engine configuration as a dict.
208208
209209
Returns:
210-
A string keyed dict of config kwargs for the SQLAlchemy :func:`get_engine <sqlalchemy.get_engine>`
210+
A string keyed dict of config kwargs for the SQLAlchemy :func:`sqlalchemy.get_engine`
211211
function.
212212
"""
213213
return simple_asdict(self.engine_config, exclude_empty=True)
@@ -217,7 +217,7 @@ def session_config_dict(self) -> dict[str, Any]:
217217
"""Return the session configuration as a dict.
218218
219219
Returns:
220-
A string keyed dict of config kwargs for the SQLAlchemy :class:`sessionmaker <sqlalchemy.orm.sessionmaker>`
220+
A string keyed dict of config kwargs for the SQLAlchemy :class:`sqlalchemy.orm.sessionmaker`
221221
class.
222222
"""
223223
return simple_asdict(self.session_config, exclude_empty=True)

docs/conf.py

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@
3838
"sphinx.ext.autosectionlabel",
3939
"sphinx.ext.githubpages",
4040
"sphinx.ext.viewcode",
41-
# "sphinx_autodoc_typehints",
42-
"auto_pytabs.sphinx_ext",
4341
"tools.sphinx_ext.missing_references",
4442
"tools.sphinx_ext.changelog",
43+
"sphinx_autodoc_typehints",
44+
"myst_parser",
45+
"auto_pytabs.sphinx_ext",
4546
"sphinx_copybutton",
4647
"sphinx.ext.todo",
4748
"sphinx.ext.viewcode",
@@ -134,6 +135,12 @@
134135
"CommonTableAttributes": "advanced_alchemy.base.CommonTableAttributes",
135136
"AuditColumns": "advanced_alchemy.base.AuditColumns",
136137
"UUIDPrimaryKey": "advanced_alchemy.base.UUIDPrimaryKey",
138+
"EngineConfig": "advanced_alchemy.config.EngineConfig",
139+
"AsyncSessionConfig": "advanced_alchemy.config.AsyncSessionConfig",
140+
"SyncSessionConfig": "advanced_alchemy.config.SyncSessionConfig",
141+
"EmptyType": "advanced_alchemy.utils.dataclass.EmptyType",
142+
"async_sessionmaker": "sqlalchemy.ext.asyncio.async_sessionmaker",
143+
"sessionmaker": "sqlalchemy.orm.sessionmaker",
137144
}
138145
autodoc_mock_imports = [
139146
"alembic",
@@ -143,15 +150,12 @@
143150
"_sa.create_engine._sphinx_paramlinks_creator",
144151
"sqlalchemy.Dialect",
145152
"sqlalchemy.orm.MetaData",
146-
"sqlalchemy.orm.strategy_options._AbstractLoad",
147-
"sqlalchemy.sql.base.ExecutableOption",
148-
"sqlalchemy.Connection.in_transaction",
149-
"sqlalchemy.orm.attributes.InstrumentedAttribute",
150-
"sqlalchemy.orm.decl_base._TableArgsType",
151-
"sqlalchemy.orm.DeclarativeBase",
152-
"litestar.dto.data_structures.DTOData",
153-
"sqlalchemy.sql.schema._NamingSchemaParameter",
154-
"sqlalchemy.sql.FromClause",
153+
# Add these new entries:
154+
"advanced_alchemy.config.engine.EngineConfig",
155+
"advanced_alchemy.config.asyncio.AsyncSessionConfig",
156+
"advanced_alchemy.config.sync.SyncSessionConfig",
157+
"advanced_alchemy.utils.dataclass.EmptyType",
158+
"advanced_alchemy.extensions.litestar.plugins.init.config.engine.EngineConfig",
155159
]
156160

157161

@@ -170,7 +174,7 @@
170174
templates_path = ["_templates"]
171175
html_js_files = ["versioning.js"]
172176
html_css_files = ["custom.css"]
173-
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
177+
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store", "PYPI_README.md"]
174178
html_show_sourcelink = True
175179
html_copy_source = True
176180

@@ -186,15 +190,32 @@
186190
"logo_target": "/",
187191
"github_url": "https://github.com/litestar-org/advanced-alchemy",
188192
"navigation_with_keys": True,
189-
"globaltoc_expand_depth": 1,
193+
"globaltoc_expand_depth": 2,
190194
"light_logo": "_static/logo-default.png",
191195
"dark_logo": "_static/logo-default.png",
192196
"discussion_url": "https://discord.gg/dSDXd4mKhp",
193197
"nav_links": [
194198
{"title": "Home", "url": "index"},
195199
{
196-
"title": "Community",
200+
"title": "About",
197201
"children": [
202+
{
203+
"title": "Changelog",
204+
"url": "changelog",
205+
"summary": "All changes for Advanced Alchemy",
206+
},
207+
{
208+
"title": "Litestar Organization",
209+
"summary": "Details about the Litestar organization, the team behind Advanced Alchemy",
210+
"url": "https://litestar.dev/about/organization",
211+
"icon": "org",
212+
},
213+
{
214+
"title": "Releases",
215+
"summary": "Explore the release process, versioning, and deprecation policy for Advanced Alchemy",
216+
"url": "releases",
217+
"icon": "releases",
218+
},
198219
{
199220
"title": "Contributing",
200221
"summary": "Learn how to contribute to the Advanced Alchemy project",
@@ -213,33 +234,7 @@
213234
"url": "https://github.com/litestar-org/.github?tab=coc-ov-file#security-ov-file",
214235
"icon": "coc",
215236
},
216-
],
217-
},
218-
{
219-
"title": "About",
220-
"children": [
221-
{
222-
"title": "Litestar Organization",
223-
"summary": "Details about the Litestar organization, the team behind Advanced Alchemy",
224-
"url": "https://litestar.dev/about/organization",
225-
"icon": "org",
226-
},
227-
{
228-
"title": "Releases",
229-
"summary": "Explore the release process, versioning, and deprecation policy for Advanced Alchemy",
230-
"url": "releases",
231-
"icon": "releases",
232-
},
233-
],
234-
},
235-
{
236-
"title": "Release notes",
237-
"children": [
238-
{
239-
"title": "Changelog",
240-
"url": "changelog",
241-
"summary": "All changes for Advanced Alchemy",
242-
},
237+
{"title": "Sponsor", "url": "https://github.com/sponsors/Litestar-Org", "icon": "heart"},
243238
],
244239
},
245240
{
@@ -265,7 +260,6 @@
265260
},
266261
],
267262
},
268-
{"title": "Sponsor", "url": "https://github.com/sponsors/Litestar-Org", "icon": "heart"},
269263
],
270264
}
271265

docs/getting-started.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@ offering useful and easy-to-use features for your database projects.
1111
* `Alembic <https://alembic.sqlalchemy.org/en/latest/>`_
1212
* `Typing Extensions <https://typing-extensions.readthedocs.io/en/latest/>`_
1313

14-
It works standalone or integrated with any WSGI or ASGI framework, and comes bundled with extensions for:
14+
It's designed to work on it's own, but it equally as well when integrated with your favorite web framework.
15+
16+
We've built extensions for some of the most popular frameworks, so you can get the most out of Advanced Alchemy with minimal effort.
1517

1618
* `Litestar <https://docs.litestar.dev/>`_
1719
* `FastAPI <https://fastapi.tiangolo.com/>`_
1820
* `Starlette <https://www.starlette.io/>`_
1921
* `Flask <https://flask.palletsprojects.com/>`_
2022
* `Sanic <https://sanicframework.org/>`_
2123

24+
If your framework is not listed, don't worry! Advanced Alchemy is designed to be modular and easily integrated with any Python web framework. Join our discord and we'll help you get started.
25+
2226
Installation
2327
------------
2428

docs/reference/base.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ base
77
:imported-members:
88
:undoc-members:
99
:show-inheritance:
10+
:no-index: advanced_alchemy.base.AdvancedDeclarativeBase.registry advanced_alchemy.base.BasicAttributes.to_dict

docs/reference/mixins/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
mixins
33
======
44

5-
API Reference for the ``mixins`` module
5+
API Reference for the ``advanced_alchemy.mixins`` module
66

77
.. note:: Private methods and attributes are not included in the API reference.
88

docs/usage/frameworks/litestar.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ First, configure the SQLAlchemy plugin with Litestar. The plugin handles databas
2121
.. code-block:: python
2222
2323
from litestar import Litestar
24-
from advanced_alchemy.extensions.litestar import (
24+
from litestar.plugins.sqlalchemy import (
2525
AsyncSessionConfig,
2626
SQLAlchemyAsyncConfig,
2727
SQLAlchemyPlugin,

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ doc = [
100100
"sphinx-paramlinks>=0.6.0",
101101
"sphinx-togglebutton>=0.3.2",
102102
"sphinx-toolbox>=3.8.1",
103+
"myst-parser",
104+
"sphinx-autodoc-typehints",
103105
]
104106
duckdb = ["duckdb>=1.1.2", "duckdb-engine>=0.13.4", "pytz>=2024.2"]
105107
fastapi = ["fastapi[all]>=0.115.3"]
@@ -273,6 +275,7 @@ dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
273275
fixable = ["ALL"]
274276
ignore = [
275277
"A003", # flake8-builtins - class attribute {name} is shadowing a python builtin
278+
"A005", # flake8-builtins - module {name} shadows a Python standard-library module
276279
"B010", # flake8-bugbear - do not call setattr with a constant attribute value
277280
"D100", # pydocstyle - missing docstring in public module
278281
"D101", # pydocstyle - missing docstring in public class

0 commit comments

Comments
 (0)