Skip to content

Commit 6ca73e0

Browse files
authored
refactor: apply future type hints style (#1884)
* refactor: apply future style type hints * docs: update changelog * Add RUF100 to ruff extend check rules * Fix codacy issues * refactor: change some function names from `_generate_xxx` to `_get_xxx` * refactor: change function name * refactor: more readable string format * fix codacy issues
1 parent 5aae984 commit 6ca73e0

File tree

93 files changed

+1005
-907
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+1005
-907
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ endif
4141

4242
lint: deps build
4343
ifneq ($(shell which black),)
44-
black --check $(checkfiles) || (echo "Please run 'make style' to auto-fix style issues" && false)
44+
black $(checkfiles)
4545
endif
46-
ruff check $(checkfiles)
46+
ruff check --fix $(checkfiles)
4747
mypy $(checkfiles)
4848
#pylint $(checkfiles)
4949
bandit -c pyproject.toml -r $(checkfiles)

examples/blacksheep/server.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# pylint: disable=E0401,E0611
2-
from typing import Union
2+
from __future__ import annotations
3+
34
from uuid import UUID
45

56
from blacksheep import Response
@@ -25,7 +26,7 @@
2526

2627

2728
@app.router.get("/")
28-
async def users_list() -> Union[UserPydanticOut]:
29+
async def users_list() -> UserPydanticOut:
2930
return ok(await UserPydanticOut.from_queryset(Users.all()))
3031

3132

examples/fastapi/schemas.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from typing import TYPE_CHECKING
24

35
from models import Users

examples/signals.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
This example demonstrates model signals usage
33
"""
44

5-
from typing import Optional
5+
from __future__ import annotations
66

77
from tortoise import BaseDBAsyncClient, Tortoise, fields, run_async
88
from tortoise.models import Model
@@ -21,33 +21,31 @@ def __str__(self):
2121

2222

2323
@pre_save(Signal)
24-
async def signal_pre_save(
25-
sender: "type[Signal]", instance: Signal, using_db, update_fields
26-
) -> None:
24+
async def signal_pre_save(sender: type[Signal], instance: Signal, using_db, update_fields) -> None:
2725
print(sender, instance, using_db, update_fields)
2826

2927

3028
@post_save(Signal)
3129
async def signal_post_save(
32-
sender: "type[Signal]",
30+
sender: type[Signal],
3331
instance: Signal,
3432
created: bool,
35-
using_db: "Optional[BaseDBAsyncClient]",
33+
using_db: BaseDBAsyncClient | None,
3634
update_fields: list[str],
3735
) -> None:
3836
print(sender, instance, using_db, created, update_fields)
3937

4038

4139
@pre_delete(Signal)
4240
async def signal_pre_delete(
43-
sender: "type[Signal]", instance: Signal, using_db: "Optional[BaseDBAsyncClient]"
41+
sender: type[Signal], instance: Signal, using_db: BaseDBAsyncClient | None
4442
) -> None:
4543
print(sender, instance, using_db)
4644

4745

4846
@post_delete(Signal)
4947
async def signal_post_delete(
50-
sender: "type[Signal]", instance: Signal, using_db: "Optional[BaseDBAsyncClient]"
48+
sender: type[Signal], instance: Signal, using_db: BaseDBAsyncClient | None
5149
) -> None:
5250
print(sender, instance, using_db)
5351

pyproject.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,11 @@ show_missing = true
198198
line-length = 100
199199
[tool.ruff.lint]
200200
ignore = ["E501"]
201+
extend-select = [
202+
"FA", # https://docs.astral.sh/ruff/rules/#flake8-future-annotations-fa
203+
"UP", # https://docs.astral.sh/ruff/rules/#pyupgrade-up
204+
"RUF100", # https://docs.astral.sh/ruff/rules/#ruff-specific-rules-ruf
205+
]
201206

202207
[tool.bandit]
203208
exclude_dirs = ["tests", 'examples/*/_tests.py', "conftest.py"]

tests/backends/test_capabilities.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class TestCapabilities(test.TestCase):
66
# pylint: disable=E1101
77

88
async def asyncSetUp(self) -> None:
9-
await super(TestCapabilities, self).asyncSetUp()
9+
await super().asyncSetUp()
1010
self.db = connections.get("models")
1111
self.caps = self.db.capabilities
1212

tests/benchmarks/test_bulk_create.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ def test_bulk_create_few_fields(benchmark):
99

1010
data = [
1111
BenchmarkFewFields(
12-
level=random.choice([10, 20, 30, 40, 50]), text=f"Insert from C, item {i}" # nosec
12+
level=random.choice([10, 20, 30, 40, 50]), # nosec
13+
text=f"Insert from C, item {i}",
1314
)
1415
for i in range(100)
1516
]

tests/contrib/test_pydantic.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
class TestPydantic(test.TestCase):
3131
async def asyncSetUp(self) -> None:
32-
await super(TestPydantic, self).asyncSetUp()
32+
await super().asyncSetUp()
3333
self.Event_Pydantic = pydantic_model_creator(Event)
3434
self.Event_Pydantic_List = pydantic_queryset_creator(Event)
3535
self.Tournament_Pydantic = pydantic_model_creator(Tournament)
@@ -1392,7 +1392,7 @@ def test_exclude_readonly(self):
13921392

13931393
class TestPydanticCycle(test.TestCase):
13941394
async def asyncSetUp(self) -> None:
1395-
await super(TestPydanticCycle, self).asyncSetUp()
1395+
await super().asyncSetUp()
13961396
self.Employee_Pydantic = pydantic_model_creator(Employee)
13971397

13981398
self.root = await Employee.create(name="Root")
@@ -1599,7 +1599,7 @@ async def test_serialisation(self):
15991599

16001600
class TestPydanticComputed(test.TestCase):
16011601
async def asyncSetUp(self) -> None:
1602-
await super(TestPydanticComputed, self).asyncSetUp()
1602+
await super().asyncSetUp()
16031603
self.Employee_Pydantic = pydantic_model_creator(Employee)
16041604
self.employee = await Employee.create(name="Some Employee")
16051605
self.maxDiff = None

tests/fields/subclass_fields.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from enum import Enum, IntEnum
24
from typing import Any
35

tests/fields/test_db_index.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from typing import Any
24

35
from pypika_tortoise.terms import Field
@@ -45,7 +47,7 @@ def test_index_repr(self):
4547
assert repr(Index(fields=("id",))) == "Index(fields=['id'])"
4648
assert repr(Index(fields=("id", "name"))) == "Index(fields=['id', 'name'])"
4749
assert repr(Index(fields=("id",), name="MyIndex")) == "Index(fields=['id'], name='MyIndex')"
48-
assert repr(Index(Field("id"))) == f'Index({str(Field("id"))})'
50+
assert repr(Index(Field("id"))) == f"Index({str(Field('id'))})"
4951
assert repr(Index(Field("a"), name="Id")) == f"Index({str(Field('a'))}, name='Id')"
5052
with self.assertRaises(ConfigurationError):
5153
Index(Field("id"), fields=("name",))

0 commit comments

Comments
 (0)