Skip to content

Commit 4402582

Browse files
authored
chore: upgrade deps and fix ruff lint issues (#374)
* chore: upgrade deps and apply ruff lint for tests/ * style: fix ruff lint issues
1 parent 252cb97 commit 4402582

File tree

9 files changed

+37
-51
lines changed

9 files changed

+37
-51
lines changed

aerich/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ async def init(self) -> None:
4242
async def _upgrade(self, conn, version_file) -> None:
4343
file_path = Path(Migrate.migrate_location, version_file)
4444
m = import_py_file(file_path)
45-
upgrade = getattr(m, "upgrade")
45+
upgrade = m.upgrade
4646
await conn.execute_script(await upgrade(conn))
4747
await Aerich.create(
4848
version=version_file,
@@ -89,7 +89,7 @@ async def downgrade(self, version: int, delete: bool) -> List[str]:
8989
) as conn:
9090
file_path = Path(Migrate.migrate_location, file)
9191
m = import_py_file(file_path)
92-
downgrade = getattr(m, "downgrade")
92+
downgrade = m.downgrade
9393
downgrade_sql = await downgrade(conn)
9494
if not downgrade_sql.strip():
9595
raise DowngradeError("No downgrade items found")

aerich/cli.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ async def cli(ctx: Context, config, app) -> None:
4747
location = tool["location"]
4848
tortoise_orm = tool["tortoise_orm"]
4949
src_folder = tool.get("src_folder", CONFIG_DEFAULT_VALUES["src_folder"])
50-
except NonExistentKey:
51-
raise UsageError("You need run `aerich init` again when upgrading to aerich 0.6.0+.")
50+
except NonExistentKey as e:
51+
raise UsageError(
52+
"You need run `aerich init` again when upgrading to aerich 0.6.0+."
53+
) from e
5254
add_src_path(src_folder)
5355
tortoise_config = get_tortoise_config(ctx, tortoise_orm)
5456
if not app:
@@ -182,10 +184,7 @@ async def init(ctx: Context, tortoise_orm, location, src_folder) -> None:
182184
add_src_path(src_folder)
183185
get_tortoise_config(ctx, tortoise_orm)
184186
config_path = Path(config_file)
185-
if config_path.exists():
186-
content = config_path.read_text()
187-
else:
188-
content = "[tool.aerich]"
187+
content = config_path.read_bytes() if config_path.exists() else "[tool.aerich]"
189188
doc: dict = tomlkit.parse(content)
190189
table = tomlkit.table()
191190
table["tortoise_orm"] = tortoise_orm

aerich/inspectdb/__init__.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import contextlib
34
from typing import Any, Callable, Dict, Optional, TypedDict
45

56
from pydantic import BaseModel
@@ -61,8 +62,8 @@ def translate(self) -> ColumnInfoDict:
6162
elif self.data_type == "bool":
6263
default = f"default={'True' if self.default == 'true' else 'False'}, "
6364
elif self.data_type in ("datetime", "timestamptz", "TIMESTAMP"):
64-
if "CURRENT_TIMESTAMP" == self.default:
65-
if "DEFAULT_GENERATED on update CURRENT_TIMESTAMP" == self.extra:
65+
if self.default == "CURRENT_TIMESTAMP":
66+
if self.extra == "DEFAULT_GENERATED on update CURRENT_TIMESTAMP":
6667
default = "auto_now=True, "
6768
else:
6869
default = "auto_now_add=True, "
@@ -94,10 +95,8 @@ class Inspect:
9495

9596
def __init__(self, conn: BaseDBAsyncClient, tables: list[str] | None = None) -> None:
9697
self.conn = conn
97-
try:
98+
with contextlib.suppress(AttributeError):
9899
self.database = conn.database # type:ignore[attr-defined]
99-
except AttributeError:
100-
pass
101100
self.tables = tables
102101

103102
@property

aerich/inspectdb/mysql.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,11 @@ async def get_columns(self, table: str) -> list[Column]:
4040
and c.TABLE_NAME = %s"""
4141
ret = await self.conn.execute_query_dict(sql, [self.database, table])
4242
for row in ret:
43-
non_unique = row["NON_UNIQUE"]
44-
if non_unique is None:
45-
unique = False
46-
else:
43+
unique = index = False
44+
if (non_unique := row["NON_UNIQUE"]) is not None:
4745
unique = not non_unique
48-
index_name = row["INDEX_NAME"]
49-
if index_name is None:
50-
index = False
51-
else:
52-
index = row["INDEX_NAME"] != "PRIMARY"
46+
if (index_name := row["INDEX_NAME"]) is not None:
47+
index = index_name != "PRIMARY"
5348
columns.append(
5449
Column(
5550
name=row["COLUMN_NAME"],

aerich/migrate.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ def diff_models(
271271
# m2m fields
272272
old_m2m_fields = cast(List[dict], old_model_describe.get("m2m_fields"))
273273
new_m2m_fields = cast(List[dict], new_model_describe.get("m2m_fields"))
274-
for action, option, change in diff(old_m2m_fields, new_m2m_fields):
274+
for action, _, change in diff(old_m2m_fields, new_m2m_fields):
275275
if change[0][0] == "db_constraint":
276276
continue
277277
new_value = change[0][1]
@@ -346,22 +346,14 @@ def diff_models(
346346
old_data_field_name = cast(str, old_data_field.get("name"))
347347
if len(changes) == 2:
348348
# rename field
349+
name_diff = (old_data_field_name, new_data_field_name)
350+
column_diff = (
351+
old_data_field.get("db_column"),
352+
new_data_field.get("db_column"),
353+
)
349354
if (
350-
changes[0]
351-
== (
352-
"change",
353-
"name",
354-
(old_data_field_name, new_data_field_name),
355-
)
356-
and changes[1]
357-
== (
358-
"change",
359-
"db_column",
360-
(
361-
old_data_field.get("db_column"),
362-
new_data_field.get("db_column"),
363-
),
364-
)
355+
changes[0] == ("change", "name", name_diff)
356+
and changes[1] == ("change", "db_column", column_diff)
365357
and old_data_field_name not in new_data_fields_name
366358
):
367359
if upgrade:

conftest.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import contextlib
23
import os
34
from typing import Generator
45

@@ -57,10 +58,8 @@ async def initialize_tests(event_loop, request) -> None:
5758
# Placing init outside the try block since it doesn't
5859
# establish connections to the DB eagerly.
5960
await Tortoise.init(config=tortoise_orm)
60-
try:
61+
with contextlib.suppress(DBConnectionError, OperationalError):
6162
await Tortoise._drop_databases()
62-
except (DBConnectionError, OperationalError):
63-
pass
6463
await Tortoise.init(config=tortoise_orm, _create_db=True)
6564
await generate_schema_for_client(Tortoise.get_connection("default"), safe=True)
6665

poetry.lock

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

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,7 @@ pretty = true
6767
python_version = "3.8"
6868
ignore_missing_imports = true
6969

70+
[tool.ruff]
71+
line-length = 100
7072
[tool.ruff.lint]
7173
ignore = ['E501']

tests/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33

44
def test_import_py_file() -> None:
55
m = import_py_file("aerich/utils.py")
6-
assert getattr(m, "import_py_file")
6+
assert getattr(m, "import_py_file", None)

0 commit comments

Comments
 (0)