Skip to content

Commit

Permalink
🎨 Add ruff & pyright
Browse files Browse the repository at this point in the history
  • Loading branch information
pajowu committed Jun 20, 2023
1 parent 2a606d8 commit 7686d23
Show file tree
Hide file tree
Showing 23 changed files with 210 additions and 98 deletions.
3 changes: 0 additions & 3 deletions .flake8

This file was deleted.

28 changes: 19 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,30 @@ repos:
- id: trailing-whitespace

# python checks
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort
args: ["--profile", "black", "--filter-files"]
- repo: https://github.com/psf/black
rev: 23.1.0
hooks:
- id: black
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.272
hooks:
- id: ruff
- repo: local
hooks:
- id: backend-pyright
name: Run typechecker for backend
entry: pdm run -p backend/ pyright backend/transcribee_backend
language: system
files: backend/.*
pass_filenames: false
- repo: local
hooks:
- id: flake8
exclude: ^backend/.*/migrations/.*$
- id: worker-pyright
name: Run typechecker for worker
entry: pdm run -p worker/ pyright worker/transcribee_worker
language: system
files: worker/.*
pass_filenames: false
- repo: local
hooks:
- id: backend-openapi
Expand Down
1 change: 1 addition & 0 deletions .ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
line-length = 100
47 changes: 42 additions & 5 deletions backend/pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dependencies = [
"fastapi>=0.92.0",
"uvicorn[standard]>=0.20.0",
"sqlmodel @ git+https://github.com/transcribee/sqlmodel.git@transcribee_main",
"alembic>=1.9.4",
"alembic>=1.11.1",
"transcribee-proto @ file:///${PROJECT_ROOT}/../proto",
"python-multipart>=0.0.6",
"filetype>=1.2.0",
Expand All @@ -40,6 +40,7 @@ dev = [
"pytest>=7.3.1",
"httpx>=0.24.0",
"pytest-alembic>=0.10.4",
"pyright>=1.1.314",
]

[tool.pdm.scripts]
Expand Down
2 changes: 1 addition & 1 deletion backend/scripts/reset_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
)
args = parser.parse_args()
with SessionContextManager() as session:
task = session.exec(
task = session.execute(
update(Task)
.where(
or_(Task.id == args.uuid, Task.document_id == args.uuid),
Expand Down
2 changes: 1 addition & 1 deletion backend/transcribee_backend/db/migrations/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from sqlalchemy import MetaData, engine_from_config, pool
from sqlmodel import SQLModel
from transcribee_backend.db import DATABASE_URL
from transcribee_backend.models import *
from transcribee_backend.models import * # noqa

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"""
import sqlalchemy as sa
import sqlmodel
from alembic import op

# revision identifiers, used by Alembic.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""
import sqlalchemy as sa
import sqlmodel
import sqlmodel.sql.sqltypes
from alembic import op

# revision identifiers, used by Alembic.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# pyright: basic
"""add TaskAttempt
Revision ID: 6392770332cd
Expand All @@ -8,12 +9,12 @@
import uuid

import sqlalchemy as sa
from sqlalchemy.sql.operators import is_
import sqlmodel.sql.sqltypes
import sqlmodel
from alembic import op
from sqlalchemy.dialects import sqlite
from transcribee_backend.config import settings
from transcribee_backend.helpers.time import now_tz_aware
from transcribee_backend.models.task import TaskState

# revision identifiers, used by Alembic.
revision = "6392770332cd"
Expand Down Expand Up @@ -76,7 +77,7 @@ def upgrade_with_autocommit() -> None:
"fk_task_assigned_worker_id_worker", type_="foreignkey"
)
batch_op.create_foreign_key(
None,
None, # type: ignore
"taskattempt",
["current_attempt_id"],
["id"],
Expand Down Expand Up @@ -107,11 +108,11 @@ def upgrade_with_autocommit() -> None:
)
bind = op.get_bind()
session = sqlmodel.Session(bind)
for task in session.exec(
Task.select().where(~sa.sql.operators.is_(Task.c.assigned_worker_id, None))
for task in session.execute(
Task.select().where(~is_(Task.c.assigned_worker_id, None))
).all():
attempt_id = uuid.uuid4()
session.exec(
session.execute(
sqlmodel.insert(TaskAttempt).values(
id=attempt_id,
task_id=task.id,
Expand All @@ -124,7 +125,7 @@ def upgrade_with_autocommit() -> None:
attempt_number=1,
)
)
session.exec(
session.execute(
sqlmodel.update(Task)
.where(Task.c.id == task.id)
.values(
Expand All @@ -139,7 +140,7 @@ def upgrade_with_autocommit() -> None:
now = now_tz_aware()
batch_op.execute(
sqlmodel.update(Task)
.where(Task.c.is_completed == True)
.where(Task.c.is_completed.is_(True))
.values(state="COMPLETED", state_changed_at=Task.c.completed_at)
)
batch_op.execute(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"""
import sqlalchemy as sa
import sqlmodel
from alembic import op

# revision identifiers, used by Alembic.
Expand All @@ -24,7 +23,11 @@ def upgrade() -> None:
)
batch_op.drop_constraint("fk_task_document_id_document", type_="foreignkey")
batch_op.create_foreign_key(
None, "document", ["document_id"], ["id"], ondelete="CASCADE"
None, # type: ignore
"document",
["document_id"],
["id"],
ondelete="CASCADE",
)

with op.batch_alter_table("taskdependency", schema=None) as batch_op:
Expand All @@ -41,10 +44,18 @@ def upgrade() -> None:
"fk_taskdependency_dependant_on_id_task", type_="foreignkey"
)
batch_op.create_foreign_key(
None, "task", ["dependent_task_id"], ["id"], ondelete="CASCADE"
None, # type: ignore
"task",
["dependent_task_id"],
["id"],
ondelete="CASCADE",
)
batch_op.create_foreign_key(
None, "task", ["dependant_on_id"], ["id"], ondelete="CASCADE"
None, # type: ignore
"task",
["dependant_on_id"],
["id"],
ondelete="CASCADE",
)

# ### end Alembic commands ###
Expand All @@ -53,8 +64,8 @@ def upgrade() -> None:
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table("taskdependency", schema=None) as batch_op:
batch_op.drop_constraint(None, type_="foreignkey")
batch_op.drop_constraint(None, type_="foreignkey")
batch_op.drop_constraint(None, type_="foreignkey") # type: ignore
batch_op.drop_constraint(None, type_="foreignkey") # type: ignore
batch_op.create_foreign_key(
"fk_taskdependency_dependant_on_id_task",
"task",
Expand All @@ -75,7 +86,7 @@ def downgrade() -> None:
)

with op.batch_alter_table("task", schema=None) as batch_op:
batch_op.drop_constraint(None, type_="foreignkey")
batch_op.drop_constraint(None, type_="foreignkey") # type: ignore
batch_op.create_foreign_key(
"fk_task_document_id_document", "document", ["document_id"], ["id"]
)
Expand Down
9 changes: 6 additions & 3 deletions backend/transcribee_backend/helpers/tasks.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime
from typing import Iterable, Optional

from sqlmodel import Session, select
from sqlmodel import Session, select, col
from transcribee_backend.config import settings
from transcribee_backend.db import SessionContextManager
from transcribee_backend.helpers.time import now_tz_aware
Expand All @@ -18,6 +18,9 @@ def finish_current_attempt(
if now is None:
now = now_tz_aware()

if task.current_attempt is None:
return

task.current_attempt.ended_at = now
task.current_attempt.last_keepalive = now
task.current_attempt.extra_data = extra_data
Expand All @@ -43,8 +46,8 @@ def timeouted_tasks(session: Session) -> Iterable[Task]:
statement = (
select(Task)
.where(
Task.current_attempt.has(
TaskAttempt.last_keepalive
col(Task.current_attempt).has(
col(TaskAttempt.last_keepalive)
< now_tz_aware() - datetime.timedelta(seconds=settings.worker_timeout)
),
)
Expand Down
7 changes: 5 additions & 2 deletions backend/transcribee_backend/models/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import uuid
from typing import Any, Dict, List, Literal, Optional

from sqlmodel import JSON, Column, Field, ForeignKey, Relationship, SQLModel
from sqlmodel import JSON, Column, Field, ForeignKey, Relationship, SQLModel, col
from sqlmodel.sql.sqltypes import GUID
from transcribee_backend.config import settings
from transcribee_backend.helpers.time import now_tz_aware
Expand Down Expand Up @@ -74,6 +74,7 @@ class Task(TaskBase, table=True):
sa_column=Column(
GUID, ForeignKey("taskattempt.id", ondelete="SET NULL", use_alter=True)
),
default=None,
)
current_attempt: Optional["TaskAttempt"] = Relationship(
sa_relationship_kwargs={
Expand Down Expand Up @@ -112,7 +113,9 @@ class TaskAttempt(SQLModel, table=True):
)

task_id: uuid.UUID = Field(
sa_column=Column(GUID, ForeignKey(Task.id, ondelete="CASCADE"), nullable=False),
sa_column=Column(
GUID, ForeignKey(col(Task.id), ondelete="CASCADE"), nullable=False
),
unique=False,
)
task: Task = Relationship(
Expand Down
Loading

0 comments on commit 7686d23

Please sign in to comment.