Skip to content

Commit

Permalink
Add missing type_params attribute for ClassDef node before Python…
Browse files Browse the repository at this point in the history
…3.12 (#89)
  • Loading branch information
SigureMo authored Oct 23, 2024
1 parent 20113b4 commit b4d2df6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
13 changes: 13 additions & 0 deletions gast/ast3.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,19 @@ def visit_AsyncFunctionDef(self, node):
)
return gast.copy_location(new_node, node)

if sys.version_info.minor < 12:

def visit_ClassDef(self, node):
new_node = gast.ClassDef(
self._visit(node.name),
self._visit(node.bases),
self._visit(node.keywords),
self._visit(node.body),
self._visit(node.decorator_list),
[], # type_params
)
return gast.copy_location(new_node, node)


class GAstToAst3(GAstToAst):
if sys.version_info.minor < 10:
Expand Down
9 changes: 9 additions & 0 deletions tests/test_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,15 @@ def test_ExtSliceEllipsis(self):
"None, type_params=[])], type_ignores=[])")
self.assertEqual(dump(tree), norm)

def test_ClassDef(self):
code = 'class Foo: pass'
tree = gast.parse(code)
compile(gast.gast_to_ast(tree), '<test>', 'exec')
norm = ("Module(body=[ClassDef(name='Foo', bases=[], keywords=[], "
"body=[Pass()], decorator_list=[], type_params=[])], "
"type_ignores=[])")
self.assertEqual(dump(tree), norm)


if __name__ == '__main__':
unittest.main()

0 comments on commit b4d2df6

Please sign in to comment.