Skip to content

Commit

Permalink
Merge pull request #29 from qua-platform/update_linting_rules
Browse files Browse the repository at this point in the history
Refactor: update linting rules
  • Loading branch information
nulinspiratie authored Oct 25, 2024
2 parents 99351ce + 78644b0 commit 2fd2830
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
cache-dependency-path: poetry.lock

- name: Install python deps
run: poetry install --with dev
run: poetry install --with dev --all-extras

- name: Check python package typing
run: poetry run poe type
Expand Down
39 changes: 20 additions & 19 deletions poetry.lock

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

16 changes: 14 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ qualibrary = ["qualibrate-core"]

[tool.poetry.group.dev.dependencies]
mypy = "^1.10.0"
ruff = "^0.4.5"
ruff = "^0.7.0"
poethepoet = "^0.26.1"

#[tool.poetry.group.qm-dev.dependencies]
Expand All @@ -42,9 +42,21 @@ ignore_missing_imports = true
[tool.ruff]
line-length = 80
target-version = "py39"
exclude = ["calibrations"]

[tool.ruff.lint]
extend-select = ["I"]
select = [
"E", # pycodestyle
"F", # Pyflakes
"UP", # pyupgrade
"B", # flake8-bugbear
"SIM", # flake8-simplify
"I", # isort
]

[tool.ruff.lint.pycodestyle]
max-line-length = 80
max-doc-length = 80

[tool.poe.tasks]
lint = "ruff check ."
Expand Down
7 changes: 4 additions & 3 deletions qualibrate_runner/api/dependencies.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from collections.abc import Mapping
from functools import cache
from typing import Annotated, Mapping, cast
from typing import Annotated

from fastapi import Depends, HTTPException
from qualibrate.qualibration_graph import QualibrationGraph
Expand Down Expand Up @@ -39,13 +40,13 @@ def get_library(
def get_nodes(
library: Annotated[QualibrationLibrary, Depends(get_library)],
) -> Mapping[str, QualibrationNode]:
return cast(Mapping[str, QualibrationNode], library.get_nodes())
return library.get_nodes()


def get_graphs(
library: Annotated[QualibrationLibrary, Depends(get_library)],
) -> Mapping[str, QualibrationGraph]:
return cast(Mapping[str, QualibrationGraph], library.get_graphs())
return library.get_graphs()


def get_node(
Expand Down
12 changes: 5 additions & 7 deletions qualibrate_runner/api/routes/get_runnables.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Annotated, Any, Mapping, Sequence, cast
from collections.abc import Mapping, Sequence
from typing import Annotated, Any

from fastapi import APIRouter, Depends
from qualibrate.qualibration_graph import QualibrationGraph
Expand Down Expand Up @@ -45,22 +46,19 @@ def get_graphs(
def get_node(
node: Annotated[QualibrationNode, Depends(get_qnode)],
) -> Mapping[str, Any]:
return cast(Mapping[str, Any], node.serialize(exclude_targets=True))
return node.serialize(exclude_targets=True)


@get_runnables_router.get("/get_graph")
def get_graph(
graph: Annotated[QualibrationGraph, Depends(get_qgraph)],
cytoscape: bool = False,
) -> Mapping[str, Any]:
return cast(Mapping[str, Any], graph.serialize(cytoscape=cytoscape))
return graph.serialize(cytoscape=cytoscape)


@get_runnables_router.get("/get_graph/cytoscape")
def get_graph_cytoscape(
graph: Annotated[QualibrationGraph, Depends(get_qgraph)],
) -> Sequence[Mapping[str, Any]]:
return cast(
Sequence[Mapping[str, Any]],
graph.cytoscape_representation(graph.serialize()),
)
return graph.cytoscape_representation(graph.serialize())
3 changes: 2 additions & 1 deletion qualibrate_runner/api/routes/last_run.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Annotated, Any, Mapping, Optional, cast
from collections.abc import Mapping
from typing import Annotated, Any, Optional, cast

from fastapi import APIRouter, Depends
from qualibrate.models.execution_history import ExecutionHistory
Expand Down
3 changes: 2 additions & 1 deletion qualibrate_runner/api/routes/others.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Annotated, Mapping, Optional, cast
from collections.abc import Mapping
from typing import Annotated, Optional, cast

from fastapi import APIRouter, Depends, HTTPException, status

Expand Down
5 changes: 3 additions & 2 deletions qualibrate_runner/api/routes/submit.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Annotated, Any, Mapping, Type, cast
from collections.abc import Mapping
from typing import Annotated, Any, cast

from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, status
from pydantic import BaseModel
Expand Down Expand Up @@ -44,7 +45,7 @@ def submit_node_run(
detail="Already running",
)
validate_input_parameters(
cast(Type[BaseModel], node.parameters_class), input_parameters
cast(type[BaseModel], node.parameters_class), input_parameters
)
background_tasks.add_task(run_node, node, input_parameters, state)
return f"Node job {node.name} is submitted"
Expand Down
10 changes: 6 additions & 4 deletions qualibrate_runner/cli/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os
import sys
from collections.abc import Mapping
from pathlib import Path
from typing import Any, Mapping
from typing import Any

import click
import tomli_w
Expand Down Expand Up @@ -48,9 +49,10 @@ def _config_from_sources(
runner_mapping: dict[str, str] = {k: k for k in config_keys}
for arg_key, arg_value in ctx.params.items():
not_default_arg = not_default(ctx, arg_key)
if arg_key in runner_mapping.keys():
if not_default_arg or runner_mapping[arg_key] not in from_file:
from_file[runner_mapping[arg_key]] = arg_value
if arg_key in runner_mapping and (
not_default_arg or runner_mapping[arg_key] not in from_file
):
from_file[runner_mapping[arg_key]] = arg_value
return from_file


Expand Down
9 changes: 5 additions & 4 deletions qualibrate_runner/config/references/resolvers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from collections import defaultdict
from typing import Any, List, Mapping, Optional, Sequence, Set, Tuple, cast
from collections.abc import Mapping, Sequence
from typing import Any, Optional, cast

import jsonpatch
import jsonpointer
Expand Down Expand Up @@ -46,7 +47,7 @@ def find_all_references(

def check_cycles_in_references(
references: Mapping[str, Sequence[str]],
) -> Tuple[bool, Optional[Sequence[str]]]:
) -> tuple[bool, Optional[Sequence[str]]]:
"""Return True if the references has a cycle.
>>> check_cycles_in_references({"a": ("b",), "b": ("c",), "c": ("a",)})
Expand All @@ -55,8 +56,8 @@ def check_cycles_in_references(
(False, None)
"""
path: List[str] = []
visited: Set[str] = set()
path: list[str] = []
visited: set[str] = set()
cycled_item: str = ""

def visit(vertex: str) -> bool:
Expand Down
3 changes: 2 additions & 1 deletion qualibrate_runner/config/validation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from collections.abc import Mapping
from pathlib import Path
from typing import Any, Mapping, Optional
from typing import Any, Optional

import click
from pydantic import ValidationError
Expand Down
3 changes: 2 additions & 1 deletion qualibrate_runner/core/models/last_run.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections.abc import Mapping
from datetime import datetime
from enum import Enum
from typing import Any, Mapping, Optional, Union
from typing import Any, Optional, Union

from pydantic import BaseModel, Field, computed_field
from qualibrate.models.run_summary.graph import GraphRunSummary
Expand Down
18 changes: 11 additions & 7 deletions qualibrate_runner/core/run_job.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import traceback
from collections.abc import Mapping
from datetime import datetime
from typing import Any, Mapping, Type
from typing import Any, Optional, cast

from fastapi import HTTPException, status
from pydantic import BaseModel, ValidationError
from qualibrate.models.run_summary.graph import GraphRunSummary
from qualibrate.models.run_summary.node import NodeRunSummary
from qualibrate.qualibration_graph import QualibrationGraph
from qualibrate.qualibration_library import QualibrationLibrary
from qualibrate.qualibration_node import QualibrationNode
Expand All @@ -18,15 +21,15 @@


def validate_input_parameters(
parameters_class: Type[BaseModel],
parameters_class: type[BaseModel],
passed_parameters: Mapping[str, Any],
) -> BaseModel:
try:
return parameters_class.model_validate(passed_parameters)
except ValidationError as ex:
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=ex.errors()
)
) from ex


def get_active_library_or_error() -> QualibrationLibrary:
Expand Down Expand Up @@ -64,15 +67,16 @@ def run_node(
run_status = RunStatus.ERROR
raise
else:
idx = node.snapshot_idx if hasattr(node, "snapshot_idx") else -1
idx = idx if idx is not None else -1
_idx = node.snapshot_idx if hasattr(node, "snapshot_idx") else -1
idx = _idx if _idx is not None else -1
run_status = RunStatus.FINISHED
finally:
state.last_run = LastRun(
name=state.last_run.name,
status=run_status,
idx=idx,
run_result=node.run_summary,
# TODO: Make run summary generic
run_result=cast(Optional[NodeRunSummary], node.run_summary),
runnable_type=state.last_run.runnable_type,
passed_parameters=passed_input_parameters,
started_at=state.last_run.started_at,
Expand Down Expand Up @@ -126,7 +130,7 @@ def run_workflow(
name=state.last_run.name,
status=run_status,
idx=idx,
run_result=workflow.run_summary,
run_result=cast(Optional[GraphRunSummary], workflow.run_summary),
started_at=state.last_run.started_at,
completed_at=datetime.now(),
runnable_type=state.last_run.runnable_type,
Expand Down
3 changes: 2 additions & 1 deletion qualibrate_runner/utils/calibration_nodes_resolver.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Any, Mapping
from collections.abc import Mapping
from typing import Any

from pydantic import BaseModel

Expand Down

0 comments on commit 2fd2830

Please sign in to comment.