Skip to content

Commit

Permalink
use ruff best practices (#24)
Browse files Browse the repository at this point in the history
Co-authored-by: Quinten <quinten.roets@gmail.com>
  • Loading branch information
github-actions[bot] and quintenroets committed Oct 6, 2024
1 parent 51d8cb3 commit ca91749
Show file tree
Hide file tree
Showing 21 changed files with 201 additions and 152 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 2.0.4
current_version = 2.0.5
commit = False
allow_dirty = True

Expand Down
15 changes: 8 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "superpathlib"
version = "2.0.4"
version = "2.0.5"
description = "Extended Pathlib"
authors = [{name = "Quinten Roets", email = "qdr2104@columbia.edu"}]
license = {text = "MIT"}
Expand Down Expand Up @@ -67,19 +67,20 @@ pythonpath = [
fix = true

[tool.ruff.lint]
select = [
"E", # pycodestyle errors
"W", # pycodestyle warnings
"F", # pyflakes
"I", # isort
"UP", # pyupgrade
select = ["ALL"]
ignore = [
"ANN101", # annotate self
"ANN102", # annotate cls
"ANN401", # annotated with Any
"D", # docstrings
]

[tool.ruff.lint.isort]
known-first-party = ["src"]

[tool.ruff.lint.per-file-ignores]
"__init__.py" = ["F401"]
"tests/*" = ["S101"] # assert used

[tool.setuptools.package-data]
superpathlib = ["py.typed"]
14 changes: 7 additions & 7 deletions src/superpathlib/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
import sys


class Path(pathlib.Path):
class Path(pathlib.Path): # pragma: nocover
"""
Extend pathlib functionality and enable further extensions by inheriting.
"""

# _flavour attribute explicitly required to inherit from pathlib
if sys.version_info.minor <= 11:
if sys.version_info < (3, 12):
_flavour = (
pathlib._windows_flavour # type: ignore[attr-defined] # noqa
pathlib._windows_flavour # type: ignore[attr-defined] # noqa: SLF001
if os.name == "nt"
else pathlib._posix_flavour # type: ignore[attr-defined] # noqa
else pathlib._posix_flavour # type: ignore[attr-defined] # noqa: SLF001
)
else: # pragma: nocover
else:
_flavour = (
pathlib.ntpath # type: ignore[attr-defined] # noqa
pathlib.ntpath # type: ignore[attr-defined]
if os.name == "nt"
else pathlib.posixpath # type: ignore[attr-defined] # noqa
else pathlib.posixpath # type: ignore[attr-defined]
)
8 changes: 4 additions & 4 deletions src/superpathlib/cached_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ class Path(metadata_properties.Path):
def cached_content(self) -> CachedFileContent[dict[str, str]]:
from package_utils.storage import CachedFileContent

return CachedFileContent(self, default={}) # type: ignore
return CachedFileContent(self, default={}) # type: ignore[arg-type]

def create_cached_content(self, default: T) -> CachedFileContent[T]:
from package_utils.storage import CachedFileContent

return CachedFileContent(self, default=default) # type: ignore
return CachedFileContent(self, default=default) # type: ignore[arg-type]

@cached_property
def cached_text(self) -> CachedFileContent[str]:
Expand All @@ -39,7 +39,7 @@ def save_function(_: Any, content: str) -> None:
self.text = content

return CachedFileContent(
self, # type: ignore
self, # type: ignore[arg-type]
default="",
load_function=load_function,
save_function=save_function,
Expand All @@ -56,7 +56,7 @@ def save_function(_: Any, content: bytes) -> None:
self.byte_content = content

return CachedFileContent(
self, # type: ignore
self, # type: ignore[arg-type]
default=b"",
load_function=load_function,
save_function=save_function,
Expand Down
2 changes: 1 addition & 1 deletion src/superpathlib/common_folders.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Path(base.Path):

@classmethod
@classproperty
def HOME(cls: type[T]) -> T:
def HOME(cls: type[T]) -> T: # noqa: N802
return cls.home()

@classmethod
Expand Down
13 changes: 7 additions & 6 deletions src/superpathlib/content_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import json
import typing
from collections.abc import Iterable
from typing import Any

from . import base

if typing.TYPE_CHECKING: # pragma: nocover
from collections.abc import Iterable

from numpy.typing import NDArray


Expand Down Expand Up @@ -68,29 +69,29 @@ def yaml(self) -> dict[str, Any] | list[Any]:
import yaml

# C implementation much faster but only supported on Linux
Loader: type[yaml.CFullLoader | yaml.FullLoader] = (
Loader: type[yaml.CFullLoader | yaml.FullLoader] = ( # noqa: N806
yaml.CFullLoader if hasattr(yaml, "CFullLoader") else yaml.FullLoader
)
return yaml.load(self.text, Loader=Loader) or {}
return yaml.load(self.text, Loader=Loader) or {} # noqa: S506

@yaml.setter
def yaml(self, value: dict[str, Any] | list[Any]) -> None:
import yaml

# C implementation much faster but only supported on Linux
Dumper = yaml.CDumper if hasattr(yaml, "CDumper") else yaml.Dumper
Dumper = yaml.CDumper if hasattr(yaml, "CDumper") else yaml.Dumper # noqa: N806
self.text = yaml.dump(value, Dumper=Dumper, width=1024)

@property
def numpy(self) -> NDArray[Any]:
import numpy as np

with self.open("rb") as fp:
return np.load(fp) # type: ignore
return np.load(fp) # type: ignore[no-any-return]

@numpy.setter
def numpy(self, value: NDArray[Any]) -> None:
import numpy as np

with self.open("wb") as fp:
np.save(fp, value) # noqa
np.save(fp, value)
26 changes: 17 additions & 9 deletions src/superpathlib/encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ class EncryptedPath(Path):
@cached_property
def password(self) -> str: # pragma: nocover
if "GITHUB_ACTION" in os.environ:
password = "github_action_password"
password = "github_action_password" # noqa: S105
else:
command = 'ksshaskpass -- "Enter passphrase for file encryption: "'
password = subprocess.getoutput(command)
password = subprocess.getoutput(command) # noqa: S605
return password

@property
Expand All @@ -39,24 +39,32 @@ def decryption_command(self) -> tuple[str, ...]:
def read_bytes(self) -> bytes:
encrypted_bytes = super().read_bytes()
if encrypted_bytes:
process = subprocess.Popen(
self.decryption_command, stdin=subprocess.PIPE, stdout=subprocess.PIPE
process = subprocess.Popen( # noqa: S603
self.decryption_command,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
)
decrypted_bytes, _ = process.communicate(input=encrypted_bytes)
else:
decrypted_bytes = encrypted_bytes
return decrypted_bytes

def write_bytes(self, data: bytes) -> int: # type: ignore
process = subprocess.Popen(
self.encryption_command, stdin=subprocess.PIPE, stdout=subprocess.PIPE
def write_bytes(self, data: bytes) -> int: # type: ignore[override]
process = subprocess.Popen( # noqa: S603
self.encryption_command,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
)
encrypted_data = process.communicate(input=data)[0]
return super().write_bytes(encrypted_data)

def read_text(self, encoding: str | None = None, errors: str | None = None) -> str:
def read_text(
self,
encoding: str | None = None, # noqa: ARG002
errors: str | None = None, # noqa: ARG002
) -> str:
return self.read_bytes().decode()

def write_text(self, data: str, **_: Any) -> int: # type: ignore
def write_text(self, data: str, **_: Any) -> int: # type: ignore[override]
byte_data = data.encode()
return self.write_bytes(byte_data)
Loading

0 comments on commit ca91749

Please sign in to comment.