Skip to content

Commit f9fdf7b

Browse files
authored
chore: switch to ruff (#2912)
1 parent f3bdcb0 commit f9fdf7b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+307
-206
lines changed

.pre-commit-config.yaml

+18-19
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
repos:
22
- repo: local
33
hooks:
4-
- id: autoflake
5-
name: autoflake
6-
entry: autoflake -i -r
7-
language: system
8-
types: [ python ]
4+
- id: ruff
5+
name: ruff
6+
description: "Run 'ruff' for extremely fast Python linting"
7+
entry: ruff check
8+
--force-exclude --fix
9+
--ignore E721
10+
--ignore E741
11+
language: python
12+
types_or: [python, pyi]
913
require_serial: true
14+
additional_dependencies: []
1015
files: ^(sqlglot/|tests/|setup.py)
11-
- id: isort
12-
args: [--combine-as]
13-
name: isort
14-
entry: isort
15-
language: system
16-
types: [ python ]
17-
files: ^(sqlglot/|tests/|setup.py)
16+
- id: ruff-format
17+
name: ruff-format
18+
description: "Run 'ruff format' for extremely fast Python formatting"
19+
entry: ruff format
20+
--force-exclude
21+
--line-length 100
22+
language: python
23+
types_or: [python, pyi]
1824
require_serial: true
19-
- id: black
20-
name: black
21-
entry: black --line-length 100
22-
language: system
23-
types: [ python ]
24-
require_serial: true
25-
files: ^(sqlglot/|tests/|setup.py)
2625
- id: mypy
2726
name: mypy
2827
entry: mypy sqlglot tests

benchmarks/bench.py

+15-14
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
# moz_sql_parser 3.10 compatibility
44
collections.Iterable = collections.abc.Iterable
5-
import gc
65
import timeit
76

87
import numpy as np
98

10-
#import sqlfluff
11-
#import moz_sql_parser
12-
#import sqloxide
13-
#import sqlparse
9+
# import sqlfluff
10+
# import moz_sql_parser
11+
# import sqloxide
12+
# import sqlparse
1413
import sqltree
1514

1615
import sqlglot
@@ -170,7 +169,7 @@ def sqlglotrs_parse(sql):
170169

171170

172171
def sqltree_parse(sql):
173-
sqltree.api.sqltree(sql.replace('"', '`').replace("''", '"'))
172+
sqltree.api.sqltree(sql.replace('"', "`").replace("''", '"'))
174173

175174

176175
def sqlparse_parse(sql):
@@ -206,11 +205,11 @@ def diff(row, column):
206205
libs = [
207206
"sqlglot",
208207
"sqlglotrs",
209-
#"sqlfluff",
208+
# "sqlfluff",
210209
"sqltree",
211-
#"sqlparse",
212-
#"moz_sql_parser",
213-
#"sqloxide",
210+
# "sqlparse",
211+
# "moz_sql_parser",
212+
# "sqloxide",
214213
]
215214
table = []
216215

@@ -231,10 +230,12 @@ def diff(row, column):
231230
lines.append(border(str("-" * width) for width in widths.values()))
232231

233232
for i, row in enumerate(table):
234-
lines.append(border(
235-
(str(row[column])[0:7] + diff(row, column)).rjust(width)[0 : width]
236-
for column, width in widths.items()
237-
))
233+
lines.append(
234+
border(
235+
(str(row[column])[0:7] + diff(row, column)).rjust(width)[0:width]
236+
for column, width in widths.items()
237+
)
238+
)
238239

239240
for line in lines:
240241
print(line)

pdoc/cli.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from pdoc.__main__ import cli, parser
88

99
# Need this import or else import_module doesn't work
10-
import sqlglot
10+
import sqlglot # noqa
1111

1212

1313
def mocked_import(*args, **kwargs):

setup.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,14 @@ def sqlglotrs_version():
2929
python_requires=">=3.7",
3030
extras_require={
3131
"dev": [
32-
"autoflake",
33-
"black",
3432
"duckdb>=0.6",
35-
"isort",
36-
"mypy>=0.990",
33+
"mypy",
3734
"pandas",
3835
"pyspark",
3936
"python-dateutil",
4037
"pdoc",
4138
"pre-commit",
39+
"ruff",
4240
"types-python-dateutil",
4341
"typing_extensions",
4442
"maturin>=1.4,<2.0",

sqlglot/__init__.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# ruff: noqa: F401
12
"""
23
.. include:: ../README.md
34
@@ -87,11 +88,13 @@ def parse(
8788

8889

8990
@t.overload
90-
def parse_one(sql: str, *, into: t.Type[E], **opts) -> E: ...
91+
def parse_one(sql: str, *, into: t.Type[E], **opts) -> E:
92+
...
9193

9294

9395
@t.overload
94-
def parse_one(sql: str, **opts) -> Expression: ...
96+
def parse_one(sql: str, **opts) -> Expression:
97+
...
9598

9699

97100
def parse_one(

sqlglot/dataframe/sql/dataframe.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,12 @@ def _create_cte_from_expression(
140140
return cte, name
141141

142142
@t.overload
143-
def _ensure_list_of_columns(self, cols: t.Collection[ColumnOrLiteral]) -> t.List[Column]: ...
143+
def _ensure_list_of_columns(self, cols: t.Collection[ColumnOrLiteral]) -> t.List[Column]:
144+
...
144145

145146
@t.overload
146-
def _ensure_list_of_columns(self, cols: ColumnOrLiteral) -> t.List[Column]: ...
147+
def _ensure_list_of_columns(self, cols: ColumnOrLiteral) -> t.List[Column]:
148+
...
147149

148150
def _ensure_list_of_columns(self, cols):
149151
return Column.ensure_cols(ensure_list(cols))

sqlglot/dialects/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
# ruff: noqa: F401
12
"""
23
## Dialects
34
45
While there is a SQL standard, most SQL engines support a variation of that standard. This makes it difficult
56
to write portable SQL code. SQLGlot bridges all the different variations, called "dialects", with an extensible
6-
SQL transpilation framework.
7+
SQL transpilation framework.
78
89
The base `sqlglot.dialects.dialect.Dialect` class implements a generic dialect that aims to be as universal as possible.
910

sqlglot/dialects/bigquery.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -474,10 +474,12 @@ def _parse_table_parts(self, schema: bool = False) -> exp.Table:
474474
return table
475475

476476
@t.overload
477-
def _parse_json_object(self, agg: Lit[False]) -> exp.JSONObject: ...
477+
def _parse_json_object(self, agg: Lit[False]) -> exp.JSONObject:
478+
...
478479

479480
@t.overload
480-
def _parse_json_object(self, agg: Lit[True]) -> exp.JSONObjectAgg: ...
481+
def _parse_json_object(self, agg: Lit[True]) -> exp.JSONObjectAgg:
482+
...
481483

482484
def _parse_json_object(self, agg=False):
483485
json_object = super()._parse_json_object()
@@ -555,7 +557,8 @@ class Generator(generator.Generator):
555557
exp.Create: _create_sql,
556558
exp.CTE: transforms.preprocess([_pushdown_cte_column_names]),
557559
exp.DateAdd: date_add_interval_sql("DATE", "ADD"),
558-
exp.DateDiff: lambda self, e: f"DATE_DIFF({self.sql(e, 'this')}, {self.sql(e, 'expression')}, {self.sql(e.args.get('unit', 'DAY'))})",
560+
exp.DateDiff: lambda self,
561+
e: f"DATE_DIFF({self.sql(e, 'this')}, {self.sql(e, 'expression')}, {self.sql(e.args.get('unit', 'DAY'))})",
559562
exp.DateFromParts: rename_func("DATE"),
560563
exp.DateStrToDate: datestrtodate_sql,
561564
exp.DateSub: date_add_interval_sql("DATE", "SUB"),
@@ -598,12 +601,13 @@ class Generator(generator.Generator):
598601
]
599602
),
600603
exp.SHA2: lambda self, e: self.func(
601-
f"SHA256" if e.text("length") == "256" else "SHA512", e.this
604+
"SHA256" if e.text("length") == "256" else "SHA512", e.this
602605
),
603606
exp.StabilityProperty: lambda self, e: (
604-
f"DETERMINISTIC" if e.name == "IMMUTABLE" else "NOT DETERMINISTIC"
607+
"DETERMINISTIC" if e.name == "IMMUTABLE" else "NOT DETERMINISTIC"
605608
),
606-
exp.StrToDate: lambda self, e: f"PARSE_DATE({self.format_time(e)}, {self.sql(e, 'this')})",
609+
exp.StrToDate: lambda self,
610+
e: f"PARSE_DATE({self.format_time(e)}, {self.sql(e, 'this')})",
607611
exp.StrToTime: lambda self, e: self.func(
608612
"PARSE_TIMESTAMP", self.format_time(e), e.this, e.args.get("zone")
609613
),
@@ -614,7 +618,7 @@ class Generator(generator.Generator):
614618
exp.TimestampDiff: rename_func("TIMESTAMP_DIFF"),
615619
exp.TimestampSub: date_add_interval_sql("TIMESTAMP", "SUB"),
616620
exp.TimeStrToTime: timestrtotime_sql,
617-
exp.Trim: lambda self, e: self.func(f"TRIM", e.this, e.expression),
621+
exp.Trim: lambda self, e: self.func("TRIM", e.this, e.expression),
618622
exp.TsOrDsAdd: _ts_or_ds_add_sql,
619623
exp.TsOrDsDiff: _ts_or_ds_diff_sql,
620624
exp.TsOrDsToTime: rename_func("TIME"),

sqlglot/dialects/clickhouse.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,8 @@ class Generator(generator.Generator):
579579
exp.Rand: rename_func("randCanonical"),
580580
exp.Select: transforms.preprocess([transforms.eliminate_qualify]),
581581
exp.StartsWith: rename_func("startsWith"),
582-
exp.StrPosition: lambda self, e: f"position({self.format_args(e.this, e.args.get('substr'), e.args.get('position'))})",
582+
exp.StrPosition: lambda self,
583+
e: f"position({self.format_args(e.this, e.args.get('substr'), e.args.get('position'))})",
583584
exp.VarMap: lambda self, e: _lower_func(var_map_sql(self, e)),
584585
exp.Xor: lambda self, e: self.func("xor", e.this, e.expression, *e.expressions),
585586
}

