Skip to content

Commit 1026ae1

Browse files
author
Philip Guo
committed
support Python 3 annotations and keyword-only arguments
1 parent 0fd95cb commit 1026ae1

5 files changed

Lines changed: 86 additions & 6 deletions

File tree

‎v3/pg_encoder.py‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,23 @@ def encode(self, dat):
158158
if k not in ('__module__', '__return__', '__locals__'):
159159
new_obj.append([self.encode(k), self.encode(v)])
160160
elif typ in (types.FunctionType, types.MethodType):
161-
# NB: In Python 3.0, getargspec is deprecated in favor of getfullargspec
162-
argspec = inspect.getargspec(dat)
161+
if is_python3:
162+
argspec = inspect.getfullargspec(dat)
163+
else:
164+
argspec = inspect.getargspec(dat)
163165

164166
printed_args = [e for e in argspec.args]
165167
if argspec.varargs:
166168
printed_args.append('*' + argspec.varargs)
167-
if argspec.keywords:
168-
printed_args.append('**' + argspec.keywords)
169+
170+
if is_python3:
171+
if argspec.varkw:
172+
printed_args.append('**' + argspec.varkw)
173+
if argspec.kwonlyargs:
174+
printed_args.extend(argspec.kwonlyargs)
175+
else:
176+
if argspec.keywords:
177+
printed_args.append('**' + argspec.keywords)
169178

170179
func_name = get_name(dat)
171180
pretty_name = func_name + '(' + ', '.join(printed_args) + ')'

‎v3/tests/backend-tests/py3_func_annotations.golden_py3‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,27 @@
1212
"event": "step_line"
1313
},
1414
{
15-
"exception_msg": "ValueError: Function has keyword-only arguments or annotations, use getfullargspec() API which can support them",
16-
"event": "uncaught_exception"
15+
"ordered_globals": [
16+
"remove_by_name"
17+
],
18+
"stdout": "",
19+
"func_name": "<module>",
20+
"stack_to_render": [],
21+
"globals": {
22+
"remove_by_name": [
23+
"REF",
24+
1
25+
]
26+
},
27+
"heap": {
28+
"1": [
29+
"FUNCTION",
30+
"remove_by_name(L, name)",
31+
null
32+
]
33+
},
34+
"line": 2,
35+
"event": "return"
1736
}
1837
]
1938
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"code": "# Python 3 keyword-only arguments\ndef my_sorted(iterable, *, reverse=False, key=None):\n pass\n",
3+
"trace": [
4+
{
5+
"exception_msg": "SyntaxError: invalid syntax (<string>, line 2)",
6+
"line": 2,
7+
"event": "uncaught_exception",
8+
"offset": 26
9+
}
10+
]
11+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"code": "# Python 3 keyword-only arguments\ndef my_sorted(iterable, *, reverse=False, key=None):\n pass\n",
3+
"trace": [
4+
{
5+
"ordered_globals": [],
6+
"stdout": "",
7+
"func_name": "<module>",
8+
"stack_to_render": [],
9+
"globals": {},
10+
"heap": {},
11+
"line": 2,
12+
"event": "step_line"
13+
},
14+
{
15+
"ordered_globals": [
16+
"my_sorted"
17+
],
18+
"stdout": "",
19+
"func_name": "<module>",
20+
"stack_to_render": [],
21+
"globals": {
22+
"my_sorted": [
23+
"REF",
24+
1
25+
]
26+
},
27+
"heap": {
28+
"1": [
29+
"FUNCTION",
30+
"my_sorted(iterable, reverse, key)",
31+
null
32+
]
33+
},
34+
"line": 2,
35+
"event": "return"
36+
}
37+
]
38+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python 3 keyword-only arguments
2+
def my_sorted(iterable, *, reverse=False, key=None):
3+
pass

0 commit comments

Comments
 (0)