Skip to content

Commit d1e1238

Browse files
rclaasenRoemer Claasen
andauthored
🐛 FIX: Skip non-columns (#2)
Skip non-columns, for instance query expressions without a data type. See https://docs.sqlalchemy.org/en/14/orm/loading_columns.html#sqlalchemy.orm.query_expression. These column expressions raise exceptions since they did not have the attributes one would expect from a normal column. They are (usually) dynamic, and not part of the table, thus should not be included in the document generation. Co-authored-by: Roemer Claasen <roemer@frosha.io>
1 parent 1af4506 commit d1e1238

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,4 @@ dmypy.json
129129
.pyre/
130130

131131
.vscode/
132+
.idea/

.pre-commit-config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ repos:
3030
additional_dependencies: [setuptools>=46.4.0]
3131

3232
- repo: https://github.com/pycqa/isort
33-
rev: 5.9.3
33+
rev: 5.12.0
3434
hooks:
3535
- id: isort
3636

@@ -41,7 +41,7 @@ repos:
4141
args: [--py37-plus]
4242

4343
- repo: https://github.com/psf/black
44-
rev: 21.7b0
44+
rev: 23.7.0
4545
hooks:
4646
- id: black
4747

@@ -57,7 +57,7 @@ repos:
5757
- flake8-markdown==0.2.0
5858

5959
- repo: https://github.com/pre-commit/mirrors-mypy
60-
rev: v0.910-1
60+
rev: v1.4.1
6161
hooks:
6262
- id: mypy
6363
additional_dependencies:

sphinx_sqlalchemy/main.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import importlib
2+
import logging
23
from typing import List, Optional, Set
34

45
from docutils import nodes
@@ -7,7 +8,7 @@
78
# from docutils.parsers.rst import directives
89
from sphinx.application import Sphinx
910
from sphinx.util.docutils import SphinxDirective
10-
from sqlalchemy import Constraint, inspect
11+
from sqlalchemy import Column, Constraint, inspect
1112
from sqlalchemy.orm.mapper import Mapper
1213
from sqlalchemy.sql.schema import (
1314
CheckConstraint,
@@ -17,6 +18,8 @@
1718
UniqueConstraint,
1819
)
1920

21+
logger = logging.getLogger(__name__)
22+
2023

2124
def setup_extension(app: Sphinx) -> None:
2225
"""Set up the sphinx extension."""
@@ -102,8 +105,18 @@ def add_content(self, mapper: Mapper, definition: nodes.definition) -> None:
102105

103106
# column documentation
104107
if mapper.columns:
108+
columns = []
109+
for column in mapper.columns:
110+
# Skip column expressions without a data type,
111+
# eg. query_expressions. See query_expression on
112+
# https://docs.sqlalchemy.org/en/14/orm/loading_columns.html
113+
if not isinstance(column, Column):
114+
logger.warning(f"Skipping column '{column.name}' {type(column)}")
115+
else:
116+
columns.append(column)
117+
105118
definition += nodes.rubric(text="Columns:")
106-
doc_column = any(column.doc for column in mapper.columns)
119+
doc_column = any(column.doc for column in columns)
107120
cols = 3 if doc_column else 2
108121
definition += nodes.table(
109122
"",
@@ -114,7 +127,7 @@ def add_content(self, mapper: Mapper, definition: nodes.definition) -> None:
114127
align="left",
115128
)
116129
body = definition[-1][-1][-1]
117-
for column in mapper.columns:
130+
for column in columns:
118131
row = nodes.row()
119132
body += row
120133
col_name = f"{column.name}"

0 commit comments

Comments
 (0)