Skip to content

Commit 4b4e4a5

Browse files
authored
refactor: migrate lint tool from isort+black to ruff (#1963)
1 parent cb72b8a commit 4b4e4a5

File tree

11 files changed

+560
-655
lines changed

11 files changed

+560
-655
lines changed

Makefile

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,39 @@ help:
1414
@echo "Targets:"
1515
@echo " up Updates dev/test dependencies"
1616
@echo " deps Ensure dev/test dependencies are installed"
17-
@echo " check Checks that build is sane"
18-
@echo " test Runs all tests"
19-
@echo " docs Builds the documentation"
17+
@echo " check Checks that build is sane"
18+
@echo " test Runs all tests"
19+
@echo " docs Builds the documentation"
2020
@echo " style Auto-formats the code"
21+
@echo " lint Auto-formats the code and check type hints"
2122

2223
up:
2324
@poetry update
2425

2526
deps:
26-
@poetry install -E asyncpg -E aiomysql -E accel -E psycopg -E asyncodbc
27+
@poetry install --all-groups -E asyncpg -E accel -E psycopg -E asyncodbc -E aiomysql
2728

2829
deps_with_asyncmy:
29-
@poetry install -E asyncpg -E asyncmy -E accel -E psycopg -E asyncodbc
30+
@poetry install --all-groups -E asyncpg -E accel -E psycopg -E asyncodbc -E asyncmy
3031

31-
check: deps build _check
32+
check: build _check
3233
_check:
33-
ifneq ($(shell which black),)
34-
black --check $(checkfiles) || (echo "Please run 'make style' to auto-fix style issues" && false)
35-
endif
34+
ruff format --check $(checkfiles) || (echo "Please run 'make style' to auto-fix style issues" && false)
3635
ruff check $(checkfiles)
3736
mypy $(checkfiles)
3837
#pylint -d C,W,R $(checkfiles)
3938
#bandit -r $(checkfiles)make
4039
twine check dist/*
4140

42-
lint: deps build
43-
ifneq ($(shell which black),)
44-
black $(checkfiles)
45-
endif
41+
style: deps _style
42+
_style:
43+
ruff format $(checkfiles)
4644
ruff check --fix $(checkfiles)
45+
46+
lint: build _lint
47+
_lint:
48+
$(MAKE) _style
4749
mypy $(checkfiles)
48-
#pylint $(checkfiles)
4950
bandit -c pyproject.toml -r $(checkfiles)
5051
twine check dist/*
5152

@@ -94,11 +95,6 @@ docs: deps
9495
rm -fR ./build
9596
sphinx-build -M html docs build
9697

97-
style: deps _style
98-
_style:
99-
isort -src $(checkfiles)
100-
black $(checkfiles)
101-
10298
build: deps
10399
rm -fR dist/
104100
poetry build

docs/CONTRIBUTING.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ We have a ``Makefile`` that has the common operations listed, to get started jus
3636
up Updates dev/test dependencies
3737
deps Ensure dev/test dependencies are installed
3838
check Checks that build is sane
39-
lint Reports all linter violations
4039
test Runs all tests
4140
docs Builds the documentation
4241
style Auto-formats the code
42+
lint Auto-formats the code and check type hints
4343

4444
So to run the tests you just need to run ``make test``, etc…
4545

@@ -59,7 +59,7 @@ The code is structured in the following directories:
5959
Common DB Backend code
6060
``tortoise/contrib/``:
6161
Anything that helps people use the project, such as Testing framework and linter plugins
62-
``tortoise/tests/``:
62+
``tests/``:
6363
The Tortoise test code
6464

6565

docs/conf.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
import sys
1818
from datetime import datetime
1919

20-
import importlib_metadata
20+
try:
21+
import importlib.metadata as importlib_metadata
22+
except ImportError:
23+
import importlib_metadata
2124

2225
sys.path.insert(0, os.path.abspath("."))
2326
sys.path.insert(0, os.path.abspath(".."))

poetry.lock

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

pyproject.toml

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ include = ["CHANGELOG.rst", "LICENSE", "README.rst"]
1111
dynamic = [ "classifiers" ]
1212
requires-python = ">=3.9"
1313
dependencies = [
14-
"pypika-tortoise (>=0.6.1,<1.0.0); python_version < '4.0'",
14+
"pypika-tortoise (>=0.6.1,<1.0.0)",
1515
"iso8601 (>=2.1.0,<3.0.0); python_version < '4.0'",
1616
"aiosqlite (>=0.16.0,<1.0.0)",
1717
"pytz",
@@ -67,16 +67,15 @@ ruff = "*"
6767
darglint = "*"
6868
pylint = "*"
6969
bandit = "*"
70-
black = "*"
7170
codespell = "*"
72-
# Test tools
73-
coveralls = "*"
74-
pytest = "*"
75-
pytest-xdist = "*"
76-
pytest-cov = "*"
77-
pytest-codspeed = "*"
7871
# Pypi
7972
twine = "*"
73+
# mypy
74+
types-PyYAML = "*"
75+
types-pytz = "*"
76+
types-PyMySQL = "*"
77+
78+
[tool.poetry.group.contrib.dependencies]
8079
# Sample integration - Quart
8180
quart = "*"
8281
# Sample integration - Sanic
@@ -95,10 +94,15 @@ aiohttp = "*"
9594
# BlackSheep support
9695
blacksheep = "^2.0.8"
9796
pytest-asyncio = ">=0.24.0"
98-
# mypy
99-
types-PyYAML = "*"
100-
types-pytz = "*"
101-
types-PyMySQL = "*"
97+
98+
[tool.poetry.group.test.dependencies]
99+
# Test tools
100+
pytest = "*"
101+
pytest-xdist = "*"
102+
pytest-cov = "*"
103+
pytest-codspeed = "*"
104+
105+
[tool.poetry.group.docs.dependencies]
102106
# Documentation tools
103107
sphinx-immaterial = "*"
104108
sphinx-copybutton = "*"
@@ -109,13 +113,6 @@ docutils = "*"
109113
requires = ["poetry-core>=2.0.0"]
110114
build-backend = "poetry.core.masonry.api"
111115

112-
[tool.isort]
113-
profile = "black"
114-
115-
[tool.black]
116-
line-length = 100
117-
target-version = ["py39", "py310", "py311", "py312", "py313"]
118-
119116
[tool.mypy]
120117
pretty = true
121118
exclude = ["docs"]

tests/backends/test_postgres.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ async def test_ssl_custom(self):
8888
pass
8989

9090
async def test_application_name(self):
91-
self.db_config["connections"]["models"]["credentials"][
92-
"application_name"
93-
] = "mytest_application"
91+
self.db_config["connections"]["models"]["credentials"]["application_name"] = (
92+
"mytest_application"
93+
)
9494
await Tortoise.init(self.db_config, _create_db=True)
9595

9696
conn = connections.get("models")

tortoise/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def init_fk_o2o_field(model: type[Model], field: str, is_o2o=False) -> None:
172172
fk_object.to_field = related_model._meta.pk_attr
173173
related_field = related_model._meta.pk
174174
key_fk_object = deepcopy(related_field)
175-
fk_object.to_field_instance = related_field # type:ignore[arg-type,call-overload]
175+
fk_object.to_field_instance = related_field
176176
fk_object.field_type = fk_object.to_field_instance.field_type
177177

178178
key_field = f"{field}_id"
@@ -215,7 +215,7 @@ def init_fk_o2o_field(model: type[Model], field: str, is_o2o=False) -> None:
215215
description=fk_object.description,
216216
)
217217
)
218-
fk_relation.to_field_instance = fk_object.to_field_instance # type:ignore
218+
fk_relation.to_field_instance = fk_object.to_field_instance
219219
related_model._meta.add_field(backward_relation_name, fk_relation)
220220
if is_o2o and fk_object.pk:
221221
model._meta.pk_attr = key_field
@@ -481,8 +481,8 @@ async def init(
481481
if not modules:
482482
raise ConfigurationError('You must specify "db_url" and "modules" together')
483483
config = generate_config(db_url, modules)
484-
else:
485-
assert config is not None # To improve type hints
484+
elif config is None:
485+
raise ConfigurationError('You must specify "config" or "config_file" or "db_url"')
486486

487487
try:
488488
connections_config = config["connections"]

tortoise/backends/base/executor.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -531,9 +531,7 @@ def _make_prefetch_queries(self) -> None:
531531
relation_field = self.model._meta.fields_map[field_name]
532532
related_model: type[Model] = relation_field.related_model # type: ignore
533533
related_query = related_model.all().using_db(self.db)
534-
related_query.query = copy(
535-
related_query.model._meta.basequery
536-
) # type:ignore[assignment]
534+
related_query.query = copy(related_query.model._meta.basequery) # type:ignore[assignment]
537535
if forwarded_prefetches:
538536
related_query = related_query.prefetch_related(*forwarded_prefetches)
539537
self._prefetch_queries.setdefault(field_name, []).append((to_attr, related_query))

tortoise/contrib/postgres/json_functions.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,7 @@ def _create_json_criterion(
9898
operator.lt,
9999
operator.le,
100100
between_and,
101-
] or type(
102-
value
103-
) in (int, float, Decimal):
101+
] or type(value) in (int, float, Decimal):
104102
criteria = Cast(criteria[0], "numeric"), criteria[1]
105103

106104
return operator_(*criteria)

tortoise/contrib/sanic/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ def register_tortoise(
8181

8282
async def tortoise_init() -> None:
8383
await Tortoise.init(config=config, config_file=config_file, db_url=db_url, modules=modules)
84-
logger.info(
85-
"Tortoise-ORM started, %s, %s", connections._get_storage(), Tortoise.apps
86-
) # pylint: disable=W0212
84+
logger.info("Tortoise-ORM started, %s, %s", connections._get_storage(), Tortoise.apps) # pylint: disable=W0212
8785

8886
if generate_schemas:
8987

0 commit comments

Comments
 (0)