Skip to content

Commit

Permalink
chore/ruff (#7)
Browse files Browse the repository at this point in the history
* apply `ruff --select ALL`
  • Loading branch information
guillp authored Aug 5, 2024
1 parent 46da201 commit 02c48dd
Show file tree
Hide file tree
Showing 13 changed files with 1,458 additions and 1,431 deletions.
9 changes: 5 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
rev: v4.6.0
hooks:
- id: trailing-whitespace
args: [--markdown-linebreak-ext=md]
Expand All @@ -22,17 +22,18 @@ repos:
- --wrap-summaries=100
- --wrap-descriptions=100
- repo: https://github.com/asottile/blacken-docs
rev: 1.16.0
rev: 1.18.0
hooks:
- id: blacken-docs
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.13
rev: v0.5.6
hooks:
- id: ruff-format
- id: ruff
args: [ --fix ]
- id: ruff-format
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.8.0
rev: v1.11.1
hooks:
- id: mypy
args:
Expand Down
1 change: 0 additions & 1 deletion binapy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Top-level package for BinaPy."""


from .binapy import (
BinaPy,
InvalidExtensionMethodError,
Expand Down
20 changes: 10 additions & 10 deletions binapy/binapy.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
overload,
)

from typing_extensions import Literal
from typing_extensions import Literal, Self


class BinaPy(bytes):
Expand All @@ -41,7 +41,7 @@ def __new__(
value: bytes | str | int | SupportsBytes = b"",
encoding: str = "utf-8",
errors: str = "strict",
) -> BinaPy:
) -> Self:
"""Override base method to accept a string with a default encoding of "utf-8".
See Also:
Expand Down Expand Up @@ -249,12 +249,10 @@ def random_bits(cls, length: int) -> BinaPy:
return cls(secrets.token_bytes(length // 8))

@overload
def __getitem__(self, index: SupportsIndex) -> int:
... # pragma: no cover
def __getitem__(self, index: SupportsIndex) -> int: ... # pragma: no cover

@overload
def __getitem__(self, slice: slice) -> BinaPy: # noqa: A002
... # pragma: no cover
def __getitem__(self, slice: slice) -> BinaPy: ... # pragma: no cover

def __getitem__(self, slice: slice | SupportsIndex) -> int | BinaPy: # noqa: A002
"""Override the base method so that slicing returns a BinaPy instead of just bytes.
Expand Down Expand Up @@ -286,7 +284,7 @@ def char_at(self, index: int) -> str:

def __add__(
self,
other: Any,
other: object,
) -> BinaPy:
"""Override base method so that addition returns a BinaPy instead of bytes.
Expand All @@ -297,7 +295,9 @@ def __add__(
a BinaPy
"""
return self.__class__(super().__add__(other))
if isinstance(other, bytes):
return self.__class__(super().__add__(other))
raise NotImplementedError

def __radd__(self, other: bytes) -> BinaPy:
"""Override base method so that right addition returns a BinaPy instead of bytes.
Expand Down Expand Up @@ -395,7 +395,7 @@ def _get_serializer(cls, extension_name: str) -> Callable[..., BinaPy]:
raise NotImplementedError(msg)
return method

def encode_to(self, name: str, *args: Any, **kwargs: Any) -> BinaPy:
def encode_to(self, name: str, *args: Any, **kwargs: object) -> BinaPy:
"""Encode data from this BinaPy according to the format `name`.
Args:
Expand All @@ -411,7 +411,7 @@ def encode_to(self, name: str, *args: Any, **kwargs: Any) -> BinaPy:

return encoder(self, *args, **kwargs)

def to(self, name: str, *args: Any, **kwargs: Any) -> BinaPy:
def to(self, name: str, *args: object, **kwargs: object) -> BinaPy:
"""Alias for `encode_to()`.
Args:
Expand Down
1 change: 1 addition & 0 deletions binapy/compression/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
"""This module contains compression related utilities."""

from . import zlib # noqa: F401
1 change: 1 addition & 0 deletions binapy/compression/zlib.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""This module contains helpers for compressing/decompressing data using `zlib`."""

import zlib

from binapy import binapy_decoder, binapy_encoder
Expand Down
1 change: 1 addition & 0 deletions binapy/encoding/dumb.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Implement support for 'dumb' ciphers such as Caesar cipher."""

from __future__ import annotations

import string
Expand Down
6 changes: 2 additions & 4 deletions binapy/encoding/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ def url_encode(bp: bytes, *, safe: str = "/", plus_spaces: bool = True) -> str:
"""
if plus_spaces:
return quote_plus(bp, safe=safe)
else:
return quote(bp, safe=safe)
return quote(bp, safe=safe)


@binapy_decoder("url")
Expand All @@ -48,5 +47,4 @@ def url_decode(bp: bytes, *, plus_spaces: bool = True, errors: str = "replace")
"""
if plus_spaces:
return unquote_plus(bp.decode(), errors=errors).encode()
else:
return unquote(bp.decode(), errors=errors).encode()
return unquote(bp.decode(), errors=errors).encode()
3 changes: 1 addition & 2 deletions binapy/hashing/sha.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@


class ShaProtocol(Protocol):
def digest(self) -> bytes:
... # pragma: no cover
def digest(self) -> bytes: ... # pragma: no cover


def sha_hash(func: Callable[[bytes], ShaProtocol], bp: bytes) -> bytes:
Expand Down
4 changes: 2 additions & 2 deletions binapy/hashing/shake.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Helpers for the Shake Hash family."""

import functools
import hashlib
from typing import Callable, Sequence
Expand All @@ -9,8 +10,7 @@


class ShakeProtocol(Protocol):
def digest(self, length: int) -> bytes:
... # pragma: no cover
def digest(self, length: int) -> bytes: ... # pragma: no cover


def shake_hash(func: Callable[[bytes], ShakeProtocol], bp: bytes, length: int) -> bytes:
Expand Down
6 changes: 4 additions & 2 deletions binapy/parsing/json.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""This module contains helpers for converting data to/from JSON."""

from __future__ import annotations

import json
from datetime import datetime
from typing import Any, Callable, Iterable, Mapping
Expand All @@ -23,9 +25,9 @@ def _default_json_encode(data: Any) -> Any:
"""
if isinstance(data, datetime):
return int(data.timestamp())
elif isinstance(data, Mapping):
if isinstance(data, Mapping):
return dict(data)
elif isinstance(data, Iterable):
if isinstance(data, Iterable):
return list(data)
return str(data)

Expand Down
8 changes: 5 additions & 3 deletions binapy/parsing/pickle.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""This module implements helpers for (de)serializing to/from Python `pickle` objects."""

from __future__ import annotations

import pickle
from typing import Any
from pickle import PickleBuffer
from typing import Any, Callable, Iterable

from binapy import binapy_parser, binapy_serializer

Expand All @@ -13,7 +15,7 @@ def to_pickle(
*,
protocol: int | None = None,
fix_imports: bool = True,
buffer_callback: Any = None,
buffer_callback: Callable[[PickleBuffer], Any] | None = None,
) -> bytes:
"""Serialize an object using `pickle.dumps()`."""
return pickle.dumps(o, protocol=protocol, fix_imports=fix_imports, buffer_callback=buffer_callback)
Expand All @@ -26,7 +28,7 @@ def from_pickle(
fix_imports: bool = True,
encoding: str = "ASCII",
errors: str = "strict",
buffers: Any = None,
buffers: Iterable[object] | None = None,
) -> object:
"""Deserialize an object using `pickle.loads()`."""
return pickle.loads(bp, fix_imports=fix_imports, encoding=encoding, errors=errors, buffers=buffers) # noqa: S301
Loading

0 comments on commit 02c48dd

Please sign in to comment.