Skip to content

Commit

Permalink
fix several lints
Browse files Browse the repository at this point in the history
  • Loading branch information
leoschwarz committed Nov 7, 2024
1 parent 7ee1dd6 commit bb5cf47
Show file tree
Hide file tree
Showing 27 changed files with 94 additions and 58 deletions.
12 changes: 9 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,15 @@ indent-width = 4
target-version = "py39"

[tool.ruff.lint]
#select = ["ANN", "BLE", "D103", "E", "F", "PLW", "PTH", "SIM", "UP", "TCH", "N"]
select = ["PLW", "SIM", "UP", "EXE", "E701", "E702", "E703", "E711", "E713", "E714", "FA100", "FA102", "W191"]
ignore = ["ANN101", "ANN102"]
select = ["ANN", "BLE", "D103", "E", "EXE", "F", "N", "PLW", "PTH", "SIM", "TCH", "UP", "W191"]
ignore = ["ANN101", "ANN102", "ANN401"]

[tool.ruff.lint.per-file-ignores]
"**/bfabric_scripts/**" = ["ALL"]
"**/wrapper_creator/**" = ["ALL"]
"**/examples/**" = ["ALL"]
"**/tests/**" = ["ALL"]
"noxfile.py" = ["ALL"]

[tool.licensecheck]
using = "PEP631"
Expand Down
3 changes: 1 addition & 2 deletions src/bfabric/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import importlib.metadata

from bfabric.bfabric import Bfabric, BfabricAPIEngineType
from bfabric.config import BfabricAuth, BfabricClientConfig
from bfabric.config.bfabric_auth import BfabricAuth
from bfabric.config.bfabric_client_config import BfabricClientConfig

__all__ = [
Expand All @@ -11,5 +11,4 @@
"BfabricClientConfig",
]


__version__ = importlib.metadata.version("bfabric")
10 changes: 6 additions & 4 deletions src/bfabric/bfabric.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
from functools import cached_property
from pathlib import Path
from pprint import pprint
from typing import Literal, Any
from collections.abc import Generator
from typing import Literal, Any, TYPE_CHECKING

from loguru import logger
from rich.console import Console
Expand All @@ -37,6 +36,9 @@
from bfabric.results.result_container import ResultContainer
from bfabric.utils.paginator import compute_requested_pages, BFABRIC_QUERY_LIMIT

if TYPE_CHECKING:
from collections.abc import Generator


class BfabricAPIEngineType(Enum):
"""Choice of engine to use."""
Expand Down Expand Up @@ -277,15 +279,15 @@ def __repr__(self) -> str:

__str__ = __repr__

def __getstate__(self):
def __getstate__(self) -> dict[str, Any]:
return {
"config": self._config,
"auth": self._auth,
"engine_type": self._engine_type,
"query_counter": self.query_counter,
}

def __setstate__(self, state):
def __setstate__(self, state: dict[str, Any]) -> None:
self._config = state["config"]
self._auth = state["auth"]
self._engine_type = state["engine_type"]
Expand Down
1 change: 0 additions & 1 deletion src/bfabric/bfabric2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@

warnings.warn("bfabric.bfabric2 module is deprecated, use bfabric instead", DeprecationWarning)
# TODO deprecated - import from bfabric instead
from bfabric.bfabric import Bfabric, BfabricAPIEngineType, get_system_auth
5 changes: 3 additions & 2 deletions src/bfabric/config/bfabric_client_config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import Annotated
from typing import Annotated, Any

from pydantic import BaseModel, BeforeValidator, Field, TypeAdapter, AnyHttpUrl

Expand All @@ -25,7 +25,8 @@ class BfabricClientConfig(BaseModel):
application_ids: Annotated[dict[str, int], Field(default_factory=dict)]
job_notification_emails: Annotated[str, Field(default="")]

def __init__(self, **kwargs) -> None:
def __init__(self, **kwargs: Any) -> None:
# TODO remove this custom constructor (note that this is currently used in some places when "None" is passed)
super().__init__(**{key: value for key, value in kwargs.items() if value is not None})

