Skip to content

Commit

Permalink
Migrate to ruff.
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewwardrop committed Jun 4, 2024
1 parent 351f866 commit d365136
Show file tree
Hide file tree
Showing 33 changed files with 105 additions and 158 deletions.
55 changes: 9 additions & 46 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,31 +59,27 @@ include = [

[tool.hatch.envs.default]
dependencies = [
"black==23.3.0",
"flake8-docstrings==1.7.0",
"flake8-pyproject",
"flake8==5.0.4",
"pylint==2.14.5",
"ruff==0.4.7",
"pytest-cov==4.0.0",
"pytest==7.2.0",
]

[tool.hatch.envs.default.scripts]
tests = ["unit-tests", "linters"]
unit-tests = "pytest --cov=spec_classes --cov-report=term-missing --cov-report=xml -vv {args} tests"
unit-tests = "pytest --cov=spec_classes --cov-report=term-missing --cov-report=xml -vv {args:tests}"
linters = [
"flake8 spec_classes tests",
"pylint spec_classes",
"black --check spec_classes tests",
"ruff check",
"ruff format --check",
]
format = "black spec_classes tests"
format = "ruff format spec_classes tests"

[[tool.hatch.envs.test.matrix]]
python = ["37", "38", "39", "310", "311", "312"]

# Linting configuration

[tool.flake8]
[tool.ruff.lint]
select = ["E", "F", "W", "I", "PL"]
ignore = [
"C901",
"D100",
Expand All @@ -98,41 +94,8 @@ ignore = [
"D400",
"D401",
"D413",
"E128",
"E501",
"F405",
"W503",
"W504",
]
max-complexity = 10
application-import-names = "spec_classes"

[tool.pylint."MESSAGES CONTROL"]
disable = [
"assignment-from-no-return",
"cyclic-import",
"duplicate-code",
"exec-used",
"fixme",
"import-outside-toplevel",
"invalid-name",
"invalid-overridden-method",
"line-too-long",
"missing-docstring",
"no-member",
"protected-access",
"too-few-public-methods",
"too-many-arguments",
"too-many-branches",
"too-many-instance-attributes",
"too-many-lines",
"too-many-locals",
"too-many-nested-blocks",
"too-many-public-methods",
"too-many-return-statements",
"too-many-statements",
"ungrouped-imports",
"unused-argument",
"unused-wildcard-import",
"wildcard-import",
"PLR09",
"PLR2004",
]
12 changes: 6 additions & 6 deletions spec_classes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
from .errors import FrozenInstanceError
from .spec_class import spec_class
from .types import (
spec_property,
classproperty,
EMPTY,
MISSING,
SENTINEL,
Alias,
DeprecatedAlias,
Attr,
AttrProxy,
MISSING,
EMPTY,
SENTINEL,
DeprecatedAlias,
classproperty,
spec_property,
)

__author__ = "Matthew Wardrop"
Expand Down
22 changes: 9 additions & 13 deletions spec_classes/collections/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Any, Callable, Dict, List, Type

from spec_classes.methods.base import AttrMethodDescriptor
from spec_classes.types import Attr, MISSING
from spec_classes.types import MISSING, Attr
from spec_classes.utils.mutation import mutate_value, protect_via_deepcopy
from spec_classes.utils.type_checking import (
check_type,
Expand Down Expand Up @@ -164,25 +164,21 @@ def prepare(self):
return self

@abstractmethod
def _prepare_items(self):
... # pragma: no cover
def _prepare_items(self): ... # pragma: no cover

@abstractmethod
def _extractor(self, value_or_index, raise_if_missing=False) -> IndexedItem:
... # pragma: no cover
def _extractor(
self, value_or_index, raise_if_missing=False
) -> IndexedItem: ... # pragma: no cover

@abstractmethod
def _inserter(self, index, item):
... # pragma: no cover
def _inserter(self, index, item): ... # pragma: no cover

@abstractmethod
def add_item(self, item):
... # pragma: no cover
def add_item(self, item): ... # pragma: no cover

@abstractmethod
def transform_item(self, value_or_index, transform):
... # pragma: no cover
def transform_item(self, value_or_index, transform): ... # pragma: no cover

@abstractmethod
def remove_item(self, value_or_index):
... # pragma: no cover
def remove_item(self, value_or_index): ... # pragma: no cover
4 changes: 1 addition & 3 deletions spec_classes/collections/mappings.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ def add_items(self, items: Mapping):
self.add_item(k, v)
return self

def transform_item(
self, key, transform, *, attr_transforms=None
): # pylint: disable=arguments-renamed,arguments-differ
def transform_item(self, key, transform, *, attr_transforms=None): # pylint: disable=arguments-renamed,arguments-differ
return self._mutate_collection(
value_or_index=key,
extractor=self._extractor,
Expand Down
4 changes: 1 addition & 3 deletions spec_classes/collections/sequences.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ def transform_item(
require_pre_existent=True,
)

def remove_item(
self, value_or_index, *, by_index=MISSING
): # pylint: disable=arguments-differ
def remove_item(self, value_or_index, *, by_index=MISSING): # pylint: disable=arguments-differ
index, _ = self._extractor(
value_or_index, by_index=by_index, raise_if_missing=True
)
Expand Down
8 changes: 2 additions & 6 deletions spec_classes/collections/sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ def _inserter(self, index, item, replace=True): # pylint: disable=arguments-dif
self.collection.discard(index)
self.collection.add(item)

def add_item(
self, item, *, value_or_index=MISSING, replace=True, attrs=None
): # pylint: disable=arguments-differ
def add_item(self, item, *, value_or_index=MISSING, replace=True, attrs=None): # pylint: disable=arguments-differ
return self._mutate_collection(
value_or_index=value_or_index,
extractor=self._extractor,
Expand All @@ -67,9 +65,7 @@ def add_items(self, items: Iterable):
self.add_item(item)
return self

def transform_item(
self, item, transform, *, attr_transforms=None
): # pylint: disable=arguments-renamed,arguments-differ
def transform_item(self, item, transform, *, attr_transforms=None): # pylint: disable=arguments-renamed,arguments-differ
return self._mutate_collection(
value_or_index=item,
extractor=self._extractor,
Expand Down
6 changes: 2 additions & 4 deletions spec_classes/methods/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,10 @@ def method(self) -> Callable:

@property
@abstractmethod
def method_name(self) -> str:
... # pragma: no cover
def method_name(self) -> str: ... # pragma: no cover

@abstractmethod
def build_method(self) -> Callable:
... # pragma: no cover
def build_method(self) -> Callable: ... # pragma: no cover


class AttrMethodDescriptor(MethodDescriptor): # pylint: disable=abstract-method
Expand Down
2 changes: 1 addition & 1 deletion spec_classes/methods/collections/mappings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from inspect import Parameter
from typing import Any, Callable, Dict, TypeVar, Union

from spec_classes.types import Attr, MISSING
from spec_classes.types import MISSING, Attr
from spec_classes.utils.method_builder import MethodBuilder
from spec_classes.utils.mutation import mutate_attr
from spec_classes.utils.type_checking import type_label
Expand Down
2 changes: 1 addition & 1 deletion spec_classes/methods/collections/sequences.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from inspect import Parameter
from typing import Any, Callable, Dict, Union

from spec_classes.types import Attr, MISSING
from spec_classes.types import MISSING, Attr
from spec_classes.utils.method_builder import MethodBuilder
from spec_classes.utils.mutation import mutate_attr
from spec_classes.utils.type_checking import type_label
Expand Down
2 changes: 1 addition & 1 deletion spec_classes/methods/collections/sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from inspect import Parameter
from typing import Any, Callable, Dict, Union

from spec_classes.types import Attr, MISSING
from spec_classes.types import MISSING, Attr
from spec_classes.utils.method_builder import MethodBuilder
from spec_classes.utils.mutation import mutate_attr
from spec_classes.utils.type_checking import type_label
Expand Down
2 changes: 1 addition & 1 deletion spec_classes/methods/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from spec_classes.errors import FrozenInstanceError
from spec_classes.methods.scalar import WithAttrMethod
from spec_classes.types import Attr, MISSING
from spec_classes.types import MISSING, Attr
from spec_classes.utils.method_builder import MethodBuilder
from spec_classes.utils.mutation import (
invalidate_attrs,
Expand Down
2 changes: 1 addition & 1 deletion spec_classes/methods/scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from cached_property import cached_property
from lazy_object_proxy import Proxy

from spec_classes.types import Attr, MISSING
from spec_classes.types import MISSING, Attr
from spec_classes.utils.method_builder import MethodBuilder
from spec_classes.utils.mutation import mutate_attr, mutate_value, prepare_attr_value

Expand Down
9 changes: 5 additions & 4 deletions spec_classes/spec_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
import warnings
from collections import defaultdict
from typing import Any, Callable, Dict, Iterable, Mapping, Optional, Type, Union
from typing_extensions import dataclass_transform

from cached_property import cached_property
from typing_extensions import dataclass_transform

from spec_classes.methods import core as core_methods, SCALAR_METHODS, TOPLEVEL_METHODS
from spec_classes.methods import SCALAR_METHODS, TOPLEVEL_METHODS
from spec_classes.methods import core as core_methods
from spec_classes.utils.weakref_cache import WeakRefCache

from .types import Attr, MISSING
from .types import MISSING, Attr


@dataclass_transform()
Expand Down Expand Up @@ -265,7 +266,7 @@ def bootstrap(self, spec_cls: type):
annotation_namespace = {spec_cls.__name__: spec_cls, **vars(spec_cls)}
if hasattr(spec_cls, "ANNOTATION_TYPES"):
spec_annotation_types = spec_cls.ANNOTATION_TYPES
if hasattr(spec_annotation_types, "__call__"):
if callable(spec_annotation_types):
spec_annotation_types = spec_cls.ANNOTATION_TYPES()
if isinstance(spec_annotation_types, Mapping):
annotation_namespace.update(spec_annotation_types)
Expand Down
4 changes: 2 additions & 2 deletions spec_classes/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from .attr import Attr
from .attr_proxy import AttrProxy
from .keyed import KeyedList, KeyedSet
from .missing import MISSING, EMPTY, SENTINEL
from .spec_property import spec_property, classproperty
from .missing import EMPTY, MISSING, SENTINEL
from .spec_property import classproperty, spec_property
from .validated import ValidatedType, bounded, validated

__all__ = (
Expand Down
2 changes: 1 addition & 1 deletion spec_classes/types/attr.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import functools
import inspect
from collections.abc import MutableMapping, MutableSequence, MutableSet
from typing import Any, Callable, Iterable, Optional, TYPE_CHECKING, Type
from typing import TYPE_CHECKING, Any, Callable, Iterable, Optional, Type

from cached_property import cached_property

Expand Down
8 changes: 2 additions & 6 deletions spec_classes/types/keyed.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ def __spec_class_check_type__(cls, instance: Any, type_: Type) -> bool:
return True


class KeyedList(
Generic[ItemType, KeyType], MutableSequence, KeyedBase
): # pylint: disable=too-many-ancestors
class KeyedList(Generic[ItemType, KeyType], MutableSequence, KeyedBase): # pylint: disable=too-many-ancestors
"""
A list-like object that can also look up items by key. The computational
complexity for list-like operations is the same as the base `list` class,
Expand Down Expand Up @@ -221,9 +219,7 @@ def __repr__(self):
return f"{type_label(self._type)}({repr(self._list)})"


class KeyedSet(
Generic[ItemType, KeyType], MutableSet, KeyedBase
): # pylint: disable=too-many-ancestors
class KeyedSet(Generic[ItemType, KeyType], MutableSet, KeyedBase): # pylint: disable=too-many-ancestors
"""
A set-like object that can also look up items by key. The computational
complexity for set-like operations is the same as the base `set` class,
Expand Down
9 changes: 3 additions & 6 deletions spec_classes/types/spec_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,13 @@ def _qualified_name(self):
return f"{self.owner.__name__ if self.owner else ''}.{self.attr_name or ''}"

@abstractmethod
def __get__(self, obj, objtype=None):
... # pragma: no cover
def __get__(self, obj, objtype=None): ... # pragma: no cover

@abstractmethod
def __set__(self, obj, value):
... # pragma: no cover
def __set__(self, obj, value): ... # pragma: no cover

@abstractmethod
def __delete__(self, obj):
... # pragma: no cover
def __delete__(self, obj): ... # pragma: no cover


class spec_property(_spec_property_base):
Expand Down
3 changes: 1 addition & 2 deletions spec_classes/types/validated.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ def __new__(cls, *args, **kwargs):

@classmethod
@abstractmethod
def validate(cls, obj: Any) -> bool:
... # pragma: no cover
def validate(cls, obj: Any) -> bool: ... # pragma: no cover


def validated(
Expand Down
14 changes: 6 additions & 8 deletions spec_classes/utils/mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import copyreg
import functools
import inspect
import sys
from threading import RLock
from types import ModuleType
from typing import Any, Callable, Dict, Optional, Set, Type, Union

from lazy_object_proxy import Proxy

from spec_classes.errors import FrozenInstanceError
from spec_classes.types import Attr, MISSING
from spec_classes.types import MISSING, Attr

from .type_checking import check_type, type_label

Expand Down Expand Up @@ -351,7 +352,8 @@ def _get_function_args(function, attrs):
builtins, function.__name__, None
):
return set()
function = getattr(function, "__init__", function)
if not inspect.isfunction(function) and not inspect.ismethod(function):
function = getattr(function, "__init__", function)
if function is object.__init__:
return set()
# Otherwise, lookup signature and annotate function with args.
Expand All @@ -361,9 +363,7 @@ def _get_function_args(function, attrs):
except AttributeError:
try:
parameters = inspect.signature(function).parameters
except (
ValueError
): # pragma: no cover; Python 3.7 and newer raise a ValueError for C functions
except ValueError: # pragma: no cover; Python 3.7 and newer raise a ValueError for C functions
parameters = {}
if (
parameters
Expand All @@ -372,8 +372,6 @@ def _get_function_args(function, attrs):
return set(attrs or {})
try:
function.__spec_class_args__ = set(parameters)
except (
AttributeError
): # pragma: no cover; this is a *very* rare edge-case affecting functions defined in C.
except AttributeError: # pragma: no cover; this is a *very* rare edge-case affecting functions defined in C.
return set(parameters)
return function.__spec_class_args__
Loading

0 comments on commit d365136

Please sign in to comment.