Skip to content
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 README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,9 @@ trade-offs to cope with legacy ASTs.

type_ignore = TypeIgnore(int lineno, string tag)

type_param = TypeVar(identifier name, expr? bound)
| ParamSpec(identifier name)
| TypeVarTuple(identifier name)
type_param = TypeVar(identifier name, expr? bound, expr? default_value)
| ParamSpec(identifier name, expr? default_value)
| TypeVarTuple(identifier name, expr? default_value)
attributes (int lineno, int col_offset, int end_lineno, int end_col_offset)
}

Expand Down
44 changes: 44 additions & 0 deletions gast/ast3.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,30 @@


class Ast3ToGAst(AstToGAst):
if sys.version_info.minor == 12:

def visit_TypeVar(self, node):
new_node = gast.TypeVar(
self._visit(node.name),
self._visit(node.bound),
None
)
return gast.copy_location(new_node, node)

def visit_TypeVarTuple(self, node):
new_node = gast.TypeVarTuple(
self._visit(node.name),
None
)
return gast.copy_location(new_node, node)

def visit_ParamSpec(self, node):
new_node = gast.ParamSpec(
self._visit(node.name),
None
)
return gast.copy_location(new_node, node)

if sys.version_info.minor < 10:

def visit_alias(self, node):
Expand Down Expand Up @@ -265,6 +289,26 @@ def visit_ClassDef(self, node):


class GAstToAst3(GAstToAst):
if sys.version_info.minor == 12:
def visit_TypeVar(self, node):
new_node = ast.TypeVar(
self._visit(node.name),
self._visit(node.bound)
)
return ast.copy_location(new_node, node)

def visit_TypeVarTuple(self, node):
new_node = ast.TypeVarTuple(
self._visit(node.name),
)
return ast.copy_location(new_node, node)

def visit_ParamSpec(self, node):
new_node = ast.ParamSpec(
self._visit(node.name),
)
return ast.copy_location(new_node, node)

if sys.version_info.minor < 10:
def visit_alias(self, node):
new_node = ast.alias(
Expand Down
12 changes: 6 additions & 6 deletions gast/gast.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,15 +366,15 @@ def _fill_field_types(Name, FieldTypes):
('type_ignore', ((), ('lineno', 'tag'), (TypeIgnore,))),

# type_param
('TypeVar', (('name', 'bound',),
('TypeVar', (('name', 'bound', 'default_value'),
('lineno', 'col_offset',
'end_lineno', 'end_col_offset'),
(type_param,))),
('ParamSpec', (('name',),
('ParamSpec', (('name', 'default_value'),
('lineno', 'col_offset',
'end_lineno', 'end_col_offset'),
(type_param,))),
('TypeVarTuple', (('name',),
('TypeVarTuple', (('name', 'default_value'),
('lineno', 'col_offset',
'end_lineno', 'end_col_offset'),
(type_param,))),
Expand Down Expand Up @@ -492,9 +492,9 @@ def _fill_field_types(Name, FieldTypes):
('MatchOr', (list[pattern],), ),

# type_param
('TypeVar', (str, expr | None), ),
('ParamSpec', (str,), ),
('TypeVarTuple', (str,), ),
('TypeVar', (str, expr | None, expr | None), ),
('ParamSpec', (str, expr | None), ),
('TypeVarTuple', (str, expr | None), ),
)

for _name, _types in _node_types:
Expand Down
12 changes: 7 additions & 5 deletions tests/test_py3_12.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ def test_type_alias(self):
def test_generic_type_alias(self):
code = "type Point[T] = tuple[T, float]"
tree = gast.parse(code)
compile(gast.gast_to_ast(tree), '<test>', 'exec')
atree = gast.gast_to_ast(tree)
compile(atree, '<test>', 'exec')
norm = ("Module(body=[TypeAlias(name=Name(id='Point', ctx=Store(), "
"annotation=None, type_comment=None), type_params=[TypeVar("
"name='T', bound=None)], value=Subscript(value=Name(id='tuple'"
"name='T', bound=None, default_value=None)], value=Subscript("
"value=Name(id='tuple'"
", ctx=Load(), annotation=None, type_comment=None), "
"slice=Tuple(elts=[Name(id='T', ctx=Load(), annotation=None, "
"type_comment=None), Name(id='float', ctx=Load(), "
Expand All @@ -51,7 +53,7 @@ def test_generic_function(self):
"kwarg=None, defaults=[]), body=[Expr(value=Constant(value="
"Ellipsis, kind=None))], decorator_list=[], returns=None, "
"type_comment=None, type_params=[TypeVar(name='T', "
"bound=None)])], type_ignores=[])")
"bound=None, default_value=None)])], type_ignores=[])")
self.assertEqual(dump(tree), norm)

def test_generic_class(self):
Expand All @@ -60,8 +62,8 @@ def test_generic_class(self):
compile(gast.gast_to_ast(tree), '<test>', 'exec')
norm = ("Module(body=[ClassDef(name='foo', bases=[], keywords=[], "
"body=[Expr(value=Constant(value=Ellipsis, kind=None))], "
"decorator_list=[], type_params=[TypeVar(name='T', bound=None)"
"])], type_ignores=[])")
"decorator_list=[], type_params=[TypeVar(name='T', bound=None, "
"default_value=None)])], type_ignores=[])")
self.assertEqual(dump(tree), norm)

if sys.version_info < (3, 12):
Expand Down
Loading