Skip to content
Snippets Groups Projects
Commit 48e4c77a authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

r4

parent b30a8544
No related branches found
No related tags found
No related merge requests found
## v2.0.19
* Dodano `checked_coerce`
* Dodano `checked_coerce` i `for_argument`
## v2.0.16 = v2.0.17 == v2.0.18
......
......@@ -9,7 +9,7 @@ from .algos import merge_dicts
from .recast_exceptions import rethrow_as, silence_excs
from .typecheck import typed, Callable, Sequence, \
TypeVar, Mapping, Iterable, Any, Optional, CallSignature, \
Number, coerce, Set, Dict, List, Tuple, checked_coerce
Number, coerce, Set, Dict, List, Tuple, checked_coerce, for_argument
from .structures import TimeBasedHeap, Heap, typednamedtuple, OmniHashableMixin
__all__ = [
......@@ -19,7 +19,7 @@ __all__ = [
'typed', 'NewType', 'Callable', 'Sequence', 'coerce'
'TypeVar','Mapping', 'Iterable', 'Union', 'Any', 'Optional',
'CallSignature', 'Number',
'Set', 'Dict', 'List', 'Tuple', 'checked_coerce',
'Set', 'Dict', 'List', 'Tuple', 'checked_coerce', 'for_argument'
'rethrow_as', 'silence_excs'
]
......@@ -475,3 +475,14 @@ def checked_coerce(*t_args, **t_kwargs):
return inner
return outer
def for_argument(*t_ops, **t_kwops):
def outer(fun):
@functools.wraps(fun)
def inner(*args, **kwargs):
# add extra 'None' argument if unbound method
assert len(args) >= len(t_ops)
return fun(*((op or __NOP)(arg) for arg, op in six.moves.zip_longest(args, t_ops)),
**{k: t_kwops.get(k, __NOP)(v) for k, v in kwargs.items()})
return inner
return outer
[metadata]
name = satella
version = 2.0.19rc3
version = 2.0.19rc4
description-file = README.md
author = Piotr Maślanka
author_email = piotrm@smok.co
......
......@@ -6,7 +6,7 @@ import unittest
import six
from satella.coding import typed, CallSignature, Number, coerce, Optional, \
List, Dict, Tuple, Set, Callable, checked_coerce
List, Dict, Tuple, Set, Callable, checked_coerce, for_argument
class TestTypecheck(unittest.TestCase):
......@@ -36,6 +36,14 @@ class TestTypecheck(unittest.TestCase):
p(None)
self.assertRaises(TypeError, lambda: p(5.0))
def test_forarg(self):
@for_argument(int)
def testa(a):
return a
self.assertEqual(testa('5'), 5)
self.assertEqual(testa(5), 5)
self.assertIsInstance(testa('5'), int)
def test_checked_coerce(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment