Skip to content

Commit e7bd550

Browse files
authored
chore: migrate lint tool from isort+black to ruff (#35)
* chore: migrate lint tool from isort+black to ruff * chore: remove unused noqa and use tuple instead of list to improve speed * chore: remove ruff extend rule SIM * refactor: add `__all__` to fix F401 issue
1 parent 268a169 commit e7bd550

File tree

17 files changed

+221
-410
lines changed

17 files changed

+221
-410
lines changed

Makefile

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
src_dir = pypika_tortoise
22
checkfiles = $(src_dir) tests/ conftest.py
3-
black_opts = -l 100 -t py39
43
py_warn = PYTHONDEVMODE=1
54
pytest_opts = -n auto --cov=$(src_dir) --cov-append --cov-branch --tb=native -q
65

76
up:
87
@poetry update
98

109
deps:
11-
@poetry install
10+
poetry install --all-groups
11+
12+
typehints:
13+
mypy $(checkfiles)
14+
bandit -c pyproject.toml -r $(checkfiles)
15+
twine check dist/*
1216

1317
check: build _check
1418
_check:
15-
ifneq ($(shell which black),)
16-
black --check $(black_opts) $(checkfiles) || (echo "Please run 'make style' to auto-fix style issues" && false)
17-
endif
19+
ruff format --check $(checkfiles) || (echo "Please run 'make style' to auto-fix style issues" && false)
1820
ruff check $(checkfiles)
19-
bandit -c pyproject.toml -r $(checkfiles)
20-
mypy $(checkfiles)
21-
twine check dist/*
21+
$(MAKE) typehints
2222

2323
test: deps _test
2424
_test:
@@ -28,16 +28,11 @@ ci: build _check _test
2828

2929
style: deps _style
3030
_style:
31-
isort -src $(checkfiles)
32-
black $(black_opts) $(checkfiles)
33-
34-
lint: build
35-
isort -src $(checkfiles)
36-
black $(black_opts) $(checkfiles)
31+
ruff format $(checkfiles)
3732
ruff check --fix $(checkfiles)
38-
mypy $(checkfiles)
39-
bandit -c pyproject.toml -r $(checkfiles)
40-
twine check dist/*
33+
34+
lint: build _style
35+
$(MAKE) typehints
4136

4237
build: deps
4338
poetry build --clean

poetry.lock

Lines changed: 90 additions & 184 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pypika_tortoise/dialects/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,19 @@
33
from .oracle import OracleQuery, OracleQueryBuilder
44
from .postgresql import PostgreSQLQuery, PostgreSQLQueryBuilder
55
from .sqlite import SQLLiteQuery, SQLLiteQueryBuilder, SQLLiteValueWrapper
6+
7+
__all__ = (
8+
"MSSQLQuery",
9+
"MSSQLQueryBuilder",
10+
"MySQLLoadQueryBuilder",
11+
"MySQLQuery",
12+
"MySQLQueryBuilder",
13+
"MySQLValueWrapper",
14+
"OracleQuery",
15+
"OracleQueryBuilder",
16+
"PostgreSQLQuery",
17+
"PostgreSQLQueryBuilder",
18+
"SQLLiteQuery",
19+
"SQLLiteQueryBuilder",
20+
"SQLLiteValueWrapper",
21+
)

pypika_tortoise/dialects/postgresql.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ def get_sql(self, ctx: SqlContext | None = None) -> str:
149149
return ""
150150

151151
has_joins = bool(self._joins)
152-
has_multiple_from_clauses = 1 < len(self._from)
153-
has_subquery_from_clause = 0 < len(self._from) and isinstance(self._from[0], QueryBuilder)
152+
has_multiple_from_clauses = len(self._from) > 1
153+
has_subquery_from_clause = len(self._from) > 0 and isinstance(self._from[0], QueryBuilder)
154154
has_reference_to_foreign_table = self._foreign_table
155155
has_update_from = self._update_table and self._from
156156

@@ -167,10 +167,7 @@ def get_sql(self, ctx: SqlContext | None = None) -> str:
167167
),
168168
)
169169
if self._update_table:
170-
if self._with:
171-
querystring = self._with_sql(ctx)
172-
else:
173-
querystring = ""
170+
querystring = self._with_sql(ctx) if self._with else ""
174171

175172
querystring += self._update_sql(ctx)
176173

pypika_tortoise/dialects/sqlite.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ def get_sql(self, ctx: SqlContext | None = None) -> str:
4343
return ""
4444

4545
has_joins = bool(self._joins)
46-
has_multiple_from_clauses = 1 < len(self._from)
47-
has_subquery_from_clause = 0 < len(self._from) and isinstance(self._from[0], QueryBuilder)
46+
has_multiple_from_clauses = len(self._from) > 1
47+
has_subquery_from_clause = len(self._from) > 0 and isinstance(self._from[0], QueryBuilder)
4848
has_reference_to_foreign_table = self._foreign_table
4949
has_update_from = self._update_table and self._from
5050

@@ -60,13 +60,9 @@ def get_sql(self, ctx: SqlContext | None = None) -> str:
6060
),
6161
)
6262
if self._update_table:
63-
if self._with:
64-
querystring = self._with_sql(ctx)
65-
else:
66-
querystring = ""
63+
querystring = self._with_sql(ctx) if self._with else ""
6764

6865
querystring += self._update_sql(ctx)
69-
7066
querystring += self._set_sql(ctx)
7167

7268
if self._joins:

pypika_tortoise/functions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def distinct(self) -> Self: # type:ignore[return]
4141

4242
class Count(DistinctOptionFunction):
4343
def __init__(self, param: Any, alias: str | None = None) -> None:
44-
is_star = isinstance(param, str) and "*" == param
44+
is_star = isinstance(param, str) and param == "*"
4545
super().__init__("COUNT", Star() if is_star else param, alias=alias)
4646

4747

pypika_tortoise/queries.py

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -876,9 +876,7 @@ def from_(self, selectable: Selectable | Query | str) -> Self: # type:ignore[re
876876
"""
877877

878878
self._from.append(
879-
Table(selectable)
880-
if isinstance(selectable, str)
881-
else selectable # type:ignore[arg-type]
879+
Table(selectable) if isinstance(selectable, str) else selectable # type:ignore[arg-type]
882880
)
883881

884882
if isinstance(selectable, (QueryBuilder, _SetOperation)) and selectable.alias is None:
@@ -1131,7 +1129,7 @@ def with_totals(self) -> Self: # type:ignore[return]
11311129
def rollup( # type:ignore[return]
11321130
self, *terms: list | tuple | set | Term, **kwargs: Any
11331131
) -> Self:
1134-
for_mysql = "mysql" == kwargs.get("vendor")
1132+
for_mysql = kwargs.get("vendor") == "mysql"
11351133

11361134
if self._mysql_rollup:
11371135
raise AttributeError("'Query' object has no attribute '%s'" % "rollup")
@@ -1151,7 +1149,7 @@ def rollup( # type:ignore[return]
11511149
self._mysql_rollup = True
11521150
self._groupbys += terms # type:ignore[arg-type]
11531151

1154-
elif 0 < len(self._groupbys) and isinstance(self._groupbys[-1], Rollup):
1152+
elif len(self._groupbys) > 0 and isinstance(self._groupbys[-1], Rollup):
11551153
# If a rollup was added last, then append the new terms to the previous rollup
11561154
self._groupbys[-1].args += terms
11571155

@@ -1278,7 +1276,7 @@ def _list_aliases(field_set: Sequence[Field], ctx: SqlContext) -> list[str]:
12781276
return [field.alias or field.get_sql(ctx) for field in field_set]
12791277

12801278
def _select_field_str(self, term: str) -> None:
1281-
if 0 == len(self._from):
1279+
if len(self._from) == 0:
12821280
raise QueryException(f"Cannot select {term}, no FROM table specified.") # nosec:B608
12831281

12841282
if term == "*":
@@ -1399,8 +1397,8 @@ def get_sql(self, ctx: SqlContext | None = None) -> str:
13991397
return ""
14001398

14011399
has_joins = bool(self._joins)
1402-
has_multiple_from_clauses = 1 < len(self._from)
1403-
has_subquery_from_clause = 0 < len(self._from) and isinstance(self._from[0], QueryBuilder)
1400+
has_multiple_from_clauses = len(self._from) > 1
1401+
has_subquery_from_clause = len(self._from) > 0 and isinstance(self._from[0], QueryBuilder)
14041402
has_reference_to_foreign_table = self._foreign_table
14051403
has_update_from = self._update_table and self._from
14061404

@@ -1417,11 +1415,7 @@ def get_sql(self, ctx: SqlContext | None = None) -> str:
14171415
)
14181416

14191417
if self._update_table:
1420-
if self._with:
1421-
querystring = self._with_sql(ctx)
1422-
else:
1423-
querystring = ""
1424-
1418+
querystring = self._with_sql(ctx) if self._with else ""
14251419
querystring += self._update_sql(ctx)
14261420

14271421
if self._joins:
@@ -1441,10 +1435,7 @@ def get_sql(self, ctx: SqlContext | None = None) -> str:
14411435
querystring = self._delete_sql(ctx)
14421436

14431437
elif not self._select_into and self._insert_table:
1444-
if self._with:
1445-
querystring = self._with_sql(ctx)
1446-
else:
1447-
querystring = ""
1438+
querystring = self._with_sql(ctx) if self._with else ""
14481439

14491440
if self._replace:
14501441
querystring += self._replace_sql(ctx)
@@ -1464,11 +1455,7 @@ def get_sql(self, ctx: SqlContext | None = None) -> str:
14641455
querystring += " " + self._select_sql(ctx)
14651456

14661457
else:
1467-
if self._with:
1468-
querystring = self._with_sql(ctx)
1469-
else:
1470-
querystring = ""
1471-
1458+
querystring = self._with_sql(ctx) if self._with else ""
14721459
querystring += self._select_sql(ctx)
14731460

14741461
if self._insert_table:
@@ -1568,7 +1555,7 @@ def _for_update_sql(self, ctx: SqlContext, lock_strength="UPDATE") -> str:
15681555
for_update = f" FOR {lock_strength}"
15691556
if self._for_update_of:
15701557
for_update += (
1571-
f' OF {", ".join([Table(item).get_sql(ctx) for item in self._for_update_of])}'
1558+
f" OF {', '.join([Table(item).get_sql(ctx) for item in self._for_update_of])}"
15721559
)
15731560
if self._for_update_nowait:
15741561
for_update += " NOWAIT"
@@ -1761,8 +1748,9 @@ def __init__(
17611748
def on(self, criterion: Criterion | None, collate: str | None = None) -> QueryBuilder:
17621749
if criterion is None:
17631750
raise JoinException(
1764-
"Parameter 'criterion' is required for a "
1765-
"{type} JOIN but was not supplied.".format(type=self.type_label)
1751+
"Parameter 'criterion' is required for a {type} JOIN but was not supplied.".format(
1752+
type=self.type_label
1753+
)
17661754
)
17671755

17681756
self.query.do_join(JoinOn(self.item, self.how, criterion, collate)) # type:ignore[arg-type]
@@ -1771,8 +1759,9 @@ def on(self, criterion: Criterion | None, collate: str | None = None) -> QueryBu
17711759
def on_field(self, *fields: Any) -> QueryBuilder:
17721760
if not fields:
17731761
raise JoinException(
1774-
"Parameter 'fields' is required for a "
1775-
"{type} JOIN but was not supplied.".format(type=self.type_label)
1762+
"Parameter 'fields' is required for a {type} JOIN but was not supplied.".format(
1763+
type=self.type_label
1764+
)
17761765
)
17771766

17781767
criterion = None
@@ -2181,7 +2170,8 @@ def _unique_key_clauses(self, ctx: SqlContext) -> list[str]:
21812170

21822171
def _primary_key_clause(self, ctx: SqlContext) -> str:
21832172
columns = ",".join(
2184-
column.get_name_sql(ctx) for column in self._primary_key # type:ignore[union-attr]
2173+
column.get_name_sql(ctx)
2174+
for column in self._primary_key # type:ignore[union-attr]
21852175
)
21862176
return f"PRIMARY KEY ({columns})"
21872177

pypika_tortoise/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ def ignore_copy(func: Callable[[T_Self, str], T_Retval]) -> Callable[[T_Self, st
4242
"""
4343

4444
def _getattr(self: T_Self, name: str) -> T_Retval:
45-
if name in [
45+
if name in (
4646
"__copy__",
4747
"__deepcopy__",
4848
"__getstate__",
4949
"__setstate__",
5050
"__getnewargs__",
51-
]:
51+
):
5252
raise AttributeError(
5353
"'%s' object has no attribute '%s'" % (self.__class__.__name__, name)
5454
)

pyproject.toml

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,19 @@ packages = [
2222
[tool.poetry.group.dev.dependencies]
2323
ruff = "*"
2424
mypy = "*"
25-
isort = "*"
26-
black = "*"
27-
pytest = "*"
2825
bandit = "*"
29-
pytest-xdist = "*"
30-
pytest-cov = "*"
3126
twine = "*"
3227
typing-extensions = {version = "*", python = "<3.11"}
3328

29+
[tool.poetry.group.test.dependencies]
30+
pytest = "*"
31+
pytest-cov = "*"
32+
pytest-xdist = "*"
33+
3434
[build-system]
3535
requires = ["poetry-core>=2.0.0"]
3636
build-backend = "poetry.core.masonry.api"
3737

38-
[tool.black]
39-
line-length = 100
40-
target-version = ['py39', 'py310', 'py311', 'py312', 'py313']
41-
4238
[tool.mypy]
4339
pretty = true
4440
python_version = "3.9"
@@ -49,6 +45,7 @@ line-length = 100
4945

5046
[tool.ruff.lint]
5147
extend-select = [
48+
"I", # https://docs.astral.sh/ruff/rules/#isort-i
5249
"FA", # https://docs.astral.sh/ruff/rules/#flake8-future-annotations-fa
5350
"UP", # https://docs.astral.sh/ruff/rules/#pyupgrade-up
5451
"RUF100", # https://docs.astral.sh/ruff/rules/#ruff-specific-rules-ruf
@@ -57,9 +54,6 @@ ignore = ["UP031", "UP032"] # https://docs.astral.sh/ruff/rules/printf-string-fo
5754

5855
[tool.ruff.lint.per-file-ignores]
5956
'pypika_tortoise/__init__.py' = ['F401']
60-
'pypika_tortoise/dialects/__init__.py' = ['F401']
61-
'tests/test_functions.py' = ['UP034']
62-
'tests/dialects/test_postgresql.py' = ['RUF100']
6357

6458
[tool.bandit]
6559
exclude_dirs = ["tests", "conftest.py"]

tests/dialects/test_postgresql.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def test_json_contains_for_field(self):
147147
)
148148

149149
self.assertEqual(
150-
"SELECT * " 'FROM "abc" ' 'WHERE "json"@>\'{"dates":"2018-07-10 - 2018-07-17"}\'',
150+
'SELECT * FROM "abc" WHERE "json"@>\'{"dates":"2018-07-10 - 2018-07-17"}\'',
151151
str(q),
152152
)
153153

@@ -200,7 +200,7 @@ def test_json_has_key(self):
200200
q = (
201201
PostgreSQLQuery.from_(self.table_abc)
202202
.select("*")
203-
.where(self.table_abc.json.has_key("dates")) # noqa: W601
203+
.where(self.table_abc.json.has_key("dates"))
204204
)
205205

206206
self.assertEqual('SELECT * FROM "abc" WHERE "json"?\'dates\'', str(q))

0 commit comments

Comments
 (0)