8000 Moved portions of earlier prototype into latest main CPython branch. … · python/cpython@9c8cd71 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9c8cd71

Browse files
committed
Moved portions of earlier prototype into latest main CPython branch. This commit includes:
1. Parser updates 2. AST updates 3. typing.py updates 4. Unit tests It does not include the following: 5. Symtable updates 6. Compiler updates 7. C implementations of TypeVar, TypeVarTuple, ParamSpec, Generic
1 parent 3516704 commit 9c8cd71

23 files changed

+3827
-1857
lines changed

Grammar/python.gram

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ simple_stmts[asdl_stmt_seq*]:
112112
# will throw a SyntaxError.
113113
simple_stmt[stmt_ty] (memo):
114114
| assignment
115+
| type_alias
115116
| e=star_expressions { _PyAST_Expr(e, EXTRA) }
116117
| &'return' return_stmt
117118
| &('import' | 'from') import_stmt
@@ -252,8 +253,8 @@ class_def[stmt_ty]:
252253

253254
class_def_raw[stmt_ty]:
254255
| invalid_class_def_raw
255-
| 'class' a=NAME b=['(' z=[arguments] ')' { z }] ':' c=block {
256-
_PyAST_ClassDef(a->v.Name.id,
256+
| 'class' a=NAME t=[type_params] b=['(' z=[arguments] ')' { z }] ':' c=block {
257+
_PyAST_ClassDef(a->v.Name.id, t,
257258
(b) ? ((expr_ty) b)->v.Call.args : NULL,
258259
(b) ? ((expr_ty) b)->v.Call.keywords : NULL,
259260
c, NULL, EXTRA) }
@@ -267,16 +268,16 @@ function_def[stmt_ty]:
267268

268269
function_def_raw[stmt_ty]:
269270
| invalid_def_raw
270-
| 'def' n=NAME &&'(' params=[params] ')' a=['->' z=expression { z }] &&':' tc=[func_type_comment] b=block {
271-
_PyAST_FunctionDef(n->v.Name.id,
271+
| 'def' n=NAME t=[type_params] &&'(' params=[params] ')' a=['->' z=expression { z }] &&':' tc=[func_type_comment] b=block {
272+
_PyAST_FunctionDef(n->v.Name.id, t,
272273
(params) ? params : CHECK(arguments_ty, _PyPegen_empty_arguments(p)),
273274
b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA) }
274-
| ASYNC 'def' n=NAME &&'(' params=[params] ')' a=['->' z=expression { z }] &&':' tc=[func_type_comment] b=block {
275+
| ASYNC 'def' n=NAME t=[type_params] &&'(' params=[params] ')' a=['->' z=expression { z }] &&':' tc=[func_type_comment] b=block {
275276
CHECK_VERSION(
276277
stmt_ty,
277278
5,
278279
"Async functions are",
279-
_PyAST_AsyncFunctionDef(n->v.Name.id,
280+
_PyAST_AsyncFunctionDef(n->v.Name.id, t,
280281
(params) ? params : CHECK(arguments_ty, _PyPegen_empty_arguments(p)),
281282
b, NULL, a, NEW_TYPE_COMMENT(p, tc), EXTRA)
282283
) }
@@ -628,6 +629,27 @@ keyword_patterns[asdl_seq*]:
628629
keyword_pattern[KeyPatternPair*]:
629630
| arg=NAME '=' value=pattern { _PyPegen_key_pattern_pair(p, arg, value) }
630631

632+
# Type statement
633+
# ---------------
634+
635+
type_alias[stmt_ty]:
636+
| "type" n=NAME t=[type_params] '=' b=expression {
637+
CHECK_VERSION(stmt_ty, 12, "Type statement is", _PyAST_TypeAlias(n->v.Name.id, t, b, EXTRA)) }
638+
639+
# Type parameter declaration
640+
# --------------------------
641+
642+
type_params[asdl_typeparam_seq*]: '[' t=type_param_seq ']' { t }
643+
644+
type_param_seq[asdl_typeparam_seq*]: a[asdl_typeparam_seq*]=','.type_param+ [','] { a }
645+
646+
type_param[typeparam_ty] (memo):
647+
| a=NAME b=[type_param_bound] { _PyAST_TypeVar(a->v.Name.id, b, EXTRA) }
648+
| '*' a=NAME { _PyAST_TypeVarTuple(a->v.Name.id, EXTRA) }
649+
| '**' a=NAME { _PyAST_ParamSpec(a->v.Name.id, EXTRA) }
650+
651+
type_param_bound[expr_ty]: ":" e=expression { e }
652+
631653
# EXPRESSIONS
632654
# -----------
633655

Include/cpython/funcobject.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ typedef struct {
4141
PyObject *func_weakreflist; /* List of weak references */
4242
PyObject *func_module; /* The __module__ attribute, can be anything */
4343
PyObject *func_annotations; /* Annotations, a dict or NULL */
44+
PyObject *func_typevars; /* Tuple of active type variables or NULL */
4445
vectorcallfunc vectorcall;
4546
/* Version number for use by specializer.
4647
* Can set to non-zero when we want to specialize.

Include/internal/pycore_ast.h

Lines changed: 76 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_ast_state.h

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_global_objects_fini_generated.h

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_global_strings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ struct _Py_global_strings {
210210
STRUCT_FOR_ID(__subclasshook__)
211211
STRUCT_FOR_ID(__truediv__)
212212
STRUCT_FOR_ID(__trunc__)
213+
STRUCT_FOR_ID(__type_variables__)
213214
STRUCT_FOR_ID(__typing_is_unpacked_typevartuple__)
214215
STRUCT_FOR_ID(__typing_prepare_subst__)
215216
STRUCT_FOR_ID(__typing_subst__)

Include/internal/pycore_runtime_init_generated.h

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_unicodeobject_generated.h

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/ast.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,7 @@ def visit_ClassDef(self, node):
10111011
self.fill("@")
10121012
self.traverse(deco)
10131013
self.fill("class " + node.name)
1014+
self._typeparams_helper(node.typeparams)
10141015
with self.delimit_if("(", ")", condition = node.bases or node.keywords):
10151016
comma = False
10161017
for e in node.bases:
@@ -1042,6 +1043,7 @@ def _function_helper(self, node, fill_suffix):
10421043
self.traverse(deco)
10431044
def_str = fill_suffix + " " + node.name
10441045
self.fill(def_str)
1046+
self._typeparams_helper(node.typeparams)
10451047
with self.delimit("(", ")"):
10461048
self.traverse(node.args)
10471049
if node.returns:
@@ -1050,6 +1052,29 @@ def _function_helper(self, node, fill_suffix):
10501052
with self.block(extra=self.get_type_comment(node)):
10511053
self._write_docstring_and_traverse_body(node)
10521054

1055+
def _typeparams_helper(self, typeparams):
1056+
if typeparams is not None and len(typeparams) > 0:
1057+
with self.delimit("[", "]"):
1058+
self.interleave(lambda: self.write(", "), self.traverse, typeparams)
1059+
1060+
def visit_TypeVar(self, node):
1061+
self.write(node.name)
1062+
if node.bound:
1063+
self.write(": ")
1064+
self.traverse(node.bound)
1065+
1066+
def visit_TypeVarTuple(self, node):
1067+
self.write("*" + node.name)
1068+
1069+
def visit_ParamSpec(self, node):
1070+
self.write("**" + node.name)
1071+
1072+
def visit_TypeAlias(self, node):
1073+
self.fill("type " + node.name)
1074+
self._typeparams_helper(node.typeparams)
1075+
self.write(" = ")
1076+
self.traverse(node.value)
1077+
10531078
def visit_For(self, node):
10541079
self._for_helper("for ", node)
10551080

Lib/keyword.py

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
0