Skip to content

gh-104656: Rename typeparams AST node to type_params #104657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Doc/library/ast.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1724,7 +1724,7 @@ Function and class definitions
body=[
FunctionDef(
name='f',
typeparams=[],
type_params=[],
args=arguments(
posonlyargs=[],
args=[
Expand Down Expand Up @@ -1848,7 +1848,7 @@ Function and class definitions
body=[
ClassDef(
name='Foo',
typeparams=[],
type_params=[],
bases=[
Name(id='base1', ctx=Load()),
Name(id='base2', ctx=Load())],
Expand Down Expand Up @@ -1887,7 +1887,7 @@ Async and await
body=[
AsyncFunctionDef(
name='f',
typeparams=[],
type_params=[],
args=arguments(
posonlyargs=[],
args=[],
Expand Down
8 changes: 4 additions & 4 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -640,12 +640,12 @@ type_alias[stmt_ty]:
# Type parameter declaration
# --------------------------

type_params[asdl_typeparam_seq*]: '[' t=type_param_seq ']' {
CHECK_VERSION(asdl_typeparam_seq *, 12, "Type parameter lists are", t) }
type_params[asdl_type_param_seq*]: '[' t=type_param_seq ']' {
CHECK_VERSION(asdl_type_param_seq *, 12, "Type parameter lists are", t) }

type_param_seq[asdl_typeparam_seq*]: a[asdl_typeparam_seq*]=','.type_param+ [','] { a }
type_param_seq[asdl_type_param_seq*]: a[asdl_type_param_seq*]=','.type_param+ [','] { a }

type_param[typeparam_ty] (memo):
type_param[type_param_ty] (memo):
| a=NAME b=[type_param_bound] { _PyAST_TypeVar(a->v.Name.id, b, EXTRA) }
| '*' a=NAME colon=":" e=expression {
RAISE_SYNTAX_ERROR_STARTING_FROM(colon, e->kind == Tuple_kind
Expand Down
53 changes: 27 additions & 26 deletions Include/internal/pycore_ast.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Include/internal/pycore_ast_state.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions Lib/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,7 @@ def visit_ClassDef(self, node):
self.fill("@")
self.traverse(deco)
self.fill("class " + node.name)
self._typeparams_helper(node.typeparams)
self._type_params_helper(node.type_params)
with self.delimit_if("(", ")", condition = node.bases or node.keywords):
comma = False
for e in node.bases:
Expand Down Expand Up @@ -1083,7 +1083,7 @@ def _function_helper(self, node, fill_suffix):
self.traverse(deco)
def_str = fill_suffix + " " + node.name
self.fill(def_str)
self._typeparams_helper(node.typeparams)
self._type_params_helper(node.type_params)
with self.delimit("(", ")"):
self.traverse(node.args)
if node.returns:
Expand All @@ -1092,10 +1092,10 @@ def _function_helper(self, node, fill_suffix):
with self.block(extra=self.get_type_comment(node)):
self._write_docstring_and_traverse_body(node)

def _typeparams_helper(self, typeparams):
if typeparams is not None and len(typeparams) > 0:
def _type_params_helper(self, type_params):
if type_params is not None and len(type_params) > 0:
with self.delimit("[", "]"):
self.interleave(lambda: self.write(", "), self.traverse, typeparams)
self.interleave(lambda: self.write(", "), self.traverse, type_params)

def visit_TypeVar(self, node):
self.write(node.name)
Expand All @@ -1112,7 +1112,7 @@ def visit_ParamSpec(self, node):
def visit_TypeAlias(self, node):
self.fill("type ")
self.traverse(node.name)
self._typeparams_helper(node.typeparams)
self._type_params_helper(node.type_params)
self.write(" = ")
self.traverse(node.value)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ attribute. This is implemented as a new AST node ``ast.TypeAlias``.

New syntax (``class X[T]: ...``, ``def func[T](): ...``) is added for defining
generic functions and classes. This is implemented as a new
``typeparams`` attribute on the AST nodes for classes and functions.
``type_params`` attribute on the AST nodes for classes and functions.
This node holds instances of the new AST classes ``ast.TypeVar``,
``ast.ParamSpec``, and ``ast.TypeVarTuple``.

Expand Down
16 changes: 8 additions & 8 deletions Parser/Python.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ module Python
| Expression(expr body)
| FunctionType(expr* argtypes, expr returns)

stmt = FunctionDef(identifier name, typeparam* typeparams, arguments args,
stmt = FunctionDef(identifier name, type_param* type_params, arguments args,
stmt* body, expr* decorator_list, expr? returns,
string? type_comment)
| AsyncFunctionDef(identifier name, typeparam* typeparams, arguments args,
| AsyncFunctionDef(identifier name, type_param* type_params, arguments args,
stmt* body, expr* decorator_list, expr? returns,
string? type_comment)

| ClassDef(identifier name,
typeparam* typeparams,
type_param* type_params,
expr* bases,
keyword* keywords,
stmt* body,
Expand All @@ -25,7 +25,7 @@ module Python

| Delete(expr* targets)
| Assign(expr* targets, expr value, string? type_comment)
| TypeAlias(expr name, typeparam* typeparams, expr value)
| TypeAlias(expr name, type_param* type_params, expr value)
| AugAssign(expr target, operator op, expr value)
-- 'simple' indicates that we annotate simple name without parens
| AnnAssign(expr target, expr annotation, expr? value, int simple)
Expand Down Expand Up @@ -145,8 +145,8 @@ module Python

type_ignore = TypeIgnore(int lineno, string tag)

typeparam = TypeVar(identifier name, expr? bound)
| ParamSpec(identifier name)
| TypeVarTuple(identifier name)
attributes (int lineno, int col_offset, int? end_lineno, int? end_col_offset)
type_param = TypeVar(identifier name, expr? bound)
| ParamSpec(identifier name)
| TypeVarTuple(identifier name)
attributes (int lineno, int col_offset, int? end_lineno, int? end_col_offset)
}
6 changes: 3 additions & 3 deletions Parser/action_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ _PyPegen_function_def_decorators(Parser *p, asdl_expr_seq *decorators, stmt_ty f
assert(function_def != NULL);
if (function_def->kind == AsyncFunctionDef_kind) {
return _PyAST_AsyncFunctionDef(
function_def->v.FunctionDef.name, function_def->v.FunctionDef.typeparams,
function_def->v.FunctionDef.name, function_def->v.FunctionDef.type_params,
function_def->v.FunctionDef.args,
function_def->v.FunctionDef.body, decorators, function_def->v.FunctionDef.returns,
function_def->v.FunctionDef.type_comment, function_def->lineno,
Expand All @@ -761,7 +761,7 @@ _PyPegen_function_def_decorators(Parser *p, asdl_expr_seq *decorators, stmt_ty f
}

return _PyAST_FunctionDef(
function_def->v.FunctionDef.name, function_def->v.FunctionDef.typeparams,
function_def->v.FunctionDef.name, function_def->v.FunctionDef.type_params,
function_def->v.FunctionDef.args,
function_def->v.FunctionDef.body, decorators,
function_def->v.FunctionDef.returns,
Expand All @@ -776,7 +776,7 @@ _PyPegen_class_def_decorators(Parser *p, asdl_expr_seq *decorators, stmt_ty clas
{
assert(class_def != NULL);
return _PyAST_ClassDef(
class_def->v.ClassDef.name, class_def->v.ClassDef.typeparams,
class_def->v.ClassDef.name, class_def->v.ClassDef.type_params,
class_def->v.ClassDef.bases, class_def->v.ClassDef.keywords,
class_def->v.ClassDef.body, decorators,
class_def->lineno, class_def->col_offset, class_def->end_lineno,
Expand Down
Loading