14
14
# limitations under the License.
15
15
16
16
from robot .errors import DataError
17
- from robot .utils import (JYTHON , PY_VERSION , PY2 , is_string , split_from_equals ,
18
- unwrap )
17
+ from robot .utils import JYTHON , PY2 , is_string , split_from_equals
19
18
from robot .variables import is_assign
20
19
21
20
from .argumentspec import ArgumentSpec
22
21
22
+ # Move PythonArgumentParser to this module when Python 2 support is dropped.
23
23
if PY2 :
24
- from inspect import getargspec , ismethod
25
-
26
- def getfullargspec (func ):
27
- return getargspec (func ) + ([], None , {})
28
- else :
29
- from inspect import getfullargspec , ismethod
30
-
31
- if PY_VERSION >= (3 , 5 ):
32
- import typing
24
+ from .py2argumentparser import PythonArgumentParser
33
25
else :
34
- typing = None
26
+ from . py3argumentparser import PythonArgumentParser
35
27
36
28
if JYTHON :
37
29
from java .lang import Class
@@ -47,91 +39,6 @@ def parse(self, source, name=None):
47
39
raise NotImplementedError
48
40
49
41
50
- class PythonArgumentParser (_ArgumentParser ):
51
-
52
- def parse (self , handler , name = None ):
53
- args , varargs , kws , defaults , kwo , kwo_defaults , annotations \
54
- = self ._get_arg_spec (handler )
55
- if ismethod (handler ) or handler .__name__ == '__init__' :
56
- args = args [1 :] # Drop 'self'.
57
- spec = ArgumentSpec (
58
- name ,
59
- self ._type ,
60
- positional = args ,
61
- varargs = varargs ,
62
- kwargs = kws ,
63
- kwonlyargs = kwo ,
64
- defaults = self ._get_defaults (args , defaults , kwo_defaults )
65
- )
66
- spec .types = self ._get_types (handler , annotations , spec )
67
- return spec
68
-
69
- def _get_arg_spec (self , handler ):
70
- handler = unwrap (handler )
71
- try :
72
- return getfullargspec (handler )
73
- except TypeError : # Can occur w/ C functions (incl. many builtins).
74
- return [], 'args' , None , None , [], None , {}
75
-
76
- def _get_defaults (self , args , default_values , kwo_defaults ):
77
- if default_values :
78
- defaults = dict (zip (args [- len (default_values ):], default_values ))
79
- else :
80
- defaults = {}
81
- if kwo_defaults :
82
- defaults .update (kwo_defaults )
83
- return defaults
84
-
85
- def _get_types (self , handler , annotations , spec ):
86
- types = getattr (handler , 'robot_types' , ())
87
- if types is None :
88
- return None
89
- if types :
90
- return types
91
- return self ._get_type_hints (handler , annotations , spec )
92
-
93
- def _get_type_hints (self , handler , annotations , spec ):
94
- if not typing :
95
- return annotations
96
- try :
97
- type_hints = typing .get_type_hints (handler )
98
- except Exception : # Can raise pretty much anything
99
- return annotations
100
- self ._remove_mismatching_type_hints (type_hints , spec .argument_names )
101
- self ._remove_optional_none_type_hints (type_hints , spec .defaults )
102
- return type_hints
103
-
104
- def _remove_mismatching_type_hints (self , type_hints , argument_names ):
105
- # typing.get_type_hints returns info from the original function even
106
- # if it is decorated. Argument names are got from the wrapping
107
- # decorator and thus there is a mismatch that needs to be resolved.
108
- mismatch = set (type_hints ) - set (argument_names )
109
- for name in mismatch :
110
- type_hints .pop (name )
111
-
112
- def _remove_optional_none_type_hints (self , type_hints , defaults ):
113
- # If argument has None as a default, typing.get_type_hints adds
114
- # optional None to the information it returns. We don't want that.
115
- for arg in defaults :
116
- if defaults [arg ] is None and arg in type_hints :
117
- type_ = type_hints [arg ]
118
- if self ._is_union (type_ ):
119
- try :
120
- types = type_ .__args__
121
- except AttributeError :
122
- # Python 3.5.2's typing uses __union_params__ instead
123
- # of __args__. This block can likely be safely removed
124
- # when Python 3.5 support is dropped
125
- types = type_ .__union_params__
126
- if len (types ) == 2 and types [1 ] is type (None ):
127
- type_hints [arg ] = types [0 ]
128
-
129
- def _is_union (self , type_ ):
130
- if PY_VERSION >= (3 , 7 ) and hasattr (type_ , '__origin__' ):
131
- type_ = type_ .__origin__
132
- return isinstance (type_ , type (typing .Union ))
133
-
134
-
135
42
class JavaArgumentParser (_ArgumentParser ):
136
43
137
44
def parse (self , signatures , name = None ):
0 commit comments