Skip to content

Commit

Permalink
Clean up based on Ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
erezsh committed Apr 17, 2023
1 parent a667b5a commit af19470
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 19 deletions.
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ minversion = "6.0"
# addopts = "-ra -q"
testpaths = [
"tests",
]
]

[tool.ruff]
line-length = 120
target-version = "py311"
11 changes: 9 additions & 2 deletions runtype/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
from .dataclass import dataclass
from .dispatch import DispatchError, MultiDispatch
from .validation import PythonTyping, TypeSystem, TypeMismatchError, assert_isa, isa, issubclass, validate_func, is_subtype, cv_type_checking
from .validation import (PythonTyping, TypeSystem, TypeMismatchError,
assert_isa, isa, issubclass, validate_func, is_subtype, cv_type_checking)
from .pytypes import Constraint, String, Int

__version__ = "0.3.1"

__all__ = (
dataclass,
DispatchError, MultiDispatch,
PythonTyping, TypeSystem, TypeMismatchError,
assert_isa, isa, issubclass, validate_func, is_subtype, cv_type_checking,
Constraint, String, Int
)

def Dispatch(typesystem: TypeSystem = PythonTyping()):
"""Creates a decorator attached to a dispatch group,
Expand Down
11 changes: 7 additions & 4 deletions runtype/dataclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .utils import ForwardRef
from .common import CHECK_TYPES
from .validation import TypeMismatchError, ensure_isa as default_ensure_isa
from .pytypes import TypeCaster, type_caster, SumType, NoneType
from .pytypes import TypeCaster, SumType, NoneType

Required = object()
MAX_SAMPLE_SIZE = 16
Expand Down Expand Up @@ -338,7 +338,8 @@ def __dataclass_transform__(
@__dataclass_transform__(eq_default=True, order_default=True)
def dataclass(cls=None, *, check_types: Union[bool, str] = CHECK_TYPES,
config: Configuration = PythonConfiguration(),
init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=True, slots=False) -> Any:
init=True, repr=True, eq=True, order=False,
unsafe_hash=False, frozen=True, slots=False) -> Any:
"""Runtype's dataclass is a drop-in replacement to Python's built-in dataclass, with added functionality.
**Differences from builtin dataclass:**
Expand All @@ -352,7 +353,8 @@ def dataclass(cls=None, *, check_types: Union[bool, str] = CHECK_TYPES,
- Adds convenience methods: replace(), aslist(), astuple(), and iterator for dict(this).
These methods won't override existing ones. They will be added only if the names aren't used.
- Setting the default as ``None`` automatically makes the type into ``Optional``, if it isn't already.
- Members without a default are allowed after members with a default (but they are required to create the instance)
- Members without a default are allowed after members with a default
(but they are required in order to create the instance)
3. Misc
- Frozen by default
Expand Down Expand Up @@ -387,7 +389,8 @@ def dataclass(cls=None, *, check_types: Union[bool, str] = CHECK_TYPES,
context_frame = inspect.currentframe().f_back # Get parent frame, to resolve forward-references
def wrap(cls):
return _process_class(cls, config, check_types, context_frame,
init=init, repr=repr, eq=eq, order=order, unsafe_hash=unsafe_hash, frozen=frozen, slots=slots)
init=init, repr=repr, eq=eq, order=order,
unsafe_hash=unsafe_hash, frozen=frozen, slots=slots)

# See if we're being called as @dataclass or @dataclass().
if cls is None:
Expand Down
3 changes: 2 additions & 1 deletion runtype/dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ def get_arg_types(self, args):
def find_function(self, args):
nodes = [self.root]
for i, a in enumerate(args):
nodes = [n for node in nodes for n in node.follow_arg(a, self.typesystem, test_subtype=i in self.test_subtypes)]
nodes = [n for node in nodes
for n in node.follow_arg(a, self.typesystem, test_subtype=i in self.test_subtypes)]

funcs = [node.func for node in nodes if node.func]

Expand Down
2 changes: 1 addition & 1 deletion runtype/mypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from typing import Callable, Optional, Type

from mypy.plugin import ClassDefContext, MethodContext, Plugin
from mypy.plugin import ClassDefContext, Plugin
from mypy.plugins import dataclasses

DATACLASS_PATHS = {"runtype.dataclass.dataclass"}
Expand Down
6 changes: 3 additions & 3 deletions runtype/pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
else:
_orig_eval = ForwardRef._evaluate

def _forwardref_evaluate(self, g, l, _):
return _orig_eval(self, g, l)
def _forwardref_evaluate(self, glob, loc, _):
return _orig_eval(self, glob, loc)
else:
_forwardref_evaluate = ForwardRef._evaluate

Expand Down Expand Up @@ -189,7 +189,7 @@ def __le__(self, other):
try:
for v in self.values:
other.validate_instance(v)
except TypeMismatchError as e:
except TypeMismatchError:
return False
return True
return NotImplemented
Expand Down
8 changes: 4 additions & 4 deletions runtype/typesystem.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@


class TypeSystem:
def isinstance(self, obj, t):
def isinstance(self, obj, t: type) -> bool:
return self.issubclass(self.get_type(obj), t)

def issubclass(self, t1, t2):
def issubclass(self, t1: type, t2: type) -> bool:
raise NotImplementedError()

def canonize_type(self, t):
def canonize_type(self, t: type) -> type:
return t

def get_type(self, obj):
def get_type(self, obj) -> type:
raise NotImplementedError()

default_type: type = NotImplemented
Expand Down
2 changes: 1 addition & 1 deletion runtype/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# python 3.6
from typing import _ForwardRef as ForwardRef
else:
from typing import ForwardRef
from typing import ForwardRef as ForwardRef

def get_func_signatures(typesystem, f):
sig = inspect.signature(f)
Expand Down
4 changes: 2 additions & 2 deletions runtype/validation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"User-facing API for validation"

from typing import Any, Dict, List, Tuple, Set, FrozenSet, Callable
from typing import Any, Dict, List, Tuple, Set, FrozenSet
from functools import wraps

from .common import CHECK_TYPES
Expand Down Expand Up @@ -38,7 +38,7 @@ def isa(obj, t):
try:
ensure_isa(obj, t)
return True
except TypeMismatchError as e:
except TypeMismatchError:
return False


Expand Down

0 comments on commit af19470

Please sign in to comment.