Skip to content

Commit ed2d611

Browse files
authored
Merge pull request #26 from erezsh/cleanup_apr17
Clean up based on Ruff
2 parents a667b5a + 3343283 commit ed2d611

File tree

9 files changed

+35
-19
lines changed

9 files changed

+35
-19
lines changed

pyproject.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,8 @@ minversion = "6.0"
3636
# addopts = "-ra -q"
3737
testpaths = [
3838
"tests",
39-
]
39+
]
40+
41+
[tool.ruff]
42+
line-length = 120
43+
target-version = "py311"

runtype/__init__.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
from .dataclass import dataclass
22
from .dispatch import DispatchError, MultiDispatch
3-
from .validation import PythonTyping, TypeSystem, TypeMismatchError, assert_isa, isa, issubclass, validate_func, is_subtype, cv_type_checking
3+
from .validation import (PythonTyping, TypeSystem, TypeMismatchError,
4+
assert_isa, isa, issubclass, validate_func, is_subtype, cv_type_checking)
45
from .pytypes import Constraint, String, Int
56

67
__version__ = "0.3.1"
7-
8+
__all__ = (
9+
'dataclass',
10+
'DispatchError', 'MultiDispatch',
11+
'PythonTyping', 'TypeSystem', 'TypeMismatchError',
12+
'assert_isa', 'isa', 'issubclass', 'validate_func', 'is_subtype', 'cv_type_checking',
13+
'Constraint', 'String', 'Int',
14+
'Dispatch',
15+
)
816

917
def Dispatch(typesystem: TypeSystem = PythonTyping()):
1018
"""Creates a decorator attached to a dispatch group,

runtype/dataclass.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from .utils import ForwardRef
1313
from .common import CHECK_TYPES
1414
from .validation import TypeMismatchError, ensure_isa as default_ensure_isa
15-
from .pytypes import TypeCaster, type_caster, SumType, NoneType
15+
from .pytypes import TypeCaster, SumType, NoneType
1616

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

392395
# See if we're being called as @dataclass or @dataclass().
393396
if cls is None:

runtype/dispatch.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ def get_arg_types(self, args):
6464
def find_function(self, args):
6565
nodes = [self.root]
6666
for i, a in enumerate(args):
67-
nodes = [n for node in nodes for n in node.follow_arg(a, self.typesystem, test_subtype=i in self.test_subtypes)]
67+
nodes = [n for node in nodes
68+
for n in node.follow_arg(a, self.typesystem, test_subtype=i in self.test_subtypes)]
6869

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

runtype/mypy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from typing import Callable, Optional, Type
1111

12-
from mypy.plugin import ClassDefContext, MethodContext, Plugin
12+
from mypy.plugin import ClassDefContext, Plugin
1313
from mypy.plugins import dataclasses
1414

1515
DATACLASS_PATHS = {"runtype.dataclass.dataclass"}

runtype/pytypes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
else:
2323
_orig_eval = ForwardRef._evaluate
2424

25-
def _forwardref_evaluate(self, g, l, _):
26-
return _orig_eval(self, g, l)
25+
def _forwardref_evaluate(self, glob, loc, _):
26+
return _orig_eval(self, glob, loc)
2727
else:
2828
_forwardref_evaluate = ForwardRef._evaluate
2929

@@ -189,7 +189,7 @@ def __le__(self, other):
189189
try:
190190
for v in self.values:
191191
other.validate_instance(v)
192-
except TypeMismatchError as e:
192+
except TypeMismatchError:
193193
return False
194194
return True
195195
return NotImplemented

runtype/typesystem.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11

22

33
class TypeSystem:
4-
def isinstance(self, obj, t):
4+
def isinstance(self, obj, t: type) -> bool:
55
return self.issubclass(self.get_type(obj), t)
66

7-
def issubclass(self, t1, t2):
7+
def issubclass(self, t1: type, t2: type) -> bool:
88
raise NotImplementedError()
99

10-
def canonize_type(self, t):
10+
def canonize_type(self, t: type) -> type:
1111
return t
1212

13-
def get_type(self, obj):
13+
def get_type(self, obj) -> type:
1414
raise NotImplementedError()
1515

1616
default_type: type = NotImplemented

runtype/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# python 3.6
88
from typing import _ForwardRef as ForwardRef
99
else:
10-
from typing import ForwardRef
10+
from typing import ForwardRef as ForwardRef
1111

1212
def get_func_signatures(typesystem, f):
1313
sig = inspect.signature(f)

runtype/validation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"User-facing API for validation"
22

3-
from typing import Any, Dict, List, Tuple, Set, FrozenSet, Callable
3+
from typing import Any, Dict, List, Tuple, Set, FrozenSet
44
from functools import wraps
55

66
from .common import CHECK_TYPES
@@ -38,7 +38,7 @@ def isa(obj, t):
3838
try:
3939
ensure_isa(obj, t)
4040
return True
41-
except TypeMismatchError as e:
41+
except TypeMismatchError:
4242
return False
4343

4444

0 commit comments

Comments
 (0)