Skip to content

Commit b51ebaf

Browse files
authored
♻️ Update expresion.py, sync from Jinja2 template, implement inherit_cache to solve errors like: SAWarning: Class SelectOfScalar will not make use of SQL compilation caching (#422)
1 parent f232166 commit b51ebaf

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

scripts/generate_select.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
from itertools import product
23
from pathlib import Path
34
from typing import List, Tuple
@@ -52,4 +53,11 @@ class Arg(BaseModel):
5253

5354
result = black.format_str(result, mode=black.Mode())
5455

56+
current_content = destiny_path.read_text()
57+
58+
if current_content != result and os.getenv("CHECK_JINJA"):
59+
raise RuntimeError(
60+
"sqlmodel/sql/expression.py content not update with Jinja2 template"
61+
)
62+
5563
destiny_path.write_text(result)

scripts/lint.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ mypy sqlmodel
77
flake8 sqlmodel tests docs_src
88
black sqlmodel tests docs_src --check
99
isort sqlmodel tests docs_src scripts --check-only
10+
# TODO: move this to test.sh after deprecating Python 3.6
11+
CHECK_JINJA=1 python scripts/generate_select.py

sqlmodel/sql/expression.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@
2929
if sys.version_info.minor >= 7:
3030

3131
class Select(_Select, Generic[_TSelect]):
32-
pass
32+
inherit_cache = True
3333

3434
# This is not comparable to sqlalchemy.sql.selectable.ScalarSelect, that has a different
3535
# purpose. This is the same as a normal SQLAlchemy Select class where there's only one
3636
# entity, so the result will be converted to a scalar by default. This way writing
3737
# for loops on the results will feel natural.
3838
class SelectOfScalar(_Select, Generic[_TSelect]):
39-
pass
39+
inherit_cache = True
4040

4141
else:
4242
from typing import GenericMeta # type: ignore
@@ -45,10 +45,10 @@ class GenericSelectMeta(GenericMeta, _Select.__class__): # type: ignore
4545
pass
4646

4747
class _Py36Select(_Select, Generic[_TSelect], metaclass=GenericSelectMeta):
48-
pass
48+
inherit_cache = True
4949

5050
class _Py36SelectOfScalar(_Select, Generic[_TSelect], metaclass=GenericSelectMeta):
51-
pass
51+
inherit_cache = True
5252

5353
# Cast them for editors to work correctly, from several tricks tried, this works
5454
# for both VS Code and PyCharm

0 commit comments

Comments
 (0)