Skip to content

Commit ffaed83

Browse files
author
Sylvain MARIE
committed
On the way to fixing #50 : created stubs for module entry_points_annotations.
1 parent 4c921e0 commit ffaed83

2 files changed

Lines changed: 140 additions & 119 deletions

File tree

valid8/entry_points_annotations.py

Lines changed: 3 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
except ImportError:
2424
from funcsigs import signature, Signature
2525

26-
from makefun import with_signature, wraps
26+
from makefun import wraps
2727

2828
from valid8.utils.decoration_tools import apply_on_each_func_args_sig
2929
from valid8.utils.typing_tools import is_pep484_nonable
@@ -93,25 +93,11 @@ def get_variable_str(self):
9393
return self.validator.validated_field_name + '=' + str(self.var_value)
9494

9595

96-
# Python 3+: load the 'more explicit api'
97-
if use_typing:
98-
new_sig = """(self,
99-
validated_func: Callable,
100-
*validation_func: ValidationFuncs,
101-
error_type: 'Type[ValidationError]' = None,
102-
help_msg: str = None,
103-
none_policy: int = None,
104-
**kw_context_args):"""
105-
else:
106-
new_sig = None
107-
108-
10996
class FuncValidator(Validator):
11097
"""
11198
Represents a special kind of `Validator` responsible to validate a function input or output
11299
"""
113100

