Skip to content

Commit

Permalink
Apply extra Ruff rule
Browse files Browse the repository at this point in the history
  • Loading branch information
gerlero committed Nov 2, 2024
1 parent 9a55004 commit 052b3d5
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 187 deletions.
42 changes: 21 additions & 21 deletions foamlib/_cases/_async.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from __future__ import annotations

import asyncio
import multiprocessing
import sys
from contextlib import asynccontextmanager
from typing import TYPE_CHECKING, Any, Callable, Optional, TypeVar, Union, overload
from typing import TYPE_CHECKING, Any, Callable, TypeVar, overload

if sys.version_info >= (3, 9):
from collections.abc import (
Expand All @@ -22,7 +24,6 @@

import aioshutil

from .._files import FoamFieldFile
from ._base import FoamCaseBase
from ._run import FoamCaseRunBase
from ._subprocess import run_async
Expand All @@ -31,6 +32,8 @@
if TYPE_CHECKING:
import os

from .._files import FoamFieldFile

X = TypeVar("X")
Y = TypeVar("Y")

Expand All @@ -48,7 +51,7 @@ class AsyncFoamCase(FoamCaseRunBase):

class TimeDirectory(FoamCaseRunBase.TimeDirectory):
@property
def _case(self) -> "AsyncFoamCase":
def _case(self) -> AsyncFoamCase:
return AsyncFoamCase(self.path.parent)

async def cell_centers(self) -> FoamFieldFile:
Expand Down Expand Up @@ -91,7 +94,7 @@ async def _cpus(cpus: int) -> AsyncGenerator[None, None]:

@staticmethod
async def _run(
cmd: Union[Sequence[Union[str, "os.PathLike[str]"]], str],
cmd: Sequence[str | os.PathLike[str]] | str,
*,
cpus: int,
**kwargs: Any,
Expand All @@ -101,19 +104,18 @@ async def _run(

@staticmethod
async def _rmtree(
path: Union["os.PathLike[str]", str], *, ignore_errors: bool = False
path: os.PathLike[str] | str, *, ignore_errors: bool = False
) -> None:
await aioshutil.rmtree(path, ignore_errors=ignore_errors)

@staticmethod
async def _copytree(
src: Union["os.PathLike[str]", str],
dest: Union["os.PathLike[str]", str],
src: os.PathLike[str] | str,
dest: os.PathLike[str] | str,
*,
symlinks: bool = False,
ignore: Optional[
Callable[[Union["os.PathLike[str]", str], Collection[str]], Collection[str]]
] = None,
ignore: Callable[[os.PathLike[str] | str, Collection[str]], Collection[str]]
| None = None,
) -> None:
await aioshutil.copytree(src, dest, symlinks=symlinks, ignore=ignore)

Expand All @@ -127,16 +129,14 @@ async def clean(self, *, check: bool = False) -> None:
await coro

@overload
def __getitem__(
self, index: Union[int, float, str]
) -> "AsyncFoamCase.TimeDirectory": ...
def __getitem__(self, index: int | float | str) -> AsyncFoamCase.TimeDirectory: ...

@overload
def __getitem__(self, index: slice) -> Sequence["AsyncFoamCase.TimeDirectory"]: ...
def __getitem__(self, index: slice) -> Sequence[AsyncFoamCase.TimeDirectory]: ...

def __getitem__(
self, index: Union[int, slice, float, str]
) -> Union["AsyncFoamCase.TimeDirectory", Sequence["AsyncFoamCase.TimeDirectory"]]:
self, index: int | slice | float | str
) -> AsyncFoamCase.TimeDirectory | Sequence[AsyncFoamCase.TimeDirectory]:
ret = super().__getitem__(index)
if isinstance(ret, FoamCaseBase.TimeDirectory):
return AsyncFoamCase.TimeDirectory(ret)
Expand All @@ -148,10 +148,10 @@ async def _prepare(self, *, check: bool = True, log: bool = True) -> None:

async def run(
self,
cmd: Optional[Union[Sequence[Union[str, "os.PathLike[str]"]], str]] = None,
cmd: Sequence[str | os.PathLike[str]] | str | None = None,
*,
parallel: Optional[bool] = None,
cpus: Optional[int] = None,
parallel: bool | None = None,
cpus: int | None = None,
check: bool = True,
log: bool = True,
) -> None:
Expand Down Expand Up @@ -192,7 +192,7 @@ async def restore_0_dir(self) -> None:
@awaitableasynccontextmanager
@asynccontextmanager
async def copy(
self, dst: Optional[Union["os.PathLike[str]", str]] = None
self, dst: os.PathLike[str] | str | None = None
) -> AsyncGenerator[Self, None]:
"""
Make a copy of this case.
Expand All @@ -213,7 +213,7 @@ async def copy(
@awaitableasynccontextmanager
@asynccontextmanager
async def clone(
self, dst: Optional[Union["os.PathLike[str]", str]] = None
self, dst: os.PathLike[str] | str | None = None
) -> AsyncGenerator[Self, None]:
"""
Clone this case (make a clean copy).
Expand Down
33 changes: 14 additions & 19 deletions foamlib/_cases/_base.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
from __future__ import annotations

import shutil
import sys
from pathlib import Path
from typing import (
TYPE_CHECKING,
Optional,
Union,
overload,
)
from typing import TYPE_CHECKING, overload

if sys.version_info >= (3, 9):
from collections.abc import (
Expand All @@ -28,7 +25,7 @@


class FoamCaseBase(Sequence["FoamCaseBase.TimeDirectory"]):
def __init__(self, path: Union["os.PathLike[str]", str] = Path()) -> None:
def __init__(self, path: os.PathLike[str] | str = Path()) -> None:
self.path = Path(path).absolute()

class TimeDirectory(AbstractSet[FoamFieldFile]):
Expand All @@ -40,11 +37,11 @@ class TimeDirectory(AbstractSet[FoamFieldFile]):
:param path: The path to the time directory.
"""

def __init__(self, path: Union["os.PathLike[str]", str]) -> None:
def __init__(self, path: os.PathLike[str] | str) -> None:
self.path = Path(path).absolute()

@property
def _case(self) -> "FoamCaseBase":
def _case(self) -> FoamCaseBase:
return FoamCaseBase(self.path.parent)

@property
Expand Down Expand Up @@ -97,7 +94,7 @@ def __str__(self) -> str:
return str(self.path)

@property
def _times(self) -> Sequence["FoamCaseBase.TimeDirectory"]:
def _times(self) -> Sequence[FoamCaseBase.TimeDirectory]:
times = []
for p in self.path.iterdir():
if p.is_dir():
Expand All @@ -113,16 +110,14 @@ def _times(self) -> Sequence["FoamCaseBase.TimeDirectory"]:
return times

@overload
def __getitem__(
self, index: Union[int, float, str]
) -> "FoamCaseBase.TimeDirectory": ...
def __getitem__(self, index: int | float | str) -> FoamCaseBase.TimeDirectory: ...

@overload
def __getitem__(self, index: slice) -> Sequence["FoamCaseBase.TimeDirectory"]: ...
def __getitem__(self, index: slice) -> Sequence[FoamCaseBase.TimeDirectory]: ...

def __getitem__(
self, index: Union[int, slice, float, str]
) -> Union["FoamCaseBase.TimeDirectory", Sequence["FoamCaseBase.TimeDirectory"]]:
self, index: int | slice | float | str
) -> FoamCaseBase.TimeDirectory | Sequence[FoamCaseBase.TimeDirectory]:
if isinstance(index, str):
return FoamCaseBase.TimeDirectory(self.path / index)
if isinstance(index, float):
Expand All @@ -136,20 +131,20 @@ def __getitem__(
def __len__(self) -> int:
return len(self._times)

def __delitem__(self, key: Union[int, float, str]) -> None:
def __delitem__(self, key: int | float | str) -> None:
shutil.rmtree(self[key].path)

@property
def name(self) -> str:
"""The name of the case."""
return self.path.name

def file(self, path: Union["os.PathLike[str]", str]) -> FoamFile:
def file(self, path: os.PathLike[str] | str) -> FoamFile:
"""Return a FoamFile object for the given path in the case."""
return FoamFile(self.path / path)

@property
def _nsubdomains(self) -> Optional[int]:
def _nsubdomains(self) -> int | None:
"""Return the number of subdomains as set in the decomposeParDict, or None if no decomposeParDict is found."""
try:
nsubdomains = self.decompose_par_dict["numberOfSubdomains"]
Expand Down
Loading

0 comments on commit 052b3d5

Please sign in to comment.