Skip to content

Commit c150536

Browse files
committed
PEP 3107 - Function Annotations thanks to Tony Lownds
1 parent f6657e6 commit c150536

32 files changed

+2704
-1746
lines changed

Grammar/Grammar

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,20 @@ eval_input: testlist NEWLINE* ENDMARKER
2121

2222
decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
2323
decorators: decorator+
24-
funcdef: [decorators] 'def' NAME parameters ':' suite
25-
parameters: '(' [varargslist] ')'
26-
varargslist: ((fpdef ['=' test] ',')*
27-
('*' [NAME] (',' NAME ['=' test])* [',' '**' NAME] | '**' NAME) |
28-
fpdef ['=' test] (',' fpdef ['=' test])* [','])
29-
fpdef: NAME | '(' fplist ')'
30-
fplist: fpdef (',' fpdef)* [',']
24+
funcdef: [decorators] 'def' NAME parameters ['->' test] ':' suite
25+
parameters: '(' [typedargslist] ')'
26+
typedargslist: ((tfpdef ['=' test] ',')*
27+
('*' [tname] (',' tname ['=' test])* [',' '**' tname] | '**' tname)
28+
| tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
29+
tname: NAME [':' test]
30+
tfpdef: tname | '(' tfplist ')'
31+
tfplist: tfpdef (',' tfpdef)* [',']
32+
varargslist: ((vfpdef ['=' test] ',')*
33+
('*' [vname] (',' vname ['=' test])* [',' '**' vname] | '**' vname)
34+
| vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
35+
vname: NAME
36+
vfpdef: vname | '(' vfplist ')'
37+
vfplist: vfpdef (',' vfpdef)* [',']
3138

3239
stmt: simple_stmt | compound_stmt
3340
simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE

Include/Python-ast.h

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ typedef struct _excepthandler *excepthandler_ty;
3030

3131
typedef struct _arguments *arguments_ty;
3232

33+
typedef struct _arg *arg_ty;
34+
3335
typedef struct _keyword *keyword_ty;
3436

3537
typedef struct _alias *alias_ty;
@@ -74,6 +76,7 @@ struct _stmt {
7476
arguments_ty args;
7577
asdl_seq *body;
7678
asdl_seq *decorators;
79+
expr_ty returns;
7780
} FunctionDef;
7881

7982
struct {
@@ -328,12 +331,30 @@ struct _excepthandler {
328331
struct _arguments {
329332
asdl_seq *args;
330333
identifier vararg;
334+
expr_ty varargannotation;
331335
asdl_seq *kwonlyargs;
332336
identifier kwarg;
337+
expr_ty kwargannotation;
333338
asdl_seq *defaults;
334339
asdl_seq *kw_defaults;
335340
};
336341

342+
enum _arg_kind {SimpleArg_kind=1, NestedArgs_kind=2};
343+
struct _arg {
344+
enum _arg_kind kind;
345+
union {
346+
struct {
347+
identifier arg;
348+
expr_ty annotation;
349+
} SimpleArg;
350+
351+
struct {
352+
asdl_seq *args;
353+
} NestedArgs;
354+
355+
} v;
356+
};
357+
337358
struct _keyword {
338359
identifier arg;
339360
expr_ty value;
@@ -350,8 +371,8 @@ mod_ty Interactive(asdl_seq * body, PyArena *arena);
350371
mod_ty Expression(expr_ty body, PyArena *arena);
351372
mod_ty Suite(asdl_seq * body, PyArena *arena);
352373
stmt_ty FunctionDef(identifier name, arguments_ty args, asdl_seq * body,
353-
asdl_seq * decorators, int lineno, int col_offset, PyArena
354-
*arena);
374+
asdl_seq * decorators, expr_ty returns, int lineno, int
375+
col_offset, PyArena *arena);
355376
stmt_ty ClassDef(identifier name, asdl_seq * bases, asdl_seq * body, int
356377
lineno, int col_offset, PyArena *arena);
357378
stmt_ty Return(expr_ty value, int lineno, int col_offset, PyArena *arena);
@@ -429,9 +450,12 @@ comprehension_ty comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs,
429450
PyArena *arena);
430451
excepthandler_ty excepthandler(expr_ty type, expr_ty name, asdl_seq * body, int
431452
lineno, int col_offset, PyArena *arena);
432-
arguments_ty arguments(asdl_seq * args, identifier vararg, asdl_seq *
433-
kwonlyargs, identifier kwarg, asdl_seq * defaults,
453+
arguments_ty arguments(asdl_seq * args, identifier vararg, expr_ty
454+
varargannotation, asdl_seq * kwonlyargs, identifier
455+
kwarg, expr_ty kwargannotation, asdl_seq * defaults,
434456
asdl_seq * kw_defaults, PyArena *arena);
457+
arg_ty SimpleArg(identifier arg, expr_ty annotation, PyArena *arena);
458+
arg_ty NestedArgs(asdl_seq * args, PyArena *arena);
435459
keyword_ty keyword(identifier arg, expr_ty value, PyArena *arena);
436460
alias_ty alias(identifier name, identifier asname, PyArena *arena);
437461

Include/funcobject.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ typedef struct {
3030
PyObject *func_dict; /* The __dict__ attribute, a dict or NULL */
3131
PyObject *func_weakreflist; /* List of weak references */
3232
PyObject *func_module; /* The __module__ attribute, can be anything */
33+
PyObject *func_annotations; /* Annotations, a dict or NULL */
3334

3435
/* Invariant:
3536
* func_closure contains the bindings for func_code->co_freevars, so
@@ -52,6 +53,8 @@ PyAPI_FUNC(PyObject *) PyFunction_GetKwDefaults(PyObject *);
5253
PyAPI_FUNC(int) PyFunction_SetKwDefaults(PyObject *, PyObject *);
5354
PyAPI_FUNC(PyObject *) PyFunction_GetClosure(PyObject *);
5455
PyAPI_FUNC(int) PyFunction_SetClosure(PyObject *, PyObject *);
56+
PyAPI_FUNC(PyObject *) PyFunction_GetAnnotations(PyObject *);
57+
PyAPI_FUNC(int) PyFunction_SetAnnotations(PyObject *, PyObject *);
5558

5659
/* Macros for direct access to these values. Type checks are *not*
5760
done, so use with care. */
@@ -67,6 +70,8 @@ PyAPI_FUNC(int) PyFunction_SetClosure(PyObject *, PyObject *);
6770
(((PyFunctionObject *)func) -> func_kwdefaults)
6871
#define PyFunction_GET_CLOSURE(func) \
6972
(((PyFunctionObject *)func) -> func_closure)
73+
#define PyFunction_GET_ANNOTATIONS(func) \
74+
(((PyFunctionObject *)func) -> func_annotations)
7075

7176
/* The classmethod and staticmethod types lives here, too */
7277
PyAPI_DATA(PyTypeObject) PyClassMethod_Type;

Include/graminit.h

Lines changed: 81 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -5,79 +5,84 @@
55
#define decorators 260
66
#define funcdef 261
77
#define parameters 262
8-
#define varargslist 263
9-
#define fpdef 264
10-
#define fplist 265
11-
#define stmt 266
12-
#define simple_stmt 267
13-
#define small_stmt 268
14-
#define expr_stmt 269
15-
#define augassign 270
16-
#define print_stmt 271
17-
#define del_stmt 272
18-
#define pass_stmt 273
19-
#define flow_stmt 274
20-
#define break_stmt 275
21-
#define continue_stmt 276
22-
#define return_stmt 277
23-
#define yield_stmt 278
24-
#define raise_stmt 279
25-
#define import_stmt 280
26-
#define import_name 281
27-
#define import_from 282
28-
#define import_as_name 283
29-
#define dotted_as_name 284
30-
#define import_as_names 285
31-
#define dotted_as_names 286
32-
#define dotted_name 287
33-
#define global_stmt 288
34-
#define assert_stmt 289
35-
#define compound_stmt 290
36-
#define if_stmt 291
37-
#define while_stmt 292
38-
#define for_stmt 293
39-
#define try_stmt 294
40-
#define with_stmt 295
41-
#define with_var 296
42-
#define except_clause 297
43-
#define suite 298
44-
#define testlist_safe 299
45-
#define old_test 300
46-
#define old_lambdef 301
47-
#define test 302
48-
#define or_test 303
49-
#define and_test 304
50-
#define not_test 305
51-
#define comparison 306
52-
#define comp_op 307
53-
#define expr 308
54-
#define xor_expr 309
55-
#define and_expr 310
56-
#define shift_expr 311
57-
#define arith_expr 312
58-
#define term 313
59-
#define factor 314
60-
#define power 315
61-
#define atom 316
62-
#define listmaker 317
63-
#define testlist_gexp 318
64-
#define lambdef 319
65-
#define trailer 320
66-
#define subscriptlist 321
67-
#define subscript 322
68-
#define sliceop 323
69-
#define exprlist 324
70-
#define testlist 325
71-
#define dictsetmaker 326
72-
#define classdef 327
73-
#define arglist 328
74-
#define argument 329
75-
#define list_iter 330
76-
#define list_for 331
77-
#define list_if 332
78-
#define gen_iter 333
79-
#define gen_for 334
80-
#define gen_if 335
81-
#define testlist1 336
82-
#define encoding_decl 337
83-
#define yield_expr 338
8+
#define typedargslist 263
9+
#define tname 264
10+
#define tfpdef 265
11+
#define tfplist 266
12+
#define varargslist 267
13+
#define vname 268
14+
#define vfpdef 269
15+
#define vfplist 270
16+
#define stmt 271
17+
#define simple_stmt 272
18+
#define small_stmt 273
19+
#define expr_stmt 274
20+
#define augassign 275
21+
#define print_stmt 276
22+
#define del_stmt 277
23+
#define pass_stmt 278
24+
#define flow_stmt 279
25+
#define break_stmt 280
26+
#define continue_stmt 281
27+
#define return_stmt 282
28+
#define yield_stmt 283
29+
#define raise_stmt 284
30+
#define import_stmt 285
31+
#define import_name 286
32+
#define import_from 287
33+
#define import_as_name 288
34+
#define dotted_as_name 289
35+
#define import_as_names 290
36+
#define dotted_as_names 291
37+
#define dotted_name 292
38+
#define global_stmt 293
39+
#define assert_stmt 294
40+
#define compound_stmt 295
41+
#define if_stmt 296
42+
#define while_stmt 297
43+
#define for_stmt 298
44+
#define try_stmt 299
45+
#define with_stmt 300
46+
#define with_var 301
47+
#define except_clause 302
48+
#define suite 303
49+
#define testlist_safe 304
50+
#define old_test 305
51+
#define old_lambdef 306
52+
#define test 307
53+
#define or_test 308
54+
#define and_test 309
55+
#define not_test 310
56+
#define comparison 311
57+
#define comp_op 312
58+
#define expr 313
59+
#define xor_expr 314
60+
#define and_expr 315
61+
#define shift_expr 316
62+
#define arith_expr 317
63+
#define term 318
64+
#define factor 319
65+
#define power 320
66+
#define atom 321
67+
#define listmaker 322
68+
#define testlist_gexp 323
69+
#define lambdef 324
70+
#define trailer 325
71+
#define subscriptlist 326
72+
#define subscript 327
73+
#define sliceop 328
74+
#define exprlist 329
75+
#define testlist 330
76+
#define dictsetmaker 331
77+
#define classdef 332
78+
#define arglist 333
79+
#define argument 334
80+
#define list_iter 335
81+
#define list_for 336
82+
#define list_if 337
83+
#define gen_iter 338
84+
#define gen_for 339
85+
#define gen_if 340
86+
#define testlist1 341
87+
#define encoding_decl 342
88+
#define yield_expr 343

Include/token.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@ extern "C" {
5858
#define DOUBLESLASH 48
5959
#define DOUBLESLASHEQUAL 49
6060
#define AT 50
61+
#define RARROW 51
6162
/* Don't forget to update the table _PyParser_TokenNames in tokenizer.c! */
62-
#define OP 51
63-
#define ERRORTOKEN 52
64-
#define N_TOKENS 53
63+
#define OP 52
64+
#define ERRORTOKEN 53
65+
#define N_TOKENS 54
6566

6667
/* Special definitions for cooperation with parser */
6768

0 commit comments

Comments
 (0)