Skip to content

Commit 4eede70

Browse files
author
Sylvain MARIE
committed
Fixed ValidationError happening in all use cases. Fixed #25
1 parent d554949 commit 4eede70

File tree

5 files changed

+31
-32
lines changed

5 files changed

+31
-32
lines changed

autoclass/autoargs_.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import sys
2-
from collections import Sequence, OrderedDict
2+
from collections import OrderedDict
33

44
from makefun import wraps
5-
from valid8 import validate
65

76
try: # python 3+
87
from inspect import signature
@@ -16,7 +15,7 @@
1615

1716
from decopatch import function_decorator, DECORATED
1817

19-
from autoclass.utils import is_attr_selected
18+
from autoclass.utils import is_attr_selected, validate_include_exclude
2019

2120

2221
@function_decorator
@@ -76,10 +75,7 @@ def autoargs_decorate(func, # type: Callable
7675
"""
7776

7877
# (0) first check parameters
79-
if include is not None and exclude is not None:
80-
raise ValueError('Only one of \'include\' or \'exclude\' argument should be provided.')
81-
validate('include', include, instance_of=[str, Sequence], enforce_not_none=False)
82-
validate('exclude', exclude, instance_of=[str, Sequence], enforce_not_none=False)
78+
validate_include_exclude(include, exclude)
8379

8480
# (1) then retrieve function signature
8581
# attrs, varargs, varkw, defaults = getargspec(func)

autoclass/autodict_.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
from collections import Mapping, Sequence
1+
from collections import Mapping
22
from warnings import warn
33

44
from six import with_metaclass
55

6-
from valid8 import validate
7-
86
try: # python 3+
97
from inspect import signature
108
except ImportError:
@@ -21,7 +19,8 @@
2119
pass
2220

2321
from autoclass.autoprops_ import DuplicateOverrideError
24-
from autoclass.utils import is_attr_selected, method_already_there, possibly_replace_with_property_name
22+
from autoclass.utils import is_attr_selected, method_already_there, possibly_replace_with_property_name, \
23+
validate_include_exclude
2524
from autoclass.utils import get_constructor
2625
from autoclass.utils import _check_known_decorators
2726

@@ -123,11 +122,8 @@ def _execute_autodict_on_class(object_type, # type: Type[T]
123122
hidden
124123
:return:
125124
"""
126-
127-
if include is not None and exclude is not None:
128-
raise ValueError('Only one of \'include\' or \'exclude\' argument should be provided.')
129-
validate('include', include, instance_of=[str, Sequence], enforce_not_none=False)
130-
validate('exclude', exclude, instance_of=[str, Sequence], enforce_not_none=False)
125+
# 0. first check parameters
126+
validate_include_exclude(include, exclude)
131127

132128
# if issubclass(object_type, Mapping):
133129
# raise ValueError('@autodict can not be set on classes that are already subclasses of Mapping, and therefore '

autoclass/autohash_.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from collections import Sequence
21
try:
32
from inspect import signature
43
except ImportError:
@@ -15,10 +14,10 @@
1514
except ImportError:
1615
pass
1716

18-
from valid8 import validate
1917
from decopatch import class_decorator, DECORATED
2018

21-
from autoclass.utils import is_attr_selected, method_already_there, possibly_replace_with_property_name
19+
from autoclass.utils import is_attr_selected, method_already_there, possibly_replace_with_property_name, \
20+
validate_include_exclude
2221
from autoclass.utils import get_constructor
2322
from autoclass.utils import _check_known_decorators
2423

@@ -110,11 +109,8 @@ def _execute_autohash_on_class(object_type, # type: Type[T]
110109
not be taken into account in the hash. Please note that this behaviour is the opposite from @autodict.
111110
:return:
112111
"""
113-
114-
if include is not None and exclude is not None:
115-
raise ValueError('Only one of \'include\' or \'exclude\' argument should be provided.')
116-
validate('include', include, instance_of=[str, Sequence], enforce_not_none=False)
117-
validate('exclude', exclude, instance_of=[str, Sequence], enforce_not_none=False)
112+
# First check parameters
113+
validate_include_exclude(include, exclude)
118114

119115
# Override hash method if not already implemented
120116
if not method_already_there(object_type, '__hash__'):

autoclass/autoprops_.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from collections import Sequence
21
from copy import copy
32
from inspect import getmembers
43
from warnings import warn
@@ -21,9 +20,8 @@
2120
pass
2221

2322
from decopatch import DECORATED, function_decorator, class_decorator
24-
from valid8 import validate
2523

26-
from autoclass.utils import is_attr_selected
24+
from autoclass.utils import is_attr_selected, validate_include_exclude
2725
from autoclass.utils import get_constructor
2826
from autoclass.utils import _check_known_decorators
2927

@@ -113,11 +111,8 @@ def _execute_autoprops_on_class(object_type, # type: Type[T]
113111
:param exclude: a tuple of explicit attribute names to exclude. In such case, include should be None.
114112
:return:
115113
"""
116-
117-
if include is not None and exclude is not None:
118-
raise ValueError('Only one of \'include\' or \'exclude\' argument should be provided.')
119-
validate('include', include, instance_of=[str, Sequence], enforce_not_none=False)
120-
validate('exclude', exclude, instance_of=[str, Sequence], enforce_not_none=False)
114+
# 0. first check parameters
115+
validate_include_exclude(include, exclude)
121116

122117
# 1. Find the __init__ constructor signature and possible pycontracts @contract
123118
constructor = get_constructor(object_type, allow_inheritance=True)

autoclass/utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
1+
from collections import Sequence
2+
from valid8 import validate
3+
14
try: # python 3.5+
25
from typing import Union, Tuple
36
except ImportError:
47
pass
58

69

10+
def validate_include_exclude(include, exclude):
11+
"""
12+
Common validator for include and exclude arguments
13+
:param include:
14+
:param exclude:
15+
:return:
16+
"""
17+
if include is not None and exclude is not None:
18+
raise ValueError("Only one of 'include' or 'exclude' argument should be provided.")
19+
validate('include', include, instance_of=(str, Sequence), enforce_not_none=False)
20+
validate('exclude', exclude, instance_of=(str, Sequence), enforce_not_none=False)
21+
22+
723
def is_attr_selected(attr_name, # type: str
824
include=None, # type: Union[str, Tuple[str]]
925
exclude=None # type: Union[str, Tuple[str]]

0 commit comments

Comments
 (0)