114-
@with_signature(new_sig)
115101
def __init__(self,
116102
validated_func, # type: Callable
117103
*validation_func, # type: ValidationFuncs
@@ -162,7 +148,6 @@ class InputValidator(FuncValidator):
162148
Represents a special kind of `Validator` responsible to validate a function input.
163149
"""
164150

165-
@with_signature(new_sig)
166151
def __init__(self,
167152
validated_func, # type: Callable,
168153
*validation_func, # type: ValidationFuncs
@@ -203,7 +188,6 @@ def __init__(self,
203188
class OutputValidator(FuncValidator):
204189
""" Represents a special kind of `Validator` responsible to validate a function output. """
205190

206-
@with_signature(new_sig)
207191
def __init__(self,
208192
validated_func, # type: Callable
209193
*validation_func, # type: ValidationFuncs
@@ -260,27 +244,12 @@ def assert_valid(self,
260244
**kw_context_args)
261245

262246

263-
# Python 3+: load the 'more explicit api'
264-
if use_typing:
265-
new_sig = """(self,
266-
validated_class: Callable,
267-
validated_field_name: str,
268-
*validation_func: ValidationFuncs,
269-
error_type: 'Type[ClassFieldValidationError]' = None,
270-
help_msg: str = None,
271-
none_policy: int = None,
272-
**kw_context_args):"""
273-
else:
274-
new_sig = None
275-
276-
277247
class ClassFieldValidator(Validator):
278248
"""
279249
Represents a special kind of `Validator` responsible to validate a class field.
280250
As opposed to other validators, the name of the field is hardcoded.
281251
"""
282252

283-
@with_signature(new_sig)
284253
def __init__(self,
285254
validated_class, # type: Callable,
286255
validated_field_name, # type: str
@@ -341,21 +310,7 @@ def get_validated_class_display_name(self):
341310
return self.validated_class.__name__
342311

343312

344-
# Python 3+: load the 'more explicit api'
345-
if use_typing:
346-
new_sig = """(cls,
347-
field_name,
348-
*validation_func: ValidationFuncs,
349-
help_msg: str = None,
350-
error_type: 'Type[InputValidationError]' = None,
351-
none_policy: int = None,
352-
**kw_context_args) -> 'Type':"""
353-
else:
354-
new_sig = None
355-
356-
357313
@class_decorator(flat_mode_decorated_name='cls')
358-
@with_signature(new_sig)
359314
def validate_field(cls,
360315
field_name,
361316
*validation_func, # type: ValidationFuncs
@@ -439,21 +394,7 @@ def myfunc(a, b):
439394
return decorate_several_with_validation(f, none_policy=none_policy, _out_=_out_, **kw_validation_funcs)
440395

441396

442-
# Python 3+: load the 'more explicit api'
443-
if use_typing:
444-
new_sig = """(f,
445-
arg_name,
446-
*validation_func: ValidationFuncs,
447-
help_msg: str = None,
448-
error_type: 'Type[InputValidationError]' = None,
449-
none_policy: int = None,
450-
**kw_context_args) -> Callable:"""
451-
else:
452-
new_sig = None
453-
454-
455397
@function_decorator(flat_mode_decorated_name='f')
456-
@with_signature(new_sig)
457398
def validate_arg(f,
458399
arg_name,
459400
*validation_func, # type: ValidationFuncs
@@ -485,18 +426,6 @@ def validate_arg(f,
485426
return decorate_with_validation(f, arg_name, *validation_func, **kwargs)
486427

487428

488-
# Python 3+: load the 'more explicit api'
489-
if use_typing:
490-
new_sig = """(*validation_func: ValidationFuncs,
491-
help_msg: str = None,
492-
error_type: 'Type[OutputValidationError]' = None,
493-
none_policy: int = None,
494-
**kw_context_args) -> Callable:"""
495-
else:
496-
new_sig = None
497-
498-
499-
@with_signature(new_sig)
500429
def validate_out(*validation_func, # type: ValidationFuncs
501430
**kwargs):
502431
# type: (...) -> Callable
@@ -530,20 +459,6 @@ def decorate(f):
530459
""" The reserved key for output validation """
531460

532461

533-
# Python 3+: load the 'more explicit api'
534-
if use_typing:
535-
new_sig = """(cls,
536-
field_name: str,
537-
*validation_func: ValidationFuncs,
538-
help_msg: str = None,
539-
error_type: 'Union[Type[InputValidationError], Type[OutputValidationError]]' = None,
540-
none_policy: int = None,
541-
**kw_context_args) -> Callable:"""
542-
else:
543-
new_sig = None
544-
545-
546-
@with_signature(new_sig)
547462
def decorate_cls_with_validation(cls,
548463
field_name, # type: str
549464
*validation_func, # type: ValidationFuncs
@@ -721,21 +636,6 @@ def decorate_several_with_validation(func,
721636
return func
722637

723638

724-
# Python 3+: load the 'more explicit api'
725-
if use_typing:
726-
new_sig = """(func,
727-
arg_name: str,
728-
*validation_func: ValidationFuncs,
729-
help_msg: str = None,
730-
error_type: 'Union[Type[InputValidationError], Type[OutputValidationError]]' = None,
731-
none_policy: int = None,
732-
_constructor_of_cls_: 'Type'=None,
733-
**kw_context_args) -> Callable:"""
734-
else:
735-
new_sig = None
736-
737-
738-
@with_signature(new_sig)
739639
def decorate_with_validation(func,
740640
arg_name, # type: str
741641
*validation_func, # type: ValidationFuncs
@@ -826,23 +726,6 @@ class fields)"""
826726
pass
827727

828728

829-
# Python 3+: load the 'more explicit api'
830-
if use_typing:
831-
new_sig = """(validated_func: Callable,
832-
s: Signature,
833-
arg_name: str,
834-
*validation_func: ValidationFuncs,
835-
help_msg: str = None,
836-
error_type: 'Type[InputValidationError]' = None,
837-
none_policy: int = None,
838-
validated_class: 'Type'=None,
839-
validated_class_field_name: str=None,
840-
**kw_context_args):"""
841-
else:
842-
new_sig = None
843-
844-
845-
@with_signature(new_sig)
846729
def _create_function_validator(validated_func, # type: Callable
847730
s, # type: Signature
848731
arg_name, # type: str
@@ -895,10 +778,11 @@ def _create_function_validator(validated_func, # type: Callable
895778
error_type=error_type, help_msg=help_msg, **kw_context_args)
896779

897780

898-
def decorate_with_validators(func,
781+
def decorate_with_validators(func, # type: Callable
899782
func_signature=None, # type: Signature
900783
**validators # type: Union[Validator, List[Validator]]
901784
):
785+
# type: (...) -> Callable
902786
"""
903787
Utility method to decorate the provided function with the provided input and output Validator objects. Since this
904788
method takes Validator objects as argument, it is for advanced users.
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
from typing import Callable, List, Union, Type
2+
3+
try:
4+
from inspect import signature, Signature
5+
except ImportError:
6+
from funcsigs import signature, Signature
7+
8+
from valid8.composition import ValidationFuncs
9+
from valid8.entry_points import ValidationError, Validator
10+
11+
12+
class InputValidationError(ValidationError):
13+
...
14+
15+
16+
class OutputValidationError(ValidationError):
17+
...
18+
19+
20+
class ClassFieldValidationError(ValidationError):
21+
...
22+
23+
24+
class FuncValidator(Validator):
25+
def __init__(self,
26+
validated_func: Callable,
27+
*validation_func: ValidationFuncs,
28+
error_type: Type[ValidationError] = None,
29+
help_msg: str = None,
30+
none_policy: int = None,
31+
**kw_context_args):
32+
...
33+
34+
def get_validated_func_display_name(self):
35+
...
36+
37+
38+
class InputValidator(FuncValidator):
39+
...
40+
41+
42+
class OutputValidator(FuncValidator):
43+
...
44+
45+
46+
class ClassFieldValidator(Validator):
47+
...
48+
49+
def get_validated_class_display_name(self):
50+
...
51+
52+
53+
def validate_field(cls,
54+
field_name,
55+
*validation_func: ValidationFuncs,
56+
help_msg: str = None,
57+
error_type: Type[InputValidationError] = None,
58+
none_policy: int = None,
59+
**kw_context_args) -> Callable[[Type], Type]:
60+
...
61+
62+
63+
def validate_io(none_policy: int=None,
64+
_out_: ValidationFuncs=None,
65+
**kw_validation_funcs: ValidationFuncs
66+
) -> Callable[[Callable], Callable]:
67+
...
68+
69+
70+
def validate_arg(arg_name,
71+
*validation_func: ValidationFuncs,
72+
help_msg: str = None,
73+
error_type: Type[InputValidationError] = None,
74+
none_policy: int = None,
75+
**kw_context_args) -> Callable[[Callable], Callable]:
76+
...
77+
78+
79+
def validate_out(*validation_func: ValidationFuncs,
80+
help_msg: str = None,
81+
error_type: Type[OutputValidationError] = None,
82+
none_policy: int = None,
83+
**kw_context_args) -> Callable[[Callable], Callable]:
84+
...
85+
86+
87+
def decorate_cls_with_validation(cls,
88+
field_name: str,
89+
*validation_func: ValidationFuncs,
90+
help_msg: str = None,
91+
error_type: 'Union[Type[InputValidationError], Type[OutputValidationError]]' = None,
92+
none_policy: int = None,
93+
**kw_context_args) -> Callable:
94+
...
95+
96+
97+
def decorate_several_with_validation(func,
98+
_out_: ValidationFuncs = None,
99+
none_policy: int = None,
100+
**validation_funcs: ValidationFuncs
101+
) -> Callable:
102+
...
103+
104+
105+
def decorate_with_validation(func,
106+
arg_name: str,
107+
*validation_func: ValidationFuncs,
108+
help_msg: str = None,
109+
error_type: Union[Type[InputValidationError], Type[OutputValidationError]] = None,
110+
none_policy: int = None,
111+
_constructor_of_cls_: Type=None,
112+
**kw_context_args) -> Callable:
113+
...
114+
115+
116+
class InvalidNameError(ValueError):
117+
...
118+
119+
120+
def _create_function_validator(validated_func: Callable,
121+
s: Signature,
122+
arg_name: str,
123+
*validation_func: ValidationFuncs,
124+
help_msg: str = None,
125+
error_type: Type[InputValidationError] = None,
126+
none_policy: int = None,
127+
validated_class: Type=None,
128+
validated_class_field_name: str=None,
129+
**kw_context_args) -> Union[ClassFieldValidator, InputValidator, OutputValidator]:
130+
...
131+
132+
133+
def decorate_with_validators(func: Callable,
134+
func_signature: Signature = None,
135+
**validators: Union[Validator, List[Validator]]
136+
) -> Callable:
137+
...

0 commit comments

Comments
 (0)