Skip to content

Commit

Permalink
Python: Format code using ruff, and satisfy linters
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed May 17, 2024
1 parent 1be2a9e commit 563e415
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 35 deletions.
5 changes: 2 additions & 3 deletions cratedb_sqlparse_py/cratedb_sqlparse/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from .parser import sqlparse, ParsingException

__all__ = ['sqlparse', 'ParsingException']
from .parser import ParsingException, sqlparse

__all__ = ["sqlparse", "ParsingException"]
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@

class AbstractSqlBaseLexer(Lexer):
"""
Automatically generated by Antlr4. It was added originally in CrateDB (https://github.com/crate/crate/commit/8e7cfd7c4a05fc27de8ce71af9165b98c986d883)
to implement $ strings, e.g: "SELECT $my friend's house$", we cannot do the same since it also generates invalid python syntax:
Automatically generated by Antlr4.
It was added originally in CrateDB (https://github.com/crate/crate/commit/8e7cfd7c4a05fc27de8ce71af9165b98c986d883)
to implement $ strings, e.g: "SELECT $my friend's house$", we cannot do the same since it also generates invalid
Python syntax:
# SqlBaseLexer.py
class SqlBaseLexer(AbstractSqlBaseLexer):
Expand Down
22 changes: 9 additions & 13 deletions cratedb_sqlparse_py/cratedb_sqlparse/parser.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from typing import List

from antlr4 import InputStream, CommonTokenStream, Token
from antlr4 import CommonTokenStream, InputStream, Token
from antlr4.error.ErrorListener import ErrorListener

from cratedb_sqlparse.generated_parser.SqlBaseParser import SqlBaseParser
from cratedb_sqlparse.generated_parser.SqlBaseLexer import SqlBaseLexer
from cratedb_sqlparse.generated_parser.SqlBaseParser import SqlBaseParser


def BEGIN_DOLLAR_QUOTED_STRING_action(self, localctx, actionIndex):
Expand All @@ -17,9 +17,10 @@ def END_DOLLAR_QUOTED_STRING_action(self, localctx, actionIndex):
self.tags.pop()


def END_DOLLAR_QUOTED_STRING_sempred(self, localctx, predIndex):
def END_DOLLAR_QUOTED_STRING_sempred(self, localctx, predIndex) -> bool:
if predIndex == 0:
return self.tags[0] == self.text
return False


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

def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e):
raise ParsingException(f'line{line}:{column} {msg}')
raise ParsingException(f"line{line}:{column} {msg}")


class Statement:
Expand Down Expand Up @@ -76,10 +77,7 @@ def query(self) -> str:
"""
Returns the query, comments and ';' are not included.
"""
return self.ctx.parser.getTokenStream().getText(
start=self.ctx.start.tokenIndex,
stop=self.ctx.stop.tokenIndex
)
return self.ctx.parser.getTokenStream().getText(start=self.ctx.start.tokenIndex, stop=self.ctx.stop.tokenIndex)

@property
def type(self):
Expand All @@ -96,8 +94,8 @@ def sqlparse(query: str) -> List[Statement]:
"""
Parses a string into SQL `Statement`.
"""
input = CaseInsensitiveStream(query)
lexer = SqlBaseLexer(input)
input_ = CaseInsensitiveStream(query)
lexer = SqlBaseLexer(input_)
lexer.removeErrorListeners()
stream = CommonTokenStream(lexer)

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

# At this point, all errors are already raised; it's seasonably safe to assume
# that the statements are valid.
statements = list(filter(
lambda children: isinstance(children, SqlBaseParser.StatementContext), tree.children
))
statements = list(filter(lambda children: isinstance(children, SqlBaseParser.StatementContext), tree.children))

return [Statement(statement) for statement in statements]
16 changes: 11 additions & 5 deletions cratedb_sqlparse_py/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ install_types = true
ignore_missing_imports = true
implicit_optional = true
non_interactive = true
# FIXME: Does not work?
exclude = [
"^SqlBaseLexer\\.py$",
"cratedb_sqlparse/generated_parser/",
"cratedb_sqlparse/generated_parser/SqlBaseParser.py",
]

[tool.pytest.ini_options]
addopts = """
Expand Down Expand Up @@ -197,17 +203,17 @@ check = [
]

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

lint = [
# { cmd = "ruff format --check ." },
# { cmd = "ruff check ." },
{ cmd = "ruff format --check ." },
{ cmd = "ruff check ." },
{ cmd = "validate-pyproject pyproject.toml" },
# { cmd = "mypy" },
# { cmd = "mypy ." },
]

release = [
Expand Down
14 changes: 8 additions & 6 deletions cratedb_sqlparse_py/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import subprocess
import sys
from importlib.util import find_spec
from pathlib import Path

import pytest


HERE = Path(__file__).parent
PROJECT_ROOT = HERE.parent.parent
SETUP_GRAMMAR = PROJECT_ROOT / "setup_grammar.py"
Expand All @@ -16,11 +16,13 @@ def generate():
Pytest fixture to generate runtime grammar from grammar description.
"""
try:
import cratedb_sqlparse.generated_parser.SqlBaseParser
# Test module for availability.
find_spec("cratedb_sqlparse.generated_parser.SqlBaseParser")
except ImportError:
subprocess.check_call([sys.executable, SETUP_GRAMMAR], cwd=HERE.parent.parent)
subprocess.check_call([sys.executable, SETUP_GRAMMAR], cwd=HERE.parent.parent) # noqa: S603

try:
import cratedb_sqlparse.generated_parser.SqlBaseParser
except ImportError:
raise RuntimeError("Python grammar has not been generated")
# Test module for availability.
find_spec("cratedb_sqlparse.generated_parser.SqlBaseParser")
except ImportError as ex:
raise RuntimeError("Python grammar has not been generated") from ex
17 changes: 11 additions & 6 deletions cratedb_sqlparse_py/tests/test_lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@

def test_sqlparser_one_statement(query=None):
from cratedb_sqlparse import sqlparse
query = query or 'SELECT 1;'

query = query or "SELECT 1;"
r = sqlparse(query)

assert len(r) == 1

stmt = r[0]

assert stmt.query == query.replace(';', '') # obj.query doesn't include comments or ';'
assert stmt.query == query.replace(";", "") # obj.query doesn't include comments or ';'
assert stmt.original_query == query
assert stmt.type == 'SELECT'
assert stmt.type == "SELECT"


def test_sqlparse_several_statements():
from cratedb_sqlparse import sqlparse

query = """
SELECT 1;
INSERT INTO doc.tbl VALUES (1,2,3,4,5,6);
Expand All @@ -29,20 +31,22 @@ def test_sqlparse_several_statements():

test_sqlparser_one_statement(r[0].query)

assert r[1].type == 'INSERT'
assert r[2].type == 'SELECT'
assert r[1].type == "INSERT"
assert r[2].type == "SELECT"


def test_sqlparse_dollar_string():
from cratedb_sqlparse import sqlparse

query = "update test set a=$$test;test$$"
r = sqlparse(query)

assert r[0].query == query


def test_sqlparse_raises_exception():
from cratedb_sqlparse import sqlparse, ParsingException
from cratedb_sqlparse import ParsingException, sqlparse

query = "SALUT MON AMIE"

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

def test_sqlparse_is_case_insensitive():
from cratedb_sqlparse import sqlparse

query = "inSerT InTo doc.Tbl1 Values (1)"

r = sqlparse(query)
Expand Down

0 comments on commit 563e415

Please sign in to comment.