mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
gh-103763: Implement PEP 695 (#103764)
This implements PEP 695, Type Parameter Syntax. It adds support for: - Generic functions (def func[T](): ...) - Generic classes (class X[T](): ...) - Type aliases (type X = ...) - New scoping when the new syntax is used within a class body - Compiler and interpreter changes to support the new syntax and scoping rules Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Co-authored-by: Eric Traut <eric@traut.com> Co-authored-by: Larry Hastings <larry@hastings.org> Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
parent
fdafdc235e
commit
24d8b88420
56 changed files with 11405 additions and 5469 deletions
26
Lib/ast.py
26
Lib/ast.py
|
@ -1051,6 +1051,7 @@ class _Unparser(NodeVisitor):
|
|||
self.fill("@")
|
||||
self.traverse(deco)
|
||||
self.fill("class " + node.name)
|
||||
self._typeparams_helper(node.typeparams)
|
||||
with self.delimit_if("(", ")", condition = node.bases or node.keywords):
|
||||
comma = False
|
||||
for e in node.bases:
|
||||
|
@ -1082,6 +1083,7 @@ class _Unparser(NodeVisitor):
|
|||
self.traverse(deco)
|
||||
def_str = fill_suffix + " " + node.name
|
||||
self.fill(def_str)
|
||||
self._typeparams_helper(node.typeparams)
|
||||
with self.delimit("(", ")"):
|
||||
self.traverse(node.args)
|
||||
if node.returns:
|
||||
|
@ -1090,6 +1092,30 @@ class _Unparser(NodeVisitor):
|
|||
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:
|
||||
with self.delimit("[", "]"):
|
||||
self.interleave(lambda: self.write(", "), self.traverse, typeparams)
|
||||
|
||||
def visit_TypeVar(self, node):
|
||||
self.write(node.name)
|
||||
if node.bound:
|
||||
self.write(": ")
|
||||
self.traverse(node.bound)
|
||||
|
||||
def visit_TypeVarTuple(self, node):
|
||||
self.write("*" + node.name)
|
||||
|
||||
def visit_ParamSpec(self, node):
|
||||
self.write("**" + node.name)
|
||||
|
||||
def visit_TypeAlias(self, node):
|
||||
self.fill("type ")
|
||||
self.traverse(node.name)
|
||||
self._typeparams_helper(node.typeparams)
|
||||
self.write(" = ")
|
||||
self.traverse(node.value)
|
||||
|
||||
def visit_For(self, node):
|
||||
self._for_helper("for ", node)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue