Skip to content

Commit 9590f1e

Browse files
committed
Issue #3.
Changed version to 0.8
1 parent 8613696 commit 9590f1e

File tree

5 files changed

+58
-3
lines changed

5 files changed

+58
-3
lines changed

pyvalid/__accepts.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from inspect import getfullargspec as getargspec
99
from pyvalid.__exceptions import InvalidArgumentNumberError, \
1010
ArgumentValidationError
11+
from pyvalid.switch import is_enabled
1112

1213

1314
class Accepts(Callable):
@@ -23,7 +24,11 @@ def __init__(self, *accepted_arg_values, **accepted_kwargs_values):
2324
def __call__(self, func):
2425
@functools.wraps(func)
2526
def decorator_wrapper(*func_args, **func_kwargs):
26-
if self.accepted_arg_values or self.accepted_kwargs_values:
27+
perform_validation = all((
28+
is_enabled(),
29+
self.accepted_arg_values or self.accepted_kwargs_values
30+
))
31+
if perform_validation:
2732
# Forget all information about function arguments.
2833
self.accepted_args[:] = list()
2934
self.optional_args[:] = list()

pyvalid/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77
accepts = Accepts
88
returns = Returns
9-
version = '0.7'
9+
version = '0.8'
1010

1111

1212
__all__ = [
13+
'validators',
14+
'switch',
1315
'version',
1416
'accepts',
1517
'returns',

pyvalid/__returns.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from collections import Callable
22
from types import MethodType
33
from pyvalid.__exceptions import InvalidReturnType
4+
from pyvalid.switch import is_enabled
45

56

67
class Returns(Callable):
@@ -14,7 +15,7 @@ def __call__(self, func):
1415
def decorator_wrapper(*func_args, **func_kwargs):
1516
from pyvalid.validators import Validator
1617
returns_val = func(*func_args, **func_kwargs)
17-
if self.accepted_returns_values:
18+
if is_enabled() and self.accepted_returns_values:
1819
is_valid = False
1920
for accepted_val in self.accepted_returns_values:
2021
if isinstance(accepted_val, (Validator, MethodType)):

pyvalid/switch.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def turn_on():
2+
globals()['pyvalid_enabled'] = True
3+
4+
5+
def turn_off():
6+
globals()['pyvalid_enabled'] = False
7+
8+
9+
def is_enabled():
10+
if 'pyvalid_enabled' in globals():
11+
enabled = globals()['pyvalid_enabled']
12+
else:
13+
enabled = True
14+
return enabled

tests/test_switch.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import unittest
2+
import pyvalid
3+
4+
5+
class TestSwitch(unittest.TestCase):
6+
7+
def setUp(self):
8+
@pyvalid.returns(True)
9+
@pyvalid.accepts(True)
10+
def func1(val):
11+
return val
12+
self.func1 = func1
13+
14+
def test_default_state(self):
15+
# Validators must be enabled by default.
16+
self.assertTrue(pyvalid.switch.is_enabled())
17+
18+
def test_turn_off(self):
19+
pyvalid.switch.turn_off()
20+
val = self.func1(False)
21+
self.assertFalse(val)
22+
23+
def test_turn_on(self):
24+
pyvalid.switch.turn_on()
25+
val = self.func1(True)
26+
self.assertTrue(val)
27+
self.assertRaises(
28+
pyvalid.ArgumentValidationError, self.func1, False
29+
)
30+
31+
32+
if __name__ == '__main__':
33+
unittest.main()

0 commit comments

Comments
 (0)