Skip to content

Commit 3ae25e6

Browse files
committed
Python: Format code using ruff, and satisfy linters
1 parent 4115508 commit 3ae25e6

File tree

6 files changed

+46
-35
lines changed

6 files changed

+46
-35
lines changed
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from .parser import sqlparse, ParsingException
2-
3-
__all__ = ['sqlparse', 'ParsingException']
1+
from .parser import ParsingException, sqlparse
42

3+
__all__ = ["sqlparse", "ParsingException"]

cratedb_sqlparse_py/cratedb_sqlparse/generated_parser/AbstractSqlBaseLexer.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33

44
class AbstractSqlBaseLexer(Lexer):
55
"""
6-
Automatically generated by Antlr4. It was added originally in CrateDB (https://github.com/crate/crate/commit/8e7cfd7c4a05fc27de8ce71af9165b98c986d883)
7-
to implement $ strings, e.g: "SELECT $my friend's house$", we cannot do the same since it also generates invalid python syntax:
6+
Automatically generated by Antlr4.
7+
8+
It was added originally in CrateDB (https://github.com/crate/crate/commit/8e7cfd7c4a05fc27de8ce71af9165b98c986d883)
9+
to implement $ strings, e.g: "SELECT $my friend's house$", we cannot do the same since it also generates invalid
10+
Python syntax:
811
912
# SqlBaseLexer.py
1013
class SqlBaseLexer(AbstractSqlBaseLexer):

cratedb_sqlparse_py/cratedb_sqlparse/parser.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from typing import List
22

3-
from antlr4 import InputStream, CommonTokenStream, Token
3+
from antlr4 import CommonTokenStream, InputStream, Token
44
from antlr4.error.ErrorListener import ErrorListener
55

6-
from cratedb_sqlparse.generated_parser.SqlBaseParser import SqlBaseParser
76
from cratedb_sqlparse.generated_parser.SqlBaseLexer import SqlBaseLexer
7+
from cratedb_sqlparse.generated_parser.SqlBaseParser import SqlBaseParser
88

99

1010
def BEGIN_DOLLAR_QUOTED_STRING_action(self, localctx, actionIndex):
@@ -17,9 +17,10 @@ def END_DOLLAR_QUOTED_STRING_action(self, localctx, actionIndex):
1717
self.tags.pop()
1818

1919

20-
def END_DOLLAR_QUOTED_STRING_sempred(self, localctx, predIndex):
20+
def END_DOLLAR_QUOTED_STRING_sempred(self, localctx, predIndex) -> bool:
2121
if predIndex == 0:
2222
return self.tags[0] == self.text
23+
return False
2324

2425

2526
SqlBaseLexer.tags = []
@@ -46,7 +47,7 @@ class ExceptionErrorListener(ErrorListener):
4647
"""
4748

4849
def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e):
49-
raise ParsingException(f'line{line}:{column} {msg}')
50+
raise ParsingException(f"line{line}:{column} {msg}")
5051

5152

5253
class Statement:
@@ -76,10 +77,7 @@ def query(self) -> str:
7677
"""
7778
Returns the query, comments and ';' are not included.
7879
"""
79-
return self.ctx.parser.getTokenStream().getText(
80-
start=self.ctx.start.tokenIndex,
81-
stop=self.ctx.stop.tokenIndex
82-
)
80+
return self.ctx.parser.getTokenStream().getText(start=self.ctx.start.tokenIndex, stop=self.ctx.stop.tokenIndex)
8381

8482
@property
8583
def type(self):
@@ -96,8 +94,8 @@ def sqlparse(query: str) -> List[Statement]:
9694
"""
9795
Parses a string into SQL `Statement`.
9896
"""
99-
input = CaseInsensitiveStream(query)
100-
lexer = SqlBaseLexer(input)
97+
input_ = CaseInsensitiveStream(query)
98+
lexer = SqlBaseLexer(input_)
10199
lexer.removeErrorListeners()
102100
stream = CommonTokenStream(lexer)
103101

@@ -109,8 +107,6 @@ def sqlparse(query: str) -> List[Statement]:
109107

110108
# At this point, all errors are already raised; it's seasonably safe to assume
111109
# that the statements are valid.
112-
statements = list(filter(
113-
lambda children: isinstance(children, SqlBaseParser.StatementContext), tree.children
114-
))
110+
statements = list(filter(lambda children: isinstance(children, SqlBaseParser.StatementContext), tree.children))
115111

116112
return [Statement(statement) for statement in statements]

cratedb_sqlparse_py/pyproject.toml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ install_types = true
116116
ignore_missing_imports = true
117117
implicit_optional = true
118118
non_interactive = true
119+
# FIXME: Does not work?
120+
exclude = [
121+
"^SqlBaseLexer\\.py$",
122+
"cratedb_sqlparse/generated_parser/",
123+
"cratedb_sqlparse/generated_parser/SqlBaseParser.py",
124+
]
119125