sqlglot/dialects/dialect.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ def normalize_identifier(self, expression: E) -> E:
377377
"""
378378
if (
379379
isinstance(expression, exp.Identifier)
380-
and not self.normalization_strategy is NormalizationStrategy.CASE_SENSITIVE
380+
and self.normalization_strategy is not NormalizationStrategy.CASE_SENSITIVE
381381
and (
382382
not expression.quoted
383383
or self.normalization_strategy is NormalizationStrategy.CASE_INSENSITIVE
@@ -1020,9 +1020,8 @@ def merge_without_target_sql(self: Generator, expression: exp.Merge) -> str:
10201020
"""Remove table refs from columns in when statements."""
10211021
alias = expression.this.args.get("alias")
10221022

1023-
normalize = lambda identifier: (
1024-
self.dialect.normalize_identifier(identifier).name if identifier else None
1025-
)
1023+
def normalize(identifier: t.Optional[exp.Identifier]) -> t.Optional[str]:
1024+
return self.dialect.normalize_identifier(identifier).name if identifier else None
10261025

10271026
targets = {normalize(expression.this.this)}
10281027

sqlglot/dialects/doris.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,14 @@ class Generator(MySQL.Generator):
5555
exp.Map: rename_func("ARRAY_MAP"),
5656
exp.RegexpLike: rename_func("REGEXP"),
5757
exp.RegexpSplit: rename_func("SPLIT_BY_STRING"),
58-
exp.StrToUnix: lambda self, e: f"UNIX_TIMESTAMP({self.sql(e, 'this')}, {self.format_time(e)})",
58+
exp.StrToUnix: lambda self,
59+
e: f"UNIX_TIMESTAMP({self.sql(e, 'this')}, {self.format_time(e)})",
5960
exp.Split: rename_func("SPLIT_BY_STRING"),
6061
exp.TimeStrToDate: rename_func("TO_DATE"),
61-
exp.ToChar: lambda self, e: f"DATE_FORMAT({self.sql(e, 'this')}, {self.format_time(e)})",
62-
exp.TsOrDsAdd: lambda self, e: f"DATE_ADD({self.sql(e, 'this')}, {self.sql(e, 'expression')})", # Only for day level
62+
exp.ToChar: lambda self,
63+
e: f"DATE_FORMAT({self.sql(e, 'this')}, {self.format_time(e)})",
64+
exp.TsOrDsAdd: lambda self,
65+
e: f"DATE_ADD({self.sql(e, 'this')}, {self.sql(e, 'expression')})", # Only for day level
6366
exp.TsOrDsToDate: lambda self, e: self.func("TO_DATE", e.this),
6467
exp.TimeToUnix: rename_func("UNIX_TIMESTAMP"),
6568
exp.TimestampTrunc: lambda self, e: self.func(

sqlglot/dialects/drill.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,14 @@ class Generator(generator.Generator):
128128
exp.DateAdd: _date_add_sql("ADD"),
129129
exp.DateStrToDate: datestrtodate_sql,
130130
exp.DateSub: _date_add_sql("SUB"),
131-
exp.DateToDi: lambda self, e: f"CAST(TO_DATE({self.sql(e, 'this')}, {Drill.DATEINT_FORMAT}) AS INT)",
132-
exp.DiToDate: lambda self, e: f"TO_DATE(CAST({self.sql(e, 'this')} AS VARCHAR), {Drill.DATEINT_FORMAT})",
133-
exp.If: lambda self, e: f"`IF`({self.format_args(e.this, e.args.get('true'), e.args.get('false'))})",
134-
exp.ILike: lambda self, e: f" {self.sql(e, 'this')} `ILIKE` {self.sql(e, 'expression')}",
131+
exp.DateToDi: lambda self,
132+
e: f"CAST(TO_DATE({self.sql(e, 'this')}, {Drill.DATEINT_FORMAT}) AS INT)",
133+
exp.DiToDate: lambda self,
134+
e: f"TO_DATE(CAST({self.sql(e, 'this')} AS VARCHAR), {Drill.DATEINT_FORMAT})",
135+
exp.If: lambda self,
136+
e: f"`IF`({self.format_args(e.this, e.args.get('true'), e.args.get('false'))})",
137+
exp.ILike: lambda self,
138+
e: f" {self.sql(e, 'this')} `ILIKE` {self.sql(e, 'expression')}",
135139
exp.Levenshtein: rename_func("LEVENSHTEIN_DISTANCE"),
136140
exp.PartitionedByProperty: lambda self, e: f"PARTITION BY {self.sql(e, 'this')}",
137141
exp.RegexpLike: rename_func("REGEXP_MATCHES"),
@@ -141,16 +145,19 @@ class Generator(generator.Generator):
141145
exp.Select: transforms.preprocess(
142146
[transforms.eliminate_distinct_on, transforms.eliminate_semi_and_anti_joins]
143147
),
144-
exp.StrToTime: lambda self, e: f"TO_TIMESTAMP({self.sql(e, 'this')}, {self.format_time(e)})",
148+
exp.StrToTime: lambda self,
149+
e: f"TO_TIMESTAMP({self.sql(e, 'this')}, {self.format_time(e)})",
145150
exp.TimeStrToDate: lambda self, e: f"CAST({self.sql(e, 'this')} AS DATE)",
146151
exp.TimeStrToTime: timestrtotime_sql,
147152
exp.TimeStrToUnix: rename_func("UNIX_TIMESTAMP"),
148153
exp.TimeToStr: lambda self, e: f"TO_CHAR({self.sql(e, 'this')}, {self.format_time(e)})",
149154
exp.TimeToUnix: rename_func("UNIX_TIMESTAMP"),
150155
exp.ToChar: lambda self, e: self.function_fallback_sql(e),
151156
exp.TryCast: no_trycast_sql,
152-
exp.TsOrDsAdd: lambda self, e: f"DATE_ADD(CAST({self.sql(e, 'this')} AS DATE), {self.sql(exp.Interval(this=e.expression, unit=exp.var('DAY')))})",
153-
exp.TsOrDiToDi: lambda self, e: f"CAST(SUBSTR(REPLACE(CAST({self.sql(e, 'this')} AS VARCHAR), '-', ''), 1, 8) AS INT)",
157+
exp.TsOrDsAdd: lambda self,
158+
e: f"DATE_ADD(CAST({self.sql(e, 'this')} AS DATE), {self.sql(exp.Interval(this=e.expression, unit=exp.var('DAY')))})",
159+
exp.TsOrDiToDi: lambda self,
160+
e: f"CAST(SUBSTR(REPLACE(CAST({self.sql(e, 'this')} AS VARCHAR), '-', ''), 1, 8) AS INT)",
154161
}
155162

156163
def normalize_func(self, name: str) -> str:

sqlglot/dialects/duckdb.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,11 @@ class Generator(generator.Generator):
365365
"DATE_DIFF", f"'{e.args.get('unit') or 'DAY'}'", e.expression, e.this
366366
),
367367
exp.DateStrToDate: datestrtodate_sql,
368-
exp.DateToDi: lambda self, e: f"CAST(STRFTIME({self.sql(e, 'this')}, {DuckDB.DATEINT_FORMAT}) AS INT)",
368+
exp.DateToDi: lambda self,
369+
e: f"CAST(STRFTIME({self.sql(e, 'this')}, {DuckDB.DATEINT_FORMAT}) AS INT)",
369370
exp.Decode: lambda self, e: encode_decode_sql(self, e, "DECODE", replace=False),
370-
exp.DiToDate: lambda self, e: f"CAST(STRPTIME(CAST({self.sql(e, 'this')} AS TEXT), {DuckDB.DATEINT_FORMAT}) AS DATE)",
371+
exp.DiToDate: lambda self,
372+
e: f"CAST(STRPTIME(CAST({self.sql(e, 'this')} AS TEXT), {DuckDB.DATEINT_FORMAT}) AS DATE)",
371373
exp.Encode: lambda self, e: encode_decode_sql(self, e, "ENCODE", replace=False),
372374
exp.Explode: rename_func("UNNEST"),
373375
exp.IntDiv: lambda self, e: self.binary(e, "//"),
@@ -408,7 +410,8 @@ class Generator(generator.Generator):
408410
exp.StrPosition: str_position_sql,
409411
exp.StrToDate: lambda self, e: f"CAST({str_to_time_sql(self, e)} AS DATE)",
410412
exp.StrToTime: str_to_time_sql,
411-
exp.StrToUnix: lambda self, e: f"EPOCH(STRPTIME({self.sql(e, 'this')}, {self.format_time(e)}))",
413+
exp.StrToUnix: lambda self,
414+
e: f"EPOCH(STRPTIME({self.sql(e, 'this')}, {self.format_time(e)}))",
412415
exp.Struct: _struct_sql,
413416
exp.Timestamp: no_timestamp_sql,
414417
exp.TimestampDiff: lambda self, e: self.func(
@@ -418,17 +421,20 @@ class Generator(generator.Generator):
418421
exp.TimeStrToDate: lambda self, e: f"CAST({self.sql(e, 'this')} AS DATE)",
419422
exp.TimeStrToTime: timestrtotime_sql,
420423
exp.TimeStrToUnix: lambda self, e: f"EPOCH(CAST({self.sql(e, 'this')} AS TIMESTAMP))",
421-
exp.TimeToStr: lambda self, e: f"STRFTIME({self.sql(e, 'this')}, {self.format_time(e)})",
424+
exp.TimeToStr: lambda self,
425+
e: f"STRFTIME({self.sql(e, 'this')}, {self.format_time(e)})",
422426
exp.TimeToUnix: rename_func("EPOCH"),
423-
exp.TsOrDiToDi: lambda self, e: f"CAST(SUBSTR(REPLACE(CAST({self.sql(e, 'this')} AS TEXT), '-', ''), 1, 8) AS INT)",
427+
exp.TsOrDiToDi: lambda self,
428+
e: f"CAST(SUBSTR(REPLACE(CAST({self.sql(e, 'this')} AS TEXT), '-', ''), 1, 8) AS INT)",
424429
exp.TsOrDsAdd: _ts_or_ds_add_sql,
425430
exp.TsOrDsDiff: lambda self, e: self.func(
426431
"DATE_DIFF",
427432
f"'{e.args.get('unit') or 'DAY'}'",
428433
exp.cast(e.expression, "TIMESTAMP"),
429434
exp.cast(e.this, "TIMESTAMP"),
430435
),
431-
exp.UnixToStr: lambda self, e: f"STRFTIME(TO_TIMESTAMP({self.sql(e, 'this')}), {self.format_time(e)})",
436+
exp.UnixToStr: lambda self,
437+
e: f"STRFTIME(TO_TIMESTAMP({self.sql(e, 'this')}), {self.format_time(e)})",
432438
exp.UnixToTime: _unix_to_time_sql,
433439
exp.UnixToTimeStr: lambda self, e: f"CAST(TO_TIMESTAMP({self.sql(e, 'this')}) AS TEXT)",
434440
exp.VariancePop: rename_func("VAR_POP"),

0 commit comments

Comments
 (0)