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

yo typecheck

parent 88c2f01c
No related branches found
No related tags found
No related merge requests found
# debug module
debug module is used during development. If Python's \_\_debug__ variable is set,
debug functions become operational.
If it's not (Python was launched with -O), they will do their best not to affect
performance, including removing themselves from code.
## Type checking
```python
from satella.coding.debug import typed
@typed(int, int)
def add_two_numbers(a, b):
return a+b
```
If you want to check for None-ness, you can pass None as well. Types for particular
arguments can also be tuples or lists, in that case if any of these types matches,
it's OK.
If type check fails, TypeError will be raised.
# coding=UTF-8
"""
It sounds like a melody
"""
from __future__ import print_function, absolute_import, division
import six
import unittest
from satella.coding.debug import typed
class TestTypecheck(unittest.TestCase):
def test_t1(self):
@typed(int, float, six.text_type)
def testf(a_int, a_float, a_string):
pass
self.assertRaises(TypeError, lambda: testf('lol', 15, 'test'))
self.assertRaises(TypeError, lambda: testf(12, 2.0, 'hey'))
testf(12, 2.0, u'hey')
@typed((None, int, float))
def testa(param):
pass
self.assertRaises(TypeError, lambda: testa('hey'))
testa(2)
testa(2)
testa(2.0)
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
from __future__ import print_function, absolute_import, division from __future__ import print_function, absolute_import, division
import six import six
import pickle
import unittest import unittest
from satella.instrumentation import Traceback from satella.instrumentation import Traceback
...@@ -10,10 +11,22 @@ class TestTraceback(unittest.TestCase): ...@@ -10,10 +11,22 @@ class TestTraceback(unittest.TestCase):
def test_tb(self): def test_tb(self):
try: try:
loc = 'hello world'
raise ValueError('hello') raise ValueError('hello')
except: except ValueError:
tb = Traceback() tb = Traceback()
p_fmt = tb.pretty_format() p_fmt = tb.pretty_format()
self.assertTrue(p_fmt) self.assertTrue(p_fmt)
\ No newline at end of file print(p_fmt)
def test_compression_happens(self):
try:
loc = ' ' * (10*1024*1024)
raise ValueError('hello')
except ValueError:
tb = Traceback()
self.assertLess(len(pickle.dumps(tb, -1)), 9*1024*1024)
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