Skip to content

Commit 1925a9d

Browse files
committed
use dataclass, not dict
1 parent 4677b11 commit 1925a9d

1 file changed

Lines changed: 14 additions & 29 deletions

File tree

template.py

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

3-
from typing import TYPE_CHECKING, Literal, Protocol, TypedDict, TypeVar, runtime_checkable
3+
import dataclasses
4+
from typing import TYPE_CHECKING, Literal, Protocol, TypeVar, runtime_checkable
45

56
if TYPE_CHECKING:
67
from collections.abc import Iterable, Iterator, Sequence
@@ -18,40 +19,26 @@
1819
]
1920

2021

21-
class CompiledSql(TypedDict):
22+
@dataclasses.dataclass(frozen=True)
23+
class CompiledSql:
2224
"""Represents a compiled SQL statement, with the final SQL string and a list of Params to be passed to duckdb."""
2325

2426
sql: str
25-
params: list[Param]
27+
params: tuple[Param, ...]
2628

29+
# def __init__(self, sql: str, params: Iterable[Param]) -> None:
30+
# self.sql = sql
31+
# self.params = tuple(params)
2732

28-
class Param(TypedDict):
33+
34+
@dataclasses.dataclass(frozen=True)
35+
class Param:
2936
"""Represents a parameter to be passed to duckdb, with a name and a value."""
3037

3138
value: object
3239
name: str
3340

3441

35-
# class IntoParam(TypedDict):
36-
# """A Param with a name that is None, which can be used as input to the template engine, which will assign it a name based on its position and optionally an expression."""
37-
38-
# value: object
39-
# name: NotRequired[str | None]
40-
41-
42-
# def is_into_param(thing: object) -> TypeIs[IntoParam]:
43-
# try:
44-
# value = thing["value"] # ty:ignore[not-subscriptable]
45-
# except (TypeError, KeyError):
46-
# return False
47-
# try:
48-
# name = thing["name"] # ty:ignore[not-subscriptable]
49-
# except KeyError:
50-
# name = None
51-
52-
# return isinstance(value, object) and isinstance(name, (str, type(None)))
53-
54-
5542
def is_into_interpolation(thing: object) -> TypeIs[IntoInterpolation]:
5643
return isinstance(thing, IntoInterpolation)
5744

@@ -211,11 +198,9 @@ def compile_parts(parts: Iterable[str | IntoInterpolation], /) -> CompiledSql:
211198
if passed_name := part.expression:
212199
param_name += f"_{passed_name}"
213200
sql_parts.append(f"${param_name}")
214-
params.append({"name": param_name, "value": part.value})
215-
return {
216-
"sql": "".join(sql_parts),
217-
"params": params,
218-
}
201+
# params.append({"name": param_name, "value": part.value})
202+
params.append(Param(name=param_name, value=part.value))
203+
return CompiledSql(sql="".join(sql_parts), params=tuple(params))
219204

220205

221206
@runtime_checkable

0 commit comments

Comments
 (0)