From af19470301f59e0032cb33cd34e97c6df124285e Mon Sep 17 00:00:00 2001 From: Erez Shinan Date: Mon, 17 Apr 2023 14:20:38 +0200 Subject: [PATCH] Clean up based on Ruff --- pyproject.toml | 6 +++++- runtype/__init__.py | 11 +++++++++-- runtype/dataclass.py | 11 +++++++---- runtype/dispatch.py | 3 ++- runtype/mypy.py | 2 +- runtype/pytypes.py | 6 +++--- runtype/typesystem.py | 8 ++++---- runtype/utils.py | 2 +- runtype/validation.py | 4 ++-- 9 files changed, 34 insertions(+), 19 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 48a25c7..81f516f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -36,4 +36,8 @@ minversion = "6.0" # addopts = "-ra -q" testpaths = [ "tests", -] \ No newline at end of file +] + +[tool.ruff] +line-length = 120 +target-version = "py311" \ No newline at end of file diff --git a/runtype/__init__.py b/runtype/__init__.py index 4ee058e..5e2e3dd 100644 --- a/runtype/__init__.py +++ b/runtype/__init__.py @@ -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, diff --git a/runtype/dataclass.py b/runtype/dataclass.py index b08ba0a..a87900a 100644 --- a/runtype/dataclass.py +++ b/runtype/dataclass.py @@ -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 @@ -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:** @@ -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 @@ -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: diff --git a/runtype/dispatch.py b/runtype/dispatch.py index 4511a81..146a957 100644 --- a/runtype/dispatch.py +++ b/runtype/dispatch.py @@ -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] diff --git a/runtype/mypy.py b/runtype/mypy.py index 977d1f0..6331b25 100644 --- a/runtype/mypy.py +++ b/runtype/mypy.py @@ -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"} diff --git a/runtype/pytypes.py b/runtype/pytypes.py index 9a10cf5..586ee96 100644 --- a/runtype/pytypes.py +++ b/runtype/pytypes.py @@ -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 @@ -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 diff --git a/runtype/typesystem.py b/runtype/typesystem.py index b36bc28..28d078e 100644 --- a/runtype/typesystem.py +++ b/runtype/typesystem.py @@ -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 diff --git a/runtype/utils.py b/runtype/utils.py index cca20d5..389b2c5 100644 --- a/runtype/utils.py +++ b/runtype/utils.py @@ -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) diff --git a/runtype/validation.py b/runtype/validation.py index a21755a..48fe936 100644 --- a/runtype/validation.py +++ b/runtype/validation.py @@ -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 @@ -38,7 +38,7 @@ def isa(obj, t): try: ensure_isa(obj, t) return True - except TypeMismatchError as e: + except TypeMismatchError: return False