Skip to content

Commit 2b14af4

Browse files
authored
Merge pull request #434 from fschulze/ast-deprecations
Fix ast deprecation warnings up to Python 3.13.
2 parents 1aae55f + b44fac4 commit 2b14af4

File tree

8 files changed

+124
-84
lines changed

8 files changed

+124
-84
lines changed

CHANGES.rst

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ Changes
33

44
In next release ...
55

6+
- Fix ``ast`` deprecation warnings up to Python 3.13.
7+
(`#430 <https://github.com/malthe/chameleon/issues/430>`_)
8+
69
- Fix ``load_module`` deprecation warnings for Python >= 3.10.
710

811
4.5.4 (2024-04-08)

docs/library.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ You can write such a compiler as a closure:
7272
def uppercase_expression(string):
7373
def compiler(target, engine):
7474
uppercased = self.string.uppercase()
75-
value = ast.Str(uppercased)
75+
value = ast.Constant(uppercased)
7676
return [ast.Assign(targets=[target], value=value)]
7777
return compiler
7878

src/chameleon/astutil.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def subscript(
4545
) -> ast.Subscript:
4646
return ast.Subscript(
4747
value=value,
48-
slice=ast.Index(value=ast.Str(s=name)),
48+
slice=ast.Constant(name),
4949
ctx=ctx,
5050
)
5151

src/chameleon/codegen.py

+36-15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import builtins
44
import re
5+
import sys
56
import textwrap
67
import types
78
from ast import AST
@@ -14,6 +15,8 @@
1415
from ast import Module
1516
from ast import NodeTransformer
1617
from ast import alias
18+
from ast import copy_location
19+
from ast import fix_missing_locations
1720
from ast import unparse
1821
from typing import TYPE_CHECKING
1922
from typing import Any
@@ -57,18 +60,34 @@ def wrapper(*vargs, **kwargs):
5760
symbols.update(kwargs)
5861

5962
class Transformer(NodeTransformer):
60-
def visit_FunctionDef(self, node) -> AST:
61-
name = symbols.get(node.name, self)
62-
if name is self:
63+
def visit_FunctionDef(self, node: ast.FunctionDef) -> AST:
64+
if node.name not in symbols:
6365
return self.generic_visit(node)
6466

65-
return FunctionDef(
66-
name=name,
67-
args=node.args,
68-
body=list(map(self.visit, node.body)),
69-
decorator_list=getattr(node, "decorator_list", []),
70-
lineno=None,
71-
)
67+
name = symbols[node.name]
68+
assert isinstance(name, str)
69+
body: list[ast.stmt] = [self.visit(stmt) for stmt in node.body]
70+
if sys.version_info >= (3, 12):
71+
# mypy complains if type_params is missing
72+
funcdef = FunctionDef(
73+
name=name,
74+
args=node.args,
75+
body=body,
76+
decorator_list=node.decorator_list,
77+
returns=node.returns,
78+
type_params=node.type_params,
79+
)
80+
else:
81+
funcdef = FunctionDef(
82+
name=name,
83+
args=node.args,
84+
body=body,
85+
decorator_list=node.decorator_list,
86+
returns=node.returns,
87+
)
88+
copy_location(funcdef, node)
89+
fix_missing_locations(funcdef)
90+
return funcdef
7291

7392
def visit_Name(self, node: ast.Name) -> AST:
7493
value = symbols.get(node.id, self)
@@ -170,15 +189,17 @@ def require(self, value: type[Any] | Hashable) -> ast.Name:
170189
def visit_Module(self, module: Module) -> AST:
171190
assert isinstance(module, Module)
172191
module = super().generic_visit(module) # type: ignore[assignment]
173-
preamble: list[AST] = []
192+
preamble: list[ast.stmt] = []
174193

175194
for name, node in self.defines.items():
176-
assignment = Assign(targets=[store(name)], value=node, lineno=None)
195+
assignment = Assign(targets=[store(name)], value=node)
196+
copy_location(assignment, node)
197+
fix_missing_locations(assignment)
177198
preamble.append(self.visit(assignment))
178199

179-
imports: list[AST] = []
200+
imports: list[ast.stmt] = []
180201
for value, node in self.imports.items():
181-
stmt: AST
202+
stmt: ast.stmt
182203

183204
if isinstance(value, types.ModuleType):
184205
stmt = Import(
@@ -198,7 +219,7 @@ def visit_Module(self, module: Module) -> AST:
198219

199220
imports.append(stmt)
200221

201-
return Module(imports + preamble + module.body, ())
222+
return Module(imports + preamble + module.body, [])
202223

203224
def visit_Comment(self, node: Comment) -> AST:
204225
self.comments.append(node.text)

0 commit comments

Comments
 (0)