From f65b259e56838d931f6673e5a97ce07439fba8bf Mon Sep 17 00:00:00 2001 From: baluyotraf Date: Mon, 25 Mar 2024 16:42:00 +0100 Subject: [PATCH 1/2] Use the value for the Query check instead of the typing --- src/altqq/translators/common.py | 14 +++++--------- src/altqq/translators/plain_text.py | 4 ++-- src/altqq/translators/psycopg.py | 4 ++-- src/altqq/translators/pyodbc.py | 4 ++-- 4 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/altqq/translators/common.py b/src/altqq/translators/common.py index 862be58..7b8ca71 100644 --- a/src/altqq/translators/common.py +++ b/src/altqq/translators/common.py @@ -1,6 +1,7 @@ """Translator related functions not belonging to other areas.""" import typing +from typing import Any from typing_extensions import Annotated @@ -8,21 +9,16 @@ from altqq.types import QueryValueTypes -def is_query_subclass(cls: type) -> bool: - """Checks if the type is a subclass of altqq.structs.Query. +def is_query_instance(value: Any) -> bool: + """Checks if the value is a subclass of altqq.structs.Query. Args: - cls (type): Type to check. + value (Any): Object to check. Returns: bool: True if a subclass, else False. """ - try: - return issubclass(cls, Query) - except TypeError: - pass - - return False + return isinstance(value, Query) def is_parameter(cls: type) -> bool: diff --git a/src/altqq/translators/plain_text.py b/src/altqq/translators/plain_text.py index 15853fd..1d5dfdf 100644 --- a/src/altqq/translators/plain_text.py +++ b/src/altqq/translators/plain_text.py @@ -4,7 +4,7 @@ from typing import Any from altqq.structs import Query -from altqq.translators.common import is_parameter, is_query_subclass +from altqq.translators.common import is_parameter, is_query_instance from altqq.types import T @@ -13,7 +13,7 @@ class PlainTextTranslator: def _resolve_value(self, query: Query, field: dc.Field[T]) -> Any: value = getattr(query, field.name) - if is_query_subclass(field.type): + if is_query_instance(value): return self.__call__(value) if is_parameter(field.type): diff --git a/src/altqq/translators/psycopg.py b/src/altqq/translators/psycopg.py index cd97ac1..5970568 100644 --- a/src/altqq/translators/psycopg.py +++ b/src/altqq/translators/psycopg.py @@ -5,7 +5,7 @@ from typing import Any, Iterable, List, Mapping, Sequence, Tuple, Union from altqq.structs import Query -from altqq.translators.common import is_parameter, is_query_subclass +from altqq.translators.common import is_parameter, is_query_instance from altqq.types import T @@ -70,7 +70,7 @@ class PsycopgTranslator: def _resolve_value(self, query: Query, field: dc.Field[T]) -> PsycopgStatement: value = getattr(query, field.name) - if is_query_subclass(field.type): + if is_query_instance(value): qq = self._convert_query(value) return PsycopgStatement(qq.query, qq.parameters) diff --git a/src/altqq/translators/pyodbc.py b/src/altqq/translators/pyodbc.py index 8410f2f..abd9d8a 100644 --- a/src/altqq/translators/pyodbc.py +++ b/src/altqq/translators/pyodbc.py @@ -5,7 +5,7 @@ from typing import Any, Iterable, List, Mapping, Sequence, Union from altqq.structs import Query -from altqq.translators.common import is_parameter, is_query_subclass +from altqq.translators.common import is_parameter, is_query_instance from altqq.types import T @@ -70,7 +70,7 @@ class PyODBCTranslator: def _resolve_value(self, query: Query, field: dc.Field[T]) -> PyODBCStatement: value = getattr(query, field.name) - if is_query_subclass(field.type): + if is_query_instance(value): qq = self.__call__(value) return PyODBCStatement(qq.query, qq.parameters) From 386e5e2d7e81d3a4b48a0b6f6240c81c5b0736bf Mon Sep 17 00:00:00 2001 From: baluyotraf Date: Mon, 25 Mar 2024 16:43:35 +0100 Subject: [PATCH 2/2] Update documentation for 0.0.5 --- CHANGELOG.md | 6 ++++++ pyproject.toml | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 62acfe0..9d5ce30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 0.0.5 + +- Nested `altqq.Query` are now checked by instance ([#13]) + +[#13]: https://github.com/baluyotraf/altqq/pull/13 + ## 0.0.4 - Subclass check improvements on `altqq.Query` ([#12]) diff --git a/pyproject.toml b/pyproject.toml index 3d40a68..92a210e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,7 +11,7 @@ convention = "google" [tool.poetry] name = "altqq" -version = "0.0.4" +version = "0.0.5" description = "Alternative Queries: Typed and Reusable Handcrafted SQL" authors = ["baluyotraf "] maintainers = ["baluyotraf "]