120126
[tool.pytest.ini_options]
121127
addopts = """
@@ -197,17 +203,17 @@ check = [
197203
]
198204

199205
format = [
200-
# { cmd = "ruff format ." },
206+
{ cmd = "ruff format ." },
201207
# Configure Ruff not to auto-fix (remove!) unused variables (F841) and `print` statements (T201).
202-
# { cmd = "ruff check --fix --ignore=ERA --ignore=F401 --ignore=F841 --ignore=T20 ." },
208+
{ cmd = "ruff check --fix --ignore=ERA --ignore=F401 --ignore=F841 --ignore=T20 ." },
203209
{ cmd = "pyproject-fmt --keep-full-version pyproject.toml" },
204210
]
205211

206212
lint = [
207-
# { cmd = "ruff format --check ." },
208-
# { cmd = "ruff check ." },
213+
{ cmd = "ruff format --check ." },
214+
{ cmd = "ruff check ." },
209215
{ cmd = "validate-pyproject pyproject.toml" },
210-
# { cmd = "mypy" },
216+
# { cmd = "mypy ." },
211217
]
212218

213219
release = [
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import subprocess
22
import sys
3+
from importlib.util import find_spec
34
from pathlib import Path
45

56
import pytest
67

7-
88
HERE = Path(__file__).parent
99
PROJECT_ROOT = HERE.parent.parent
1010
SETUP_GRAMMAR = PROJECT_ROOT / "setup_grammar.py"
@@ -16,11 +16,13 @@ def generate():
1616
Pytest fixture to generate runtime grammar from grammar description.
1717
"""
1818
try:
19-
import cratedb_sqlparse.generated_parser.SqlBaseParser
19+
# Test module for availability.
20+
find_spec("cratedb_sqlparse.generated_parser.SqlBaseParser")
2021
except ImportError:
21-
subprocess.check_call([sys.executable, SETUP_GRAMMAR], cwd=HERE.parent.parent)
22+
subprocess.check_call([sys.executable, SETUP_GRAMMAR], cwd=HERE.parent.parent) # noqa: S603
2223

2324
try:
24-
import cratedb_sqlparse.generated_parser.SqlBaseParser
25-
except ImportError:
26-
raise RuntimeError("Python grammar has not been generated")
25+
# Test module for availability.
26+
find_spec("cratedb_sqlparse.generated_parser.SqlBaseParser")
27+
except ImportError as ex:
28+
raise RuntimeError("Python grammar has not been generated") from ex

cratedb_sqlparse_py/tests/test_lexer.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@
33

44
def test_sqlparser_one_statement(query=None):
55
from cratedb_sqlparse import sqlparse
6-
query = query or 'SELECT 1;'
6+
7+
query = query or "SELECT 1;"
78
r = sqlparse(query)
89

910
assert len(r) == 1
1011

1112
stmt = r[0]
1213

13-
assert stmt.query == query.replace(';', '') # obj.query doesn't include comments or ';'
14+
assert stmt.query == query.replace(";", "") # obj.query doesn't include comments or ';'
1415
assert stmt.original_query == query
15-
assert stmt.type == 'SELECT'
16+
assert stmt.type == "SELECT"
1617

1718

1819
def test_sqlparse_several_statements():
1920
from cratedb_sqlparse import sqlparse
21+
2022
query = """
2123
SELECT 1;
2224
INSERT INTO doc.tbl VALUES (1,2,3,4,5,6);
@@ -29,20 +31,22 @@ def test_sqlparse_several_statements():
2931

3032
test_sqlparser_one_statement(r[0].query)
3133

32-
assert r[1].type == 'INSERT'
33-
assert r[2].type == 'SELECT'
34+
assert r[1].type == "INSERT"
35+
assert r[2].type == "SELECT"
3436

3537

3638
def test_sqlparse_dollar_string():
3739
from cratedb_sqlparse import sqlparse
40+
3841
query = "update test set a=$$test;test$$"
3942
r = sqlparse(query)
4043

4144
assert r[0].query == query
4245

4346

4447
def test_sqlparse_raises_exception():
45-
from cratedb_sqlparse import sqlparse, ParsingException
48+
from cratedb_sqlparse import ParsingException, sqlparse
49+
4650
query = "SALUT MON AMIE"
4751

4852
with pytest.raises(ParsingException):
@@ -51,6 +55,7 @@ def test_sqlparse_raises_exception():
5155

5256
def test_sqlparse_is_case_insensitive():
5357
from cratedb_sqlparse import sqlparse
58+
5459
query = "inSerT InTo doc.Tbl1 Values (1)"
5560

5661
r = sqlparse(query)

0 commit comments

Comments
 (0)