def copy_with(
Expand Down
8 changes: 4 additions & 4 deletions src/bfabric/config/config_file.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import os
from typing import Annotated
from typing import Annotated, Any

from loguru import logger
from pydantic import BaseModel, Field, model_validator
Expand All @@ -21,14 +21,14 @@ class EnvironmentConfig(BaseModel):

@model_validator(mode="before")
@classmethod
def gather_config(cls, values):
def gather_config(cls, values: dict[str, Any]) -> dict[str, Any]:
"""Gathers all configs into the config field."""
values["config"] = {key: value for key, value in values.items() if key not in ["login", "password"]}
return values

@model_validator(mode="before")
@classmethod
def gather_auth(cls, values):
def gather_auth(cls, values: dict[str, Any]) -> dict[str, Any]:
if "login" in values:
values["auth"] = BfabricAuth.model_validate(values)
return values
Expand All @@ -40,7 +40,7 @@ class ConfigFile(BaseModel):

@model_validator(mode="before")
@classmethod
def gather_configs(cls, values):
def gather_configs(cls, values: dict[str, Any]) -> dict[str, Any]:
"""Gathers all configs into the configs field."""
configs = {}
for key, value in values.items():
Expand Down
2 changes: 1 addition & 1 deletion src/bfabric/engine/response_format_suds.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def convert_suds_type(item: Any) -> int | str | Any:
return item


def suds_asdict_recursive(d, convert_types: bool = False) -> dict[str, Value]:
def suds_asdict_recursive(d: Any, convert_types: bool = False) -> dict[str, Value]:
"""Convert Suds object into serializable format.
https://stackoverflow.com/a/15678861
:param d: The input suds object
Expand Down
2 changes: 1 addition & 1 deletion src/bfabric/entities/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

from typing import Any, TYPE_CHECKING

from bfabric import Bfabric
from bfabric.entities.core.entity import Entity
from bfabric.entities.core.has_one import HasOne

if TYPE_CHECKING:
from bfabric import Bfabric
from bfabric.entities.executable import Executable
from bfabric.entities.storage import Storage

Expand Down
6 changes: 3 additions & 3 deletions src/bfabric/entities/core/entity.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from __future__ import annotations

from collections.abc import Iterable
from typing import TYPE_CHECKING

from loguru import logger

from bfabric import Bfabric
from bfabric.experimental import MultiQuery
from bfabric.experimental.entity_lookup_cache import EntityLookupCache

if TYPE_CHECKING:
from collections.abc import Iterable
from bfabric import Bfabric
from typing import Any, Self


Expand Down Expand Up @@ -101,7 +101,7 @@ def __repr__(self) -> str:
__str__ = __repr__

@classmethod
def __check_ids_list(cls, ids) -> list[int]:
def __check_ids_list(cls, ids: list[int]) -> list[int]:
"""Converts the ids to a list of integers (if they are not already) and raises an error if this fails or
there are duplicates."""
ids_requested = [int(id) for id in ids]
Expand Down
21 changes: 13 additions & 8 deletions src/bfabric/entities/core/has_many.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
from __future__ import annotations

from typing import Generic, TypeVar
from collections.abc import Iterator

from typing import Generic, TypeVar, TYPE_CHECKING
from bfabric.entities.core.relationship import Relationship
from polars import DataFrame

from bfabric import Bfabric
from bfabric.entities.core.entity import Entity # type: ignore
from bfabric.entities.core.relationship import Relationship
if TYPE_CHECKING:
from collections.abc import Iterator
from bfabric import Bfabric

# noinspection PyUnresolvedReferences
from bfabric.entities.core.entity import Entity

E = TypeVar("E", bound="Entity")
T = TypeVar("T")


class HasMany(Relationship[E]):
Expand All @@ -28,15 +31,17 @@ def __init__(
self._client_property = client_property
self._optional = optional

def __get__(self, obj, objtype=None) -> _HasManyProxy:
def __get__(self, obj: T | None, objtype: type[T] | None = None) -> _HasManyProxy:
cache_attr = f"_HasMany__{self._ids_property or self._bfabric_field}_cache"
if obj is None:
raise ValueError("Cannot access HasMany relationship on class")
if not hasattr(obj, cache_attr):
ids = self._get_ids(obj)
client = getattr(obj, self._client_property)
setattr(obj, cache_attr, _HasManyProxy(entity_type=self._entity_type, ids=ids, client=client))
return getattr(obj, cache_attr)

def _get_ids(self, obj) -> list[int]:
def _get_ids(self, obj: T) -> list[int]:
if self._bfabric_field is not None:
if self._ids_property is not None:
raise ValueError("Exactly one of bfabric_field and ids_property must be set, but both are set")
Expand Down
12 changes: 8 additions & 4 deletions src/bfabric/entities/core/has_one.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from __future__ import annotations

from typing import TypeVar
from typing import TypeVar, TYPE_CHECKING

from bfabric.entities.core.entity import Entity # type: ignore
from bfabric.entities.core.relationship import Relationship

if TYPE_CHECKING:
# noinspection PyUnresolvedReferences
from bfabric.entities.core.entity import Entity

E = TypeVar("E", bound="Entity")
T = TypeVar("T")


class HasOne(Relationship[E]):
Expand All @@ -14,13 +18,13 @@ def __init__(self, entity: str, *, bfabric_field: str, optional: bool = False) -
self._bfabric_field = bfabric_field
self._optional = optional

def __get__(self, obj, objtype=None) -> E | None:
def __get__(self, obj: T | None, objtype: type[T] | None = None) -> E | None:
cache_attr = f"_HasOne__{self._bfabric_field}_cache"
if not hasattr(obj, cache_attr):
setattr(obj, cache_attr, self._load_entity(obj=obj))
return getattr(obj, cache_attr)

def _load_entity(self, obj) -> E | None:
def _load_entity(self, obj: T) -> E | None:
client = obj._client
entity_data = obj.data_dict.get(self._bfabric_field)
if self._optional and entity_data is None:
Expand Down
6 changes: 4 additions & 2 deletions src/bfabric/entities/core/relationship.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import importlib
from functools import cached_property
from typing import TypeVar, Generic
from typing import TypeVar, Generic, TYPE_CHECKING

from bfabric.entities.core.entity import Entity # type: ignore

if TYPE_CHECKING:
from bfabric.entities.core.entity import Entity

E = TypeVar("E", bound="Entity")

Expand Down
6 changes: 4 additions & 2 deletions src/bfabric/entities/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import tempfile
from pathlib import Path
from typing import Any
from typing import Any, TYPE_CHECKING

from polars import DataFrame

from bfabric import Bfabric
from bfabric.entities.core.entity import Entity

if TYPE_CHECKING:
from bfabric import Bfabric


class Dataset(Entity):
"""Immutable representation of a single dataset in B-Fabric.
Expand Down
6 changes: 4 additions & 2 deletions src/bfabric/entities/executable.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from __future__ import annotations

from typing import Any
from typing import Any, TYPE_CHECKING

from bfabric import Bfabric
from bfabric.entities.core.entity import Entity

if TYPE_CHECKING:
from bfabric import Bfabric


class Executable(Entity):
ENDPOINT = "executable"
Expand Down
2 changes: 1 addition & 1 deletion src/bfabric/entities/externaljob.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
from functools import cached_property
from typing import Any, TYPE_CHECKING

from bfabric import Bfabric
from bfabric.entities.core.entity import Entity
from bfabric.entities.core.has_one import HasOne

if TYPE_CHECKING:
from bfabric import Bfabric
from bfabric.entities.workunit import Workunit
from bfabric.entities.executable import Executable

Expand Down
6 changes: 4 additions & 2 deletions src/bfabric/entities/multiplexid.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from __future__ import annotations

from typing import Any
from typing import Any, TYPE_CHECKING

from bfabric import Bfabric
from bfabric.entities.core.entity import Entity

if TYPE_CHECKING:
from bfabric import Bfabric


class MultiplexId(Entity):
ENDPOINT = "multiplexid"
Expand Down
2 changes: 1 addition & 1 deletion src/bfabric/entities/multiplexkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
from functools import cached_property
from typing import Any, TYPE_CHECKING

from bfabric import Bfabric
from bfabric.entities.core.entity import Entity
from bfabric.entities.core.has_many import HasMany


if TYPE_CHECKING:
from bfabric import Bfabric
from bfabric.entities.multiplexid import MultiplexId


Expand Down
2 changes: 1 addition & 1 deletion src/bfabric/entities/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

from typing import Any, TYPE_CHECKING

from bfabric import Bfabric
from bfabric.entities.core.entity import Entity
from bfabric.entities.core.has_one import HasOne

if TYPE_CHECKING:
from bfabric import Bfabric
from bfabric.entities.project import Project


Expand Down
6 changes: 4 additions & 2 deletions src/bfabric/entities/parameter.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from __future__ import annotations

from typing import Any
from typing import Any, TYPE_CHECKING

from bfabric import Bfabric
from bfabric.entities.core.entity import Entity

if TYPE_CHECKING:
from bfabric import Bfabric


class Parameter(Entity):
ENDPOINT = "parameter"
Expand Down
6 changes: 4 additions & 2 deletions src/bfabric/entities/project.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from __future__ import annotations

from typing import Any
from typing import Any, TYPE_CHECKING

from bfabric import Bfabric
from bfabric.entities.core.entity import Entity

if TYPE_CHECKING:
from bfabric import Bfabric


class Project(Entity):
ENDPOINT = "project"
Expand Down
Loading

0 comments on commit bb5cf47

Please sign in to comment.