Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate lint/format tool from isort/black to ruff and run unittest of examples in ci #1596

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,17 @@ jobs:
- name: Run ci
run: make ci
- name: Upload Coverage
run: coveralls --service=github
run: |
pip3 install --upgrade coveralls
coveralls --service=github
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COVERALLS_FLAG_NAME: ${{ matrix.python-version }}
COVERALLS_PARALLEL: true
- name: Test Examples
run: |
cd examples/blacksheep && PYTHONPATH=. pytest _tests.py && cd ../..
cd examples/fastapi && PYTHONPATH=. pytest _tests.py && cd ../..

coveralls:
name: Finish Coveralls
Expand Down
18 changes: 8 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,21 @@ deps:
@poetry install -E asyncpg -E aiomysql -E accel -E psycopg -E asyncodbc

check: deps build
ifneq ($(shell which black),)
black --check $(checkfiles) || (echo "Please run 'make style' to auto-fix style issues" && false)
ifneq ($(shell which ruff),)
(ruff format --check $(checkfiles) && ruff check $(checkfiles)) || (echo "Please run 'make style' to auto-fix style issues" && false)
endif
ruff check $(checkfiles)
mypy $(checkfiles)
#pylint -d C,W,R $(checkfiles)
#bandit -r $(checkfiles)make
twine check dist/*

lint: deps build
ifneq ($(shell which black),)
black --check $(checkfiles) || (echo "Please run 'make style' to auto-fix style issues" && false)
ifneq ($(shell which ruff),)
(ruff format --check $(checkfiles) && ruff check $(checkfiles)) || (echo "Please run 'make style' to auto-fix style issues" && false)
endif
ruff check $(checkfiles)
mypy $(checkfiles)
#pylint $(checkfiles)
bandit -r $(checkfiles)
bandit -c pyproject.toml -r $(checkfiles)
twine check dist/*

test: deps
Expand Down Expand Up @@ -69,15 +67,15 @@ _testall: test_sqlite test_postgres_asyncpg test_postgres_psycopg test_mysql_myi
testall: deps _testall
coverage report

ci: check testall
ci: check _testall

docs: deps
rm -fR ./build
sphinx-build -M html docs build

style: deps
isort -src $(checkfiles)
black $(checkfiles)
ruff check --fix $(checkfiles)
ruff format $(checkfiles)

build: deps
rm -fR dist/
Expand Down
1 change: 1 addition & 0 deletions examples/fastapi/_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytest
from asgi_lifespan import LifespanManager
from httpx import ASGITransport, AsyncClient

from main import app
from models import Users

Expand Down
529 changes: 240 additions & 289 deletions poetry.lock

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ darglint = "*"
pylint = "*"
pygments = "*"
bandit = "*"
black = "*"
codespell = "*"
# Test tools
coveralls = "*"
Expand Down Expand Up @@ -106,13 +105,6 @@ asyncodbc = ["asyncodbc"]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[tool.isort]
profile = "black"

[tool.black]
line-length = 100
target-version = ["py38", "py39", "py310", "py311"]

[tool.mypy]
pretty = true
ignore_missing_imports = true
Expand Down Expand Up @@ -189,5 +181,14 @@ source = ["tortoise"]
[tool.coverage.report]
show_missing = true

[tool.ruff]
line-length = 100

[tool.ruff.lint]
ignore = ["E501"]
extend-select = [
"I", # isort
]

[tool.bandit]
exclude_dirs = ["tests", 'examples/*/_tests.py', "conftest.py"]
6 changes: 3 additions & 3 deletions tests/backends/test_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ async def test_ssl_custom(self):
pass

async def test_application_name(self):
self.db_config["connections"]["models"]["credentials"][
"application_name"
] = "mytest_application"
self.db_config["connections"]["models"]["credentials"]["application_name"] = (
"mytest_application"
)
await Tortoise.init(self.db_config, _create_db=True)

conn = connections.get("models")
Expand Down
22 changes: 14 additions & 8 deletions tests/fields/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,25 +155,31 @@ async def test_aggregate_sum_no_exist_field_with_f_expression(self):
FieldError,
"There is no non-virtual field not_exist on Model DecimalFields",
):
await testmodels.DecimalFields.all().annotate(sum_decimal=Sum(F("not_exist"))).values(
"sum_decimal"
await (
testmodels.DecimalFields.all()
.annotate(sum_decimal=Sum(F("not_exist")))
.values("sum_decimal")
)

async def test_aggregate_sum_different_field_type_at_right_with_f_expression(self):
with self.assertRaisesRegex(
FieldError, "Cannot use arithmetic expression between different field type"
):
await testmodels.DecimalFields.all().annotate(
sum_decimal=Sum(F("decimal") + F("id"))
).values("sum_decimal")
await (
testmodels.DecimalFields.all()
.annotate(sum_decimal=Sum(F("decimal") + F("id")))
.values("sum_decimal")
)

async def test_aggregate_sum_different_field_type_at_left_with_f_expression(self):
with self.assertRaisesRegex(
FieldError, "Cannot use arithmetic expression between different field type"
):
await testmodels.DecimalFields.all().annotate(
sum_decimal=Sum(F("id") + F("decimal"))
).values("sum_decimal")
await (
testmodels.DecimalFields.all()
.annotate(sum_decimal=Sum(F("id") + F("decimal")))
.values("sum_decimal")
)

async def test_aggregate_avg(self):
await testmodels.DecimalFields.create(decimal=Decimal("0"), decimal_nodec=1)
Expand Down
3 changes: 2 additions & 1 deletion tests/schema/models_fk_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@

class One(Model):
tournament: fields.ForeignKeyRelation[Two] = fields.ForeignKeyField(
"models.Two", on_delete="WABOOM" # type:ignore
"models.Two",
on_delete="WABOOM", # type:ignore
)
3 changes: 2 additions & 1 deletion tests/schema/models_o2o_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@

class One(Model):
tournament: fields.OneToOneRelation[Two] = fields.OneToOneField(
"models.Two", on_delete="WABOOM" # type:ignore
"models.Two",
on_delete="WABOOM", # type:ignore
)
8 changes: 5 additions & 3 deletions tests/test_prefetching.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ async def test_prefetch_unknown_field(self):
tournament = await Tournament.create(name="tournament")
await Event.create(name="First", tournament=tournament)
await Event.create(name="Second", tournament=tournament)
await Tournament.all().prefetch_related(
Prefetch("events1", queryset=Event.filter(name="First"))
).first()
await (
Tournament.all()
.prefetch_related(Prefetch("events1", queryset=Event.filter(name="First")))
.first()
)

async def test_prefetch_m2m(self):
tournament = await Tournament.create(name="tournament")
Expand Down
3 changes: 1 addition & 2 deletions tortoise/backends/base/schema_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

if TYPE_CHECKING: # pragma: nocoverage
from tortoise.backends.base.client import BaseDBAsyncClient
from tortoise.fields.relational import ForeignKeyFieldInstance # noqa
from tortoise.fields.relational import ManyToManyFieldInstance
from tortoise.fields.relational import ForeignKeyFieldInstance, ManyToManyFieldInstance # noqa
from tortoise.models import Model

# pylint: disable=R0201
Expand Down
2 changes: 1 addition & 1 deletion tortoise/backends/mysql/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from asyncmy.charset import charset_by_name
except ImportError:
import aiomysql as mysql
from pymysql.charset import charset_by_name
from pymysql import err as errors
from pymysql.charset import charset_by_name

from pypika import MySQLQuery

Expand Down
16 changes: 10 additions & 6 deletions tortoise/backends/oracle/executor.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
from typing import TYPE_CHECKING, cast

from tortoise import Model
from tortoise.backends.odbc.executor import ODBCExecutor

if TYPE_CHECKING:
from .client import OracleClient # noqa:F401


class OracleExecutor(ODBCExecutor):
async def _process_insert_result(self, instance: Model, results: int) -> None:
sql = "SELECT SEQUENCE_NAME FROM ALL_TAB_IDENTITY_COLS where TABLE_NAME = ? and OWNER = ?"
ret = await self.db.execute_query_dict(
sql, values=[instance._meta.db_table, self.db.database] # type: ignore
)
db = cast("OracleClient", self.db)
ret = await db.execute_query_dict(sql, values=[instance._meta.db_table, db.database])
try:
seq = ret[0]["SEQUENCE_NAME"]
except IndexError:
return
sql = f"SELECT {seq}.CURRVAL FROM DUAL"
ret = await self.db.execute_query_dict(sql)
await super(OracleExecutor, self)._process_insert_result(instance, ret[0]["CURRVAL"])
sql = f"SELECT {seq}.CURRVAL FROM DUAL" # nosec
ret = await db.execute_query_dict(sql)
await super()._process_insert_result(instance, ret[0]["CURRVAL"])
4 changes: 3 additions & 1 deletion tortoise/contrib/mysql/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ def __init__(self, binary_compression: bool = True, **kwargs: Any) -> None:
self.SQL_TYPE = "BINARY(16)"
self._binary_compression = binary_compression

def to_db_value(self, value: Any, instance: "Union[Type[Model], Model]") -> Optional[Union[str, bytes]]: # type: ignore
def to_db_value( # type: ignore
self, value: Any, instance: "Union[Type[Model], Model]"
) -> Optional[Union[str, bytes]]:
# Make sure that value is a UUIDv4
# If not, raise an error
# This is to prevent UUIDv1 or any other version from being stored in the database
Expand Down
3 changes: 1 addition & 2 deletions tortoise/contrib/mysql/json_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
from typing import Any, Dict, List

from pypika.functions import Cast
from pypika.terms import Criterion
from pypika.terms import Criterion, Term, ValueWrapper
from pypika.terms import Function as PypikaFunction
from pypika.terms import Term, ValueWrapper

from tortoise.filters import not_equal

Expand Down
3 changes: 1 addition & 2 deletions tortoise/contrib/mysql/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
from typing import Any, Optional

from pypika.enums import Comparator
from pypika.terms import BasicCriterion
from pypika.terms import BasicCriterion, Term
from pypika.terms import Function as PypikaFunction
from pypika.terms import Term


class Comp(Comparator): # type: ignore
Expand Down
4 changes: 2 additions & 2 deletions tortoise/contrib/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ async def _tearDownDB(self) -> None:
for app in Tortoise.apps.values():
for model in app.values():
quote_char = model._meta.db.query_class._builder().QUOTE_CHAR
await model._meta.db.execute_script( # nosec
f"DELETE FROM {quote_char}{model._meta.db_table}{quote_char}"
await model._meta.db.execute_script(
f"DELETE FROM {quote_char}{model._meta.db_table}{quote_char}" # nosec
)
await super()._tearDownDB()

Expand Down
3 changes: 1 addition & 2 deletions tortoise/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
from pypika import Field as PypikaField
from pypika import Table
from pypika.functions import DistinctOptionFunction
from pypika.terms import ArithmeticExpression, Criterion
from pypika.terms import ArithmeticExpression, Criterion, Term
from pypika.terms import Function as PypikaFunction
from pypika.terms import Term
from pypika.utils import format_alias_sql

from tortoise.exceptions import ConfigurationError, FieldError, OperationalError
Expand Down
Loading