Skip to content

Commit b934d15

Browse files
committed
fixup
1 parent 08a1fba commit b934d15

1 file changed

Lines changed: 13 additions & 24 deletions

File tree

template.py

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
import dataclasses
44
from collections import Counter
5+
from collections.abc import Iterable
56
from typing import TYPE_CHECKING, Literal, NoReturn, Protocol, TypeVar, runtime_checkable
67

78
if TYPE_CHECKING:
8-
from collections.abc import Iterable, Iterator, Sequence
9+
from collections.abc import Iterator, Sequence
910

1011
from typing_extensions import TypeIs
1112

@@ -15,8 +16,7 @@
1516
"IntoTemplate",
1617
"Param",
1718
"SupportsDuckdbTemplate",
18-
"compile",
19-
"resolve",
19+
"param",
2020
"template",
2121
]
2222

@@ -29,10 +29,6 @@ class CompiledSql:
2929
params: dict[str, object]
3030

3131

32-
def is_into_interpolation(thing: object) -> TypeIs[IntoInterpolation]:
33-
return isinstance(thing, IntoInterpolation)
34-
35-
3632
@runtime_checkable
3733
class SupportsDuckdbTemplate(Protocol):
3834
"""Something that can be converted into a Template by implementing the __duckdb_template__ method."""
@@ -169,12 +165,12 @@ def template(
169165
return SqlTemplate(ParamInterpolation(thing))
170166
if isinstance(thing, IntoInterpolation):
171167
return SqlTemplate(thing)
172-
if is_iterable(thing):
168+
if _is_iterable(thing):
173169
return SqlTemplate(*thing)
174170
return SqlTemplate(ParamInterpolation(param(value=thing)))
175171

176172

177-
def is_iterable(thing: object) -> TypeIs[Iterable]:
173+
def _is_iterable(thing: object) -> TypeIs[Iterable]:
178174
return isinstance(thing, Iterable) and not isinstance(thing, (str, bytes))
179175

180176

@@ -188,25 +184,18 @@ def __init__(self, param: Param):
188184
self.format_spec = ""
189185

190186

191-
def resolve(parts: Iterable[str | IntoInterpolation]) -> ResolvedSqlTemplate:
187+
def _resolve(parts: Iterable[str | IntoInterpolation]) -> ResolvedSqlTemplate:
192188
"""Resolve a stream of strings and Interpolations, recursively resolving inner interpolations."""
193189
resolved: list[str | Param] = []
194190
for part in parts:
195191
if isinstance(part, str):
196192
resolved.append(part)
197193
else:
198-
resolved.extend(resolve_interpolation(part))
194+
resolved.extend(_resolve_interpolation(part))
199195
return ResolvedSqlTemplate(resolved)
200196

201197

202-
def compile(thing: object) -> CompiledSql:
203-
"""Compile a thing into a final SQL string with named parameter placeholders, and a list of Params."""
204-
t = template(thing)
205-
resolved = resolve(t)
206-
return compile_parts(resolved)
207-
208-
209-
def resolve_interpolation(interp: IntoInterpolation) -> Iterable[str | Param]:
198+
def _resolve_interpolation(interp: IntoInterpolation) -> Iterable[str | Param]:
210199
value = interp.value
211200
if isinstance(value, Param):
212201
# If it's already a Param, we can skip the template resolution and just return it as a param.
@@ -235,8 +224,7 @@ def resolve_interpolation(interp: IntoInterpolation) -> Iterable[str | Param]:
235224
if isinstance(value, str):
236225
# do NOT pass to template, since that would treat it as a raw SQL.
237226
return (param(value, name=interp.expression),)
238-
templ = template(value)
239-
# resolved = resolve(templ)
227+
templ = template(value) # ty:ignore[invalid-argument-type]
240228
# If the resolved inner is just a single Interpolation, then just return
241229
# the original value so that we preserve the expression name.
242230
# For example, if we have
@@ -258,7 +246,7 @@ def resolve_interpolation(interp: IntoInterpolation) -> Iterable[str | Param]:
258246
# names = t"SELECT name FROM ({foo})"
259247
# names should resolve to:
260248
# "SELECT name FROM (SELECT * FROM people WHERE age = $age)", with a param $age=37
261-
return resolve(templ)
249+
return _resolve(templ)
262250

263251

264252
@runtime_checkable
@@ -305,7 +293,8 @@ def __init__(
305293
# it's a Param, so we need to wrap it in a ParamInterpolation
306294
interps.append(ParamInterpolation(part))
307295
else:
308-
raise TypeError(f"Unexpected part type: {type(part)}. Expected str, IntoInterpolation, or Param.")
296+
msg = f"Unexpected part type: {type(part)}. Expected str, IntoInterpolation, or Param."
297+
raise TypeError(msg)
309298
self.interpolations = interps
310299

311300
def __iter__(self) -> Iterator[str | IntoInterpolation]:
@@ -317,7 +306,7 @@ def __iter__(self) -> Iterator[str | IntoInterpolation]:
317306

318307
def resolve(self) -> ResolvedSqlTemplate:
319308
"""Recursively resolve Interpolations into Params, returning a ResolvedSqlTemplate."""
320-
return resolve(self)
309+
return _resolve(self)
321310

322311
def compile(self) -> CompiledSql:
323312
"""Compile this template into a final SQL string with named parameter placeholders, and a list of Params."""

0 commit comments

Comments
 (0)