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

recheck

parent 77e53ae7
No related branches found
No related tags found
No related merge requests found
...@@ -7,7 +7,7 @@ from __future__ import print_function, absolute_import, division ...@@ -7,7 +7,7 @@ from __future__ import print_function, absolute_import, division
from .concurrent import Monitor, RMonitor, CallableGroup from .concurrent import Monitor, RMonitor, CallableGroup
from .algos import merge_dicts from .algos import merge_dicts
from .recast_exceptions import rethrow_as, silence_excs from .recast_exceptions import rethrow_as, silence_excs
from .typecheck import typed, List, Tuple, Dict, Callable, Sequence, \ from .typecheck import typed, Callable, Sequence, \
TypeVar, Mapping, Iterable, Union, Any, Optional, CallSignature, \ TypeVar, Mapping, Iterable, Union, Any, Optional, CallSignature, \
Number, coerce Number, coerce
from .structures import TimeBasedHeap, Heap, typednamedtuple, OmniHashableMixin from .structures import TimeBasedHeap, Heap, typednamedtuple, OmniHashableMixin
...@@ -16,8 +16,9 @@ __all__ = [ ...@@ -16,8 +16,9 @@ __all__ = [
'typednamedtuple', 'OmniHashableMixin' 'typednamedtuple', 'OmniHashableMixin'
'TimeBasedHeap', 'Heap', 'CallableGroup', 'TimeBasedHeap', 'Heap', 'CallableGroup',
'Monitor', 'RMonitor', 'merge_dicts', 'Monitor', 'RMonitor', 'merge_dicts',
'typed', 'List', 'Tuple', 'Dict', 'NewType', 'Callable', 'Sequence', 'coerce' 'typed', 'NewType', 'Callable', 'Sequence', 'coerce'
'TypeVar', 'Generic', 'Mapping', 'Iterable', 'Union', 'Any', 'Optional', 'CallSignature', 'Number', 'TypeVar','Mapping', 'Iterable', 'Union', 'Any', 'Optional',
'CallSignature', 'Number',
'rethrow_as', 'silence_excs' 'rethrow_as', 'silence_excs'
] ]
...@@ -64,8 +64,6 @@ class Heap(object): ...@@ -64,8 +64,6 @@ class Heap(object):
Not thread-safe Not thread-safe
""" """
__slots__ = ('heap',) # this is rather private, plz
def __init__(self, from_list=()): def __init__(self, from_list=()):
self.heap = list(from_list) self.heap = list(from_list)
heapq.heapify(self.heap) heapq.heapify(self.heap)
...@@ -157,7 +155,7 @@ class Heap(object): ...@@ -157,7 +155,7 @@ class Heap(object):
return len(self.heap) return len(self.heap)
def __str__(self): def __str__(self):
return '<satella.coding.Heap: %s elements>' % (len(self.heap, )) return '<satella.coding.Heap: %s elements>' % (len(self, ))
def __unicode__(self): def __unicode__(self):
return six.text_type(str(self)) return six.text_type(str(self))
...@@ -207,7 +205,7 @@ class TimeBasedHeap(Heap): ...@@ -207,7 +205,7 @@ class TimeBasedHeap(Heap):
Initialize an empty heap Initialize an empty heap
""" """
self.default_clock_source = default_clock_source or time.time self.default_clock_source = default_clock_source or time.time
super(TimeBasedHeap, self).__init__() super(TimeBasedHeap, self).__init__(from_list=())
def put(self, *args): def put(self, *args):
""" """
......
...@@ -28,9 +28,6 @@ import itertools ...@@ -28,9 +28,6 @@ import itertools
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
List = typing.List
Tuple = typing.Tuple
Dict = typing.Dict
Callable = typing.Callable Callable = typing.Callable
Sequence = typing.Sequence Sequence = typing.Sequence
Number = numbers.Real Number = numbers.Real
...@@ -285,8 +282,6 @@ def __typeinfo_to_tuple_of_types(typeinfo, operator=type): ...@@ -285,8 +282,6 @@ def __typeinfo_to_tuple_of_types(typeinfo, operator=type):
return (typeinfo,) return (typeinfo,)
def istype(var, type_): def istype(var, type_):
print(var, type_)
if type_ is None or type_ == 'self': if type_ is None or type_ == 'self':
return True return True
...@@ -299,8 +294,10 @@ def istype(var, type_): ...@@ -299,8 +294,10 @@ def istype(var, type_):
except TypeError as e: # must be a typing.* annotation except TypeError as e: # must be a typing.* annotation
if type(type_) == type(typing.Union): if type(type_) == type(typing.Union):
return any(tuple(type_.__args__)) return istype(var, tuple(type_.__args__))
if type(type(type_)) == typing.Callable:
return '__call__' in var
try: try:
return all(hasattr(var, n) for n in { return all(hasattr(var, n) for n in {
typing.Iterable: ('__iter__',), typing.Iterable: ('__iter__',),
...@@ -313,7 +310,6 @@ def istype(var, type_): ...@@ -313,7 +310,6 @@ def istype(var, type_):
return type(var) == type_ return type(var) == type_
return False return False
...@@ -392,16 +388,17 @@ def typed(*t_args, **t_kwargs): ...@@ -392,16 +388,17 @@ def typed(*t_args, **t_kwargs):
def inner(*args, **kwargs): def inner(*args, **kwargs):
# add extra 'None' argument if unbound method # add extra 'None' argument if unbound method
for argument, typedescr in zip(args, t_args): for argument, typedescr in zip(args, t_args):
_do_if_not_type(argument, typedescr, lambda: \ if not istype(argument, typedescr):
TypeError('Got %s, expected %s' % ( raise TypeError('Got %s, expected %s' % (
type(argument), typedescr))) type(argument), typedescr))
rt = fun(*args, **kwargs) rt = fun(*args, **kwargs)
return _do_if_not_type(rt, t_retarg, lambda: if not istype(rt, t_retarg):
TypeError('Returned %s, expected %s' % ( raise TypeError('Returned %s, expected %s' % (
type(rt), t_retarg))) type(rt), t_retarg))
return rt
return inner return inner
return outer return outer
......
...@@ -5,7 +5,7 @@ import unittest ...@@ -5,7 +5,7 @@ import unittest
import six import six
from satella.coding import typed, CallSignature, Number, coerce from satella.coding import typed, CallSignature, Number, coerce, Optional
class TestTypecheck(unittest.TestCase): class TestTypecheck(unittest.TestCase):
...@@ -85,10 +85,10 @@ class TestTypecheck(unittest.TestCase): ...@@ -85,10 +85,10 @@ class TestTypecheck(unittest.TestCase):
testc(None) testc(None)
def test_t2(self): def test_t2(self):
@typed((int, None)) @typed(Optional[int])
def testa(a=5): def testa(a=5):
pass pass
self.assertRaises(TypeError, lambda: testa(2.0)) self.assertRaises(TypeError, lambda: testa(2.0))
testa(a=2.0) testa(a=2.0)
self.assertRaises(TypeError, lambda: testa('yuyu')) self.assertRaises(TypeError, lambda: testa('yuyu'))
...@@ -146,6 +146,17 @@ class TestTypecheck(unittest.TestCase): ...@@ -146,6 +146,17 @@ class TestTypecheck(unittest.TestCase):
testa(a=None) testa(a=None)
testa(a=6) testa(a=6)
def test_T2a(self):
@typed(Optional[int])
def testa(a=5):
pass
self.assertRaises(TypeError, lambda: testa(2.0))
testa(a=2.0)
self.assertRaises(TypeError, lambda: testa('yuyu'))
testa(a=None)
testa(a=6)
def test_t3(self): def test_t3(self):
def a(b, c): def a(b, c):
pass pass
......
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