Skip to content

Commit 6346237

Browse files
Typing: improve expressions.builders module typing coverage (tobymao#7429)
* feat: typing improvements to `expressions.builder` functions * fix: use `set_kwargs` in `to_table` return line
1 parent c2b69d4 commit 6346237

1 file changed

Lines changed: 37 additions & 24 deletions

File tree

sqlglot/expressions/builders.py

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,15 @@
5151

5252

5353
if t.TYPE_CHECKING:
54-
from collections.abc import Sequence, Iterable
54+
from collections.abc import Sequence, Iterable, Iterator
5555
from sqlglot.dialects.dialect import DialectType
5656
from sqlglot.expressions.core import ExpOrStr, Func
5757
from sqlglot.expressions.datatypes import DATA_TYPE
5858
from sqlglot._typing import ParserArgs, ParserNoDialectArgs, E
59-
from typing_extensions import Unpack
59+
from typing_extensions import Unpack, ParamSpec, Concatenate
60+
from sqlglot.expressions.core import Dot
61+
62+
P = ParamSpec("P")
6063

6164

6265
def select(
@@ -115,7 +118,7 @@ def from_(
115118

116119
def update(
117120
table: str | Table,
118-
properties: t.Optional[dict] = None,
121+
properties: t.Optional[dict[str, object]] = None,
119122
where: t.Optional[ExpOrStr] = None,
120123
from_: t.Optional[ExpOrStr] = None,
121124
with_: t.Optional[dict[str, ExpOrStr]] = None,
@@ -348,7 +351,7 @@ def to_interval(interval: str | Expr) -> Interval:
348351

349352

350353
def to_table(
351-
sql_path: str | Table, dialect: DialectType = None, copy: bool = True, **kwargs
354+
sql_path: str | Table, dialect: DialectType = None, copy: bool = True, **kwargs: object
352355
) -> Table:
353356
"""
354357
Create a table expression from a `[catalog].[schema].[table]` sql path. Catalog and schema are optional.
@@ -376,19 +379,16 @@ def to_table(
376379

377380
table = table_(this, db=db, catalog=catalog)
378381

379-
for k, v in kwargs.items():
380-
table.set(k, v)
381-
382-
return table
382+
return table.set_kwargs(kwargs)
383383

384384

385385
def to_column(
386386
sql_path: str | Column,
387387
quoted: t.Optional[bool] = None,
388388
dialect: DialectType = None,
389389
copy: bool = True,
390-
**kwargs,
391-
) -> Column:
390+
**kwargs: t.Any,
391+
) -> t.Union[Column, Dot]:
392392
"""
393393
Create a column from a `[table].[column]` sql path. Table is optional.
394394
If a column is passed in then that column is returned.
@@ -648,7 +648,12 @@ def rename_column(
648648
)
649649

650650

651-
def replace_children(expression: Expr, fun: t.Callable, *args, **kwargs) -> None:
651+
def replace_children(
652+
expression: Expr,
653+
fun: t.Callable[Concatenate[Expr, P], object],
654+
*args: P.args,
655+
**kwargs: P.kwargs,
656+
) -> None:
652657
"""
653658
Replace children of an expression with the result of a lambda fun(child) -> exp.
654659
"""
@@ -673,7 +678,7 @@ def replace_children(expression: Expr, fun: t.Callable, *args, **kwargs) -> None
673678

674679
def replace_tree(
675680
expression: Expr,
676-
fun: t.Callable,
681+
fun: t.Callable[[Expr], Expr],
677682
prune: t.Optional[t.Callable[[Expr], bool]] = None,
678683
) -> Expr:
679684
"""
@@ -697,7 +702,7 @@ def replace_tree(
697702
return new_node
698703

699704

700-
def find_tables(expression: Expr) -> t.Set[Table]:
705+
def find_tables(expression: Expr) -> set[Table]:
701706
"""
702707
Find all tables referenced in a query.
703708
@@ -717,7 +722,7 @@ def find_tables(expression: Expr) -> t.Set[Table]:
717722
}
718723

719724

720-
def column_table_names(expression: Expr, exclude: str = "") -> t.Set[str]:
725+
def column_table_names(expression: Expr, exclude: str = "") -> set[str]:
721726
"""
722727
Return all table names referenced through columns in an expression.
723728
@@ -797,7 +802,7 @@ def normalize_table_name(table: str | Table, dialect: DialectType = None, copy:
797802

798803

799804
def replace_tables(
800-
expression: E, mapping: t.Dict[str, str], dialect: DialectType = None, copy: bool = True
805+
expression: E, mapping: dict[str, str], dialect: DialectType = None, copy: bool = True
801806
) -> E:
802807
"""Replace all tables in expression according to the mapping.
803808
@@ -836,7 +841,7 @@ def _replace_tables(node: Expr) -> Expr:
836841
return expression.transform(_replace_tables, copy=copy) # type: ignore
837842

838843

839-
def replace_placeholders(expression: Expr, *args, **kwargs) -> Expr:
844+
def replace_placeholders(expression: Expr, *args: object, **kwargs: t.Any) -> Expr:
840845
"""Replace placeholders in an expression.
841846
842847
Args:
@@ -856,7 +861,7 @@ def replace_placeholders(expression: Expr, *args, **kwargs) -> Expr:
856861
The mapped expression.
857862
"""
858863

859-
def _replace_placeholders(node: Expr, args, **kwargs) -> Expr:
864+
def _replace_placeholders(node: Expr, args: Iterator[object], **kwargs: object) -> Expr:
860865
if isinstance(node, Placeholder):
861866
if node.this:
862867
new_name = kwargs.get(node.this)
@@ -874,7 +879,7 @@ def _replace_placeholders(node: Expr, args, **kwargs) -> Expr:
874879

875880
def expand(
876881
expression: Expr,
877-
sources: t.Dict[str, Query | t.Callable[[], Query]],
882+
sources: dict[str, Query | t.Callable[[], Query]],
878883
dialect: DialectType = None,
879884
copy: bool = True,
880885
) -> Expr:
@@ -918,7 +923,9 @@ def _expand(node: Expr):
918923
return expression.transform(_expand, copy=copy)
919924

920925

921-
def func(name: str, *args, copy: bool = True, dialect: DialectType = None, **kwargs) -> Func:
926+
def func(
927+
name: str, *args: t.Any, copy: bool = True, dialect: DialectType = None, **kwargs: t.Any
928+
) -> Func:
922929
"""
923930
Returns a Func expression.
924931
@@ -950,7 +957,7 @@ def func(name: str, *args, copy: bool = True, dialect: DialectType = None, **kwa
950957

951958
dialect = Dialect.get_or_raise(dialect)
952959

953-
converted: t.List[Expr] = [maybe_parse(arg, dialect=dialect, copy=copy) for arg in args]
960+
converted: list[Expr] = [maybe_parse(arg, dialect=dialect, copy=copy) for arg in args]
954961
kwargs = {key: maybe_parse(value, dialect=dialect, copy=copy) for key, value in kwargs.items()}
955962

956963
constructor = dialect.parser_class.FUNCTIONS.get(name.upper())
@@ -1007,7 +1014,10 @@ def case(
10071014

10081015

10091016
def array(
1010-
*expressions: ExpOrStr, copy: bool = True, dialect: DialectType = None, **kwargs
1017+
*expressions: ExpOrStr,
1018+
copy: bool = True,
1019+
dialect: DialectType = None,
1020+
**kwargs: Unpack[ParserNoDialectArgs],
10111021
) -> Array:
10121022
"""
10131023
Returns an array.
@@ -1034,7 +1044,10 @@ def array(
10341044

10351045

10361046
def tuple_(
1037-
*expressions: ExpOrStr, copy: bool = True, dialect: DialectType = None, **kwargs
1047+
*expressions: ExpOrStr,
1048+
copy: bool = True,
1049+
dialect: DialectType = None,
1050+
**kwargs: Unpack[ParserNoDialectArgs],
10381051
) -> Tuple:
10391052
"""
10401053
Returns an tuple.
@@ -1083,10 +1096,10 @@ def null() -> Null:
10831096

10841097
def apply_index_offset(
10851098
this: Expr,
1086-
expressions: t.List[E],
1099+
expressions: list[E],
10871100
offset: int,
10881101
dialect: DialectType = None,
1089-
) -> t.List[E]:
1102+
) -> list[E]:
10901103
if not offset or len(expressions) != 1:
10911104
return expressions
10921105

0 commit comments

Comments
 (0)