From b51ebaf658d3835ac476fdb09cb22cbc8e07cfcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Mon, 29 Aug 2022 11:44:08 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Update=20`expresion.py`,?= =?UTF-8?q?=20sync=20from=20Jinja2=20template,=20implement=20`inherit=5Fca?= =?UTF-8?q?che`=20to=20solve=20errors=20like:=20`SAWarning:=20Class=20Sele?= =?UTF-8?q?ctOfScalar=20will=20not=20make=20use=20of=20SQL=20compilation?= =?UTF-8?q?=20caching`=20(#422)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/generate_select.py | 8 ++++++++ scripts/lint.sh | 2 ++ sqlmodel/sql/expression.py | 8 ++++---- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/scripts/generate_select.py b/scripts/generate_select.py index b66a167..f8aa300 100644 --- a/scripts/generate_select.py +++ b/scripts/generate_select.py @@ -1,3 +1,4 @@ +import os from itertools import product from pathlib import Path from typing import List, Tuple @@ -52,4 +53,11 @@ result = ( result = black.format_str(result, mode=black.Mode()) +current_content = destiny_path.read_text() + +if current_content != result and os.getenv("CHECK_JINJA"): + raise RuntimeError( + "sqlmodel/sql/expression.py content not update with Jinja2 template" + ) + destiny_path.write_text(result) diff --git a/scripts/lint.sh b/scripts/lint.sh index 4191d90..02568cd 100755 --- a/scripts/lint.sh +++ b/scripts/lint.sh @@ -7,3 +7,5 @@ mypy sqlmodel flake8 sqlmodel tests docs_src black sqlmodel tests docs_src --check isort sqlmodel tests docs_src scripts --check-only +# TODO: move this to test.sh after deprecating Python 3.6 +CHECK_JINJA=1 python scripts/generate_select.py diff --git a/sqlmodel/sql/expression.py b/sqlmodel/sql/expression.py index e7317bc..31c0bc1 100644 --- a/sqlmodel/sql/expression.py +++ b/sqlmodel/sql/expression.py @@ -29,14 +29,14 @@ _TSelect = TypeVar("_TSelect") if sys.version_info.minor >= 7: class Select(_Select, Generic[_TSelect]): - pass + inherit_cache = True # This is not comparable to sqlalchemy.sql.selectable.ScalarSelect, that has a different # purpose. This is the same as a normal SQLAlchemy Select class where there's only one # entity, so the result will be converted to a scalar by default. This way writing # for loops on the results will feel natural. class SelectOfScalar(_Select, Generic[_TSelect]): - pass + inherit_cache = True else: from typing import GenericMeta # type: ignore @@ -45,10 +45,10 @@ else: pass class _Py36Select(_Select, Generic[_TSelect], metaclass=GenericSelectMeta): - pass + inherit_cache = True class _Py36SelectOfScalar(_Select, Generic[_TSelect], metaclass=GenericSelectMeta): - pass + inherit_cache = True # Cast them for editors to work correctly, from several tricks tried, this works # for both VS Code and PyCharm