Skip to content

Commit

Permalink
PEP 3107 - Function Annotations thanks to Tony Lownds
Browse files Browse the repository at this point in the history
  • Loading branch information
nnorwitz committed Dec 28, 2006
1 parent f6657e6 commit c150536
Show file tree
Hide file tree
Showing 32 changed files with 2,704 additions and 1,746 deletions.
21 changes: 14 additions & 7 deletions Grammar/Grammar
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@ eval_input: testlist NEWLINE* ENDMARKER

decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
decorators: decorator+
funcdef: [decorators] 'def' NAME parameters ':' suite
parameters: '(' [varargslist] ')'
varargslist: ((fpdef ['=' test] ',')*
('*' [NAME] (',' NAME ['=' test])* [',' '**' NAME] | '**' NAME) |
fpdef ['=' test] (',' fpdef ['=' test])* [','])
fpdef: NAME | '(' fplist ')'
fplist: fpdef (',' fpdef)* [',']
funcdef: [decorators] 'def' NAME parameters ['->' test] ':' suite
parameters: '(' [typedargslist] ')'
typedargslist: ((tfpdef ['=' test] ',')*
('*' [tname] (',' tname ['=' test])* [',' '**' tname] | '**' tname)
| tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
tname: NAME [':' test]
tfpdef: tname | '(' tfplist ')'
tfplist: tfpdef (',' tfpdef)* [',']
varargslist: ((vfpdef ['=' test] ',')*
('*' [vname] (',' vname ['=' test])* [',' '**' vname] | '**' vname)
| vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
vname: NAME
vfpdef: vname | '(' vfplist ')'
vfplist: vfpdef (',' vfpdef)* [',']

stmt: simple_stmt | compound_stmt
simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
Expand Down
32 changes: 28 additions & 4 deletions Include/Python-ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ typedef struct _excepthandler *excepthandler_ty;

typedef struct _arguments *arguments_ty;

typedef struct _arg *arg_ty;

typedef struct _keyword *keyword_ty;

typedef struct _alias *alias_ty;
Expand Down Expand Up @@ -74,6 +76,7 @@ struct _stmt {
arguments_ty args;
asdl_seq *body;
asdl_seq *decorators;
expr_ty returns;
} FunctionDef;

struct {
Expand Down Expand Up @@ -328,12 +331,30 @@ struct _excepthandler {
struct _arguments {
asdl_seq *args;
identifier vararg;
expr_ty varargannotation;
asdl_seq *kwonlyargs;
identifier kwarg;
expr_ty kwargannotation;
asdl_seq *defaults;
asdl_seq *kw_defaults;
};

enum _arg_kind {SimpleArg_kind=1, NestedArgs_kind=2};
struct _arg {
enum _arg_kind kind;
union {
struct {
identifier arg;
expr_ty annotation;
} SimpleArg;

struct {
asdl_seq *args;
} NestedArgs;

} v;
};

struct _keyword {
identifier arg;
expr_ty value;
Expand All @@ -350,8 +371,8 @@ mod_ty Interactive(asdl_seq * body, PyArena *arena);
mod_ty Expression(expr_ty body, PyArena *arena);
mod_ty Suite(asdl_seq * body, PyArena *arena);
stmt_ty FunctionDef(identifier name, arguments_ty args, asdl_seq * body,
asdl_seq * decorators, int lineno, int col_offset, PyArena
*arena);
asdl_seq * decorators, expr_ty returns, int lineno, int
col_offset, PyArena *arena);
stmt_ty ClassDef(identifier name, asdl_seq * bases, asdl_seq * body, int
lineno, int col_offset, PyArena *arena);
stmt_ty Return(expr_ty value, int lineno, int col_offset, PyArena *arena);
Expand Down Expand Up @@ -429,9 +450,12 @@ comprehension_ty comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs,
PyArena *arena);
excepthandler_ty excepthandler(expr_ty type, expr_ty name, asdl_seq * body, int
lineno, int col_offset, PyArena *arena);
arguments_ty arguments(asdl_seq * args, identifier vararg, asdl_seq *
kwonlyargs, identifier kwarg, asdl_seq * defaults,
arguments_ty arguments(asdl_seq * args, identifier vararg, expr_ty
varargannotation, asdl_seq * kwonlyargs, identifier
kwarg, expr_ty kwargannotation, asdl_seq * defaults,
asdl_seq * kw_defaults, PyArena *arena);
arg_ty SimpleArg(identifier arg, expr_ty annotation, PyArena *arena);
arg_ty NestedArgs(asdl_seq * args, PyArena *arena);
keyword_ty keyword(identifier arg, expr_ty value, PyArena *arena);
alias_ty alias(identifier name, identifier asname, PyArena *arena);

Expand Down
5 changes: 5 additions & 0 deletions Include/funcobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ typedef struct {
PyObject *func_dict; /* The __dict__ attribute, a dict or NULL */
PyObject *func_weakreflist; /* List of weak references */
PyObject *func_module; /* The __module__ attribute, can be anything */
PyObject *func_annotations; /* Annotations, a dict or NULL */

/* Invariant:
* func_closure contains the bindings for func_code->co_freevars, so
Expand All @@ -52,6 +53,8 @@ PyAPI_FUNC(PyObject *) PyFunction_GetKwDefaults(PyObject *);
PyAPI_FUNC(int) PyFunction_SetKwDefaults(PyObject *, PyObject *);
PyAPI_FUNC(PyObject *) PyFunction_GetClosure(PyObject *);
PyAPI_FUNC(int) PyFunction_SetClosure(PyObject *, PyObject *);
PyAPI_FUNC(PyObject *) PyFunction_GetAnnotations(PyObject *);
PyAPI_FUNC(int) PyFunction_SetAnnotations(PyObject *, PyObject *);

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

/* The classmethod and staticmethod types lives here, too */
PyAPI_DATA(PyTypeObject) PyClassMethod_Type;
Expand Down
157 changes: 81 additions & 76 deletions Include/graminit.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,79 +5,84 @@
#define decorators 260
#define funcdef 261
#define parameters 262
#define varargslist 263
#define fpdef 264
#define fplist 265
#define stmt 266
#define simple_stmt 267
#define small_stmt 268
#define expr_stmt 269
#define augassign 270
#define print_stmt 271
#define del_stmt 272
#define pass_stmt 273
#define flow_stmt 274
#define break_stmt 275
#define continue_stmt 276
#define return_stmt 277
#define yield_stmt 278
#define raise_stmt 279
#define import_stmt 280
#define import_name 281
#define import_from 282
#define import_as_name 283
#define dotted_as_name 284
#define import_as_names 285
#define dotted_as_names 286
#define dotted_name 287
#define global_stmt 288
#define assert_stmt 289
#define compound_stmt 290
#define if_stmt 291
#define while_stmt 292
#define for_stmt 293
#define try_stmt 294
#define with_stmt 295
#define with_var 296
#define except_clause 297
#define suite 298
#define testlist_safe 299
#define old_test 300
#define old_lambdef 301
#define test 302
#define or_test 303
#define and_test 304
#define not_test 305
#define comparison 306
#define comp_op 307
#define expr 308
#define xor_expr 309
#define and_expr 310
#define shift_expr 311
#define arith_expr 312
#define term 313
#define factor 314
#define power 315
#define atom 316
#define listmaker 317
#define testlist_gexp 318
#define lambdef 319
#define trailer 320
#define subscriptlist 321
#define subscript 322
#define sliceop 323
#define exprlist 324
#define testlist 325
#define dictsetmaker 326
#define classdef 327
#define arglist 328
#define argument 329
#define list_iter 330
#define list_for 331
#define list_if 332
#define gen_iter 333
#define gen_for 334
#define gen_if 335
#define testlist1 336
#define encoding_decl 337
#define yield_expr 338
#define typedargslist 263
#define tname 264
#define tfpdef 265
#define tfplist 266
#define varargslist 267
#define vname 268
#define vfpdef 269
#define vfplist 270
#define stmt 271
#define simple_stmt 272
#define small_stmt 273
#define expr_stmt 274
#define augassign 275
#define print_stmt 276
#define del_stmt 277
#define pass_stmt 278
#define flow_stmt 279
#define break_stmt 280
#define continue_stmt 281
#define return_stmt 282
#define yield_stmt 283
#define raise_stmt 284
#define import_stmt 285
#define import_name 286
#define import_from 287
#define import_as_name 288
#define dotted_as_name 289
#define import_as_names 290
#define dotted_as_names 291
#define dotted_name 292
#define global_stmt 293
#define assert_stmt 294
#define compound_stmt 295
#define if_stmt 296
#define while_stmt 297
#define for_stmt 298
#define try_stmt 299
#define with_stmt 300
#define with_var 301
#define except_clause 302
#define suite 303
#define testlist_safe 304
#define old_test 305
#define old_lambdef 306
#define test 307
#define or_test 308
#define and_test 309
#define not_test 310
#define comparison 311
#define comp_op 312
#define expr 313
#define xor_expr 314
#define and_expr 315
#define shift_expr 316
#define arith_expr 317
#define term 318
#define factor 319
#define power 320
#define atom 321
#define listmaker 322
#define testlist_gexp 323
#define lambdef 324
#define trailer 325
#define subscriptlist 326
#define subscript 327
#define sliceop 328
#define exprlist 329
#define testlist 330
#define dictsetmaker 331
#define classdef 332
#define arglist 333
#define argument 334
#define list_iter 335
#define list_for 336
#define list_if 337
#define gen_iter 338
#define gen_for 339
#define gen_if 340
#define testlist1 341
#define encoding_decl 342
#define yield_expr 343
7 changes: 4 additions & 3 deletions Include/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ extern "C" {
#define DOUBLESLASH 48
#define DOUBLESLASHEQUAL 49
#define AT 50
#define RARROW 51
/* Don't forget to update the table _PyParser_TokenNames in tokenizer.c! */
#define OP 51
#define ERRORTOKEN 52
#define N_TOKENS 53
#define OP 52
#define ERRORTOKEN 53
#define N_TOKENS 54

/* Special definitions for cooperation with parser */

Expand Down
Loading

0 comments on commit c150536

Please sign in to comment.