Skip to content

Commit

Permalink
Issue #3.
Browse files Browse the repository at this point in the history
Changed version to 0.8
  • Loading branch information
eterey committed Sep 24, 2014
1 parent 8613696 commit 9590f1e
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 3 deletions.
7 changes: 6 additions & 1 deletion pyvalid/__accepts.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from inspect import getfullargspec as getargspec
from pyvalid.__exceptions import InvalidArgumentNumberError, \
ArgumentValidationError
from pyvalid.switch import is_enabled


class Accepts(Callable):
Expand All @@ -23,7 +24,11 @@ def __init__(self, *accepted_arg_values, **accepted_kwargs_values):
def __call__(self, func):
@functools.wraps(func)
def decorator_wrapper(*func_args, **func_kwargs):
if self.accepted_arg_values or self.accepted_kwargs_values:
perform_validation = all((
is_enabled(),
self.accepted_arg_values or self.accepted_kwargs_values
))
if perform_validation:
# Forget all information about function arguments.
self.accepted_args[:] = list()
self.optional_args[:] = list()
Expand Down
4 changes: 3 additions & 1 deletion pyvalid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

accepts = Accepts
returns = Returns
version = '0.7'
version = '0.8'


__all__ = [
'validators',
'switch',
'version',
'accepts',
'returns',
Expand Down
3 changes: 2 additions & 1 deletion pyvalid/__returns.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections import Callable
from types import MethodType
from pyvalid.__exceptions import InvalidReturnType
from pyvalid.switch import is_enabled


class Returns(Callable):
Expand All @@ -14,7 +15,7 @@ def __call__(self, func):
def decorator_wrapper(*func_args, **func_kwargs):
from pyvalid.validators import Validator
returns_val = func(*func_args, **func_kwargs)
if self.accepted_returns_values:
if is_enabled() and self.accepted_returns_values:
is_valid = False
for accepted_val in self.accepted_returns_values:
if isinstance(accepted_val, (Validator, MethodType)):
Expand Down
14 changes: 14 additions & 0 deletions pyvalid/switch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def turn_on():
globals()['pyvalid_enabled'] = True


def turn_off():
globals()['pyvalid_enabled'] = False


def is_enabled():
if 'pyvalid_enabled' in globals():
enabled = globals()['pyvalid_enabled']
else:
enabled = True
return enabled
33 changes: 33 additions & 0 deletions tests/test_switch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import unittest
import pyvalid


class TestSwitch(unittest.TestCase):

def setUp(self):
@pyvalid.returns(True)
@pyvalid.accepts(True)
def func1(val):
return val
self.func1 = func1

def test_default_state(self):
# Validators must be enabled by default.
self.assertTrue(pyvalid.switch.is_enabled())

def test_turn_off(self):
pyvalid.switch.turn_off()
val = self.func1(False)
self.assertFalse(val)

def test_turn_on(self):
pyvalid.switch.turn_on()
val = self.func1(True)
self.assertTrue(val)
self.assertRaises(
pyvalid.ArgumentValidationError, self.func1, False
)


if __name__ == '__main__':
unittest.main()

0 comments on commit 9590f1e

Please sign in to comment.