Browse Source

Intermediate changes

robot-piglet 8 months ago
parent
commit
7a7d160e4e

+ 1 - 1
contrib/python/gast/.dist-info/METADATA

@@ -1,6 +1,6 @@
 Metadata-Version: 2.1
 Name: gast
-Version: 0.5.4
+Version: 0.5.5
 Summary: Python AST that abstracts the underlying Python version
 Home-page: https://github.com/serge-sans-paille/gast/
 Author: serge-sans-paille

+ 26 - 3
contrib/python/gast/README.rst

@@ -64,6 +64,9 @@ The AST used by GAST is the same as the one used in Python3.9, with the
 notable exception of ``ast.arg`` being replaced by an ``ast.Name`` with an
 ``ast.Param`` context.
 
+The ``name`` field of ``ExceptHandler`` is represented as an ``ast.Name`` with
+an ``ast.Store`` context and not a ``str``.
+
 For minor version before 3.9, please note that ``ExtSlice`` and ``Index`` are
 not used.
 
@@ -138,20 +141,22 @@ trade-offs to cope with legacy ASTs.
 
         stmt = FunctionDef(identifier name, arguments args,
                            stmt* body, expr* decorator_list, expr? returns,
-                           string? type_comment)
+                           string? type_comment, type_param* type_params)
               | AsyncFunctionDef(identifier name, arguments args,
                                  stmt* body, expr* decorator_list, expr? returns,
-                                 string? type_comment)
+                                 string? type_comment, type_param* type_params)
 
               | ClassDef(identifier name,
                  expr* bases,
                  keyword* keywords,
                  stmt* body,
-                 expr* decorator_list)
+                 expr* decorator_list,
+                 type_param* type_params)
               | Return(expr? value)
 
               | Delete(expr* targets)
               | Assign(expr* targets, expr value, string? type_comment)
+              | 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)
@@ -278,4 +283,22 @@ trade-offs to cope with legacy ASTs.
                  attributes (int lineno, int col_offset, int end_lineno, int end_col_offset)
 
         type_ignore = TypeIgnore(int lineno, string tag)
+
+         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)
     }
+
+
+Reporting Bugs
+--------------
+
+Bugs can be reported through `GitHub issues <https://github.com/serge-sans-paille/gast/issues>`_.
+
+Reporting Security Issues
+-------------------------
+
+If for some reason, you think your bug is security-related and should be subject
+to responsible disclosure, don't hesitate to `contact the maintainer
+<mailto:serge.guelton@telecom-bretagne.eu>`_ directly.

+ 2 - 0
contrib/python/gast/gast/ast2.py

@@ -22,6 +22,7 @@ class Ast2ToGAst(AstToGAst):
             self._visit(node.decorator_list),
             None,  # returns
             None,  # type_comment
+            [],  # type_params
         )
         gast.copy_location(new_node, node)
         new_node.end_lineno = new_node.end_col_offset = None
@@ -34,6 +35,7 @@ class Ast2ToGAst(AstToGAst):
             [],  # keywords
             self._visit(node.body),
             self._visit(node.decorator_list),
+            [],  # type_params
         )
 
         gast.copy_location(new_node, node)

+ 62 - 0
contrib/python/gast/gast/ast3.py

@@ -82,6 +82,7 @@ class Ast3ToGAst(AstToGAst):
                 self._visit(node.decorator_list),
                 self._visit(node.returns),
                 None,  # type_comment
+                [],  # type_params
             )
             return gast.copy_location(new_node, node)
 
@@ -93,6 +94,7 @@ class Ast3ToGAst(AstToGAst):
                 self._visit(node.decorator_list),
                 self._visit(node.returns),
                 None,  # type_comment
+                [],  # type_params
             )
             return gast.copy_location(new_node, node)
 
@@ -223,6 +225,31 @@ class Ast3ToGAst(AstToGAst):
             )
             return ast.copy_location(new_node, node)
 
+    if 8 <= sys.version_info.minor < 12:
+        def visit_FunctionDef(self, node):
+            new_node = gast.FunctionDef(
+                self._visit(node.name),
+                self._visit(node.args),
+                self._visit(node.body),
+                self._visit(node.decorator_list),
+                self._visit(node.returns),
+                self._visit(node.type_comment),
+                [],  # type_params
+            )
+            return gast.copy_location(new_node, node)
+
+        def visit_AsyncFunctionDef(self, node):
+            new_node = gast.AsyncFunctionDef(
+                self._visit(node.name),
+                self._visit(node.args),
+                self._visit(node.body),
+                self._visit(node.decorator_list),
+                self._visit(node.returns),
+                self._visit(node.type_comment),
+                [],  # type_params
+            )
+            return gast.copy_location(new_node, node)
+
 
 class GAstToAst3(GAstToAst):
     if sys.version_info.minor < 10:
@@ -423,6 +450,41 @@ class GAstToAst3(GAstToAst):
                 self._visit(node.keywords),
             )
             return ast.copy_location(new_node, node)
+    if  5 <= sys.version_info.minor < 12:
+        def visit_ClassDef(self, node):
+            new_node = ast.ClassDef(
+                self._visit(node.name),
+                self._visit(node.bases),
+                self._visit(node.keywords),
+                self._visit(node.body),
+                self._visit(node.decorator_list),
+            )
+            return ast.copy_location(new_node, node)
+
+    if  8 <= sys.version_info.minor < 12:
+        def visit_FunctionDef(self, node):
+            new_node = ast.FunctionDef(
+                self._visit(node.name),
+                self._visit(node.args),
+                self._visit(node.body),
+                self._visit(node.decorator_list),
+                self._visit(node.returns),
+                self._visit(node.type_comment),
+            )
+            return ast.copy_location(new_node, node)
+
+        def visit_AsyncFunctionDef(self, node):
+            new_node = ast.AsyncFunctionDef(
+                self._visit(node.name),
+                self._visit(node.args),
+                self._visit(node.body),
+                self._visit(node.decorator_list),
+                self._visit(node.returns),
+                self._visit(node.type_comment),
+            )
+            return ast.copy_location(new_node, node)
+
+
 
     def visit_arguments(self, node):
         extra_args = [self._make_arg(node.vararg),

+ 45 - 16
contrib/python/gast/gast/gast.py

@@ -17,20 +17,29 @@ except ImportError:
         pass
 
 
+try:
+    from ast import type_param
+except ImportError:
+    class type_param(AST):
+        pass
+
+
 def _make_node(Name, Fields, Attributes, Bases):
-    NBFields = len(Fields)
 
+    # This constructor is used a lot during conversion from ast to gast,
+    # then as the primary way to build ast nodes. So we tried to optimized it
+    # for speed and not for readability.
     def create_node(self, *args, **kwargs):
-        if args:
-            if len(args) + len([k for k in kwargs if k in Fields]) != NBFields:
-                raise TypeError(
-                    "{} constructor takes either 0 or {} mandatory arguments".
-                    format(Name, NBFields))
-            for argname, argval in zip(Fields, args):
-                setattr(self, argname, argval)
-        if kwargs:
-            for argname, argval in kwargs.items():
-                setattr(self, argname, argval)
+        if len(args) > len(Fields):
+            raise TypeError(
+                "{} constructor takes at most {} positional arguments".
+                format(Name, len(Fields)))
+
+        # it's faster to iterate rather than zipping or enumerate
+        for i in range(len(args)):
+            setattr(self, Fields[i], args[i])
+        if kwargs:  # cold branch
+            self.__dict__.update(kwargs)
 
     setattr(_sys.modules[__name__],
             Name,
@@ -51,16 +60,16 @@ _nodes = (
 
     # stmt
     ('FunctionDef', (('name', 'args', 'body', 'decorator_list', 'returns',
-                      'type_comment'),
+                      'type_comment', 'type_params'),
                      ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
                      (stmt,))),
-    ('AsyncFunctionDef', (('name', 'args', 'body',
-                           'decorator_list', 'returns',
-                           'type_comment'),
+    ('AsyncFunctionDef', (('name', 'args', 'body', 'decorator_list', 'returns',
+                           'type_comment', 'type_params',),
                           ('lineno', 'col_offset',
                            'end_lineno', 'end_col_offset',),
                           (stmt,))),
-    ('ClassDef', (('name', 'bases', 'keywords', 'body', 'decorator_list',),
+    ('ClassDef', (('name', 'bases', 'keywords', 'body', 'decorator_list',
+                   'type_params',),
                   ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
                   (stmt,))),
     ('Return', (('value',),
@@ -72,6 +81,9 @@ _nodes = (
     ('Assign', (('targets', 'value', 'type_comment'),
                 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
                 (stmt,))),
+    ('TypeAlias', (('name', 'type_params', 'value'),
+                  ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
+                  (stmt,))),
     ('AugAssign', (('target', 'op', 'value',),
                    ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
                    (stmt,))),
@@ -340,8 +352,25 @@ _nodes = (
 
     # type_ignore
     ('type_ignore', ((), ('lineno', 'tag'), (TypeIgnore,))),
+
+    # type_param
+    ('TypeVar', (('name', 'bound',),
+                 ('lineno', 'col_offset',
+                  'end_lineno', 'end_col_offset'),
+                 (type_param,))),
+    ('ParamSpec', (('name',),
+                 ('lineno', 'col_offset',
+                  'end_lineno', 'end_col_offset'),
+                 (type_param,))),
+    ('TypeVarTuple', (('name',),
+                 ('lineno', 'col_offset',
+                  'end_lineno', 'end_col_offset'),
+                 (type_param,))),
     )
 
+
+
+
 for name, descr in _nodes:
     _make_node(name, *descr)
 

+ 28 - 0
contrib/python/gast/gast/unparser.py

@@ -412,6 +412,8 @@ class _Unparser(NodeVisitor):
             self.fill("@")
             self.traverse(deco)
         self.fill("class " + node.name)
+        if hasattr(node, "type_params"):
+            self._type_params_helper(node.type_params)
         with self.delimit_if("(", ")", condition = node.bases or node.keywords):
             comma = False
             for e in node.bases:
@@ -443,6 +445,8 @@ class _Unparser(NodeVisitor):
             self.traverse(deco)
         def_str = fill_suffix + " " + node.name
         self.fill(def_str)
+        if hasattr(node, "type_params"):
+            self._type_params_helper(node.type_params)
         with self.delimit("(", ")"):
             self.traverse(node.args)
         if node.returns:
@@ -451,6 +455,30 @@ class _Unparser(NodeVisitor):
         with self.block(extra=self.get_type_comment(node)):
             self._write_docstring_and_traverse_body(node)
 
+    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, type_params)
+
+    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._type_params_helper(node.type_params)
+        self.write(" = ")
+        self.traverse(node.value)
+
     def visit_For(self, node):
         self._for_helper("for ", node)
 

+ 1 - 1
contrib/python/gast/gast/version.py

@@ -1 +1 @@
-__version__ = '0.5.4'
+__version__ = '0.5.5'

+ 1 - 1
contrib/python/gast/ya.make

@@ -2,7 +2,7 @@
 
 PY3_LIBRARY()
 
-VERSION(0.5.4)
+VERSION(0.5.5)
 
 LICENSE(BSD-3-Clause)