Skip to content

Commit

Permalink
Merge pull request #434 from fschulze/ast-deprecations
Browse files Browse the repository at this point in the history
Fix ast deprecation warnings up to Python 3.13.
  • Loading branch information
malthe authored Dec 22, 2024
2 parents 1aae55f + b44fac4 commit 2b14af4
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 84 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ Changes

In next release ...

- Fix ``ast`` deprecation warnings up to Python 3.13.
(`#430 <https://github.com/malthe/chameleon/issues/430>`_)

- Fix ``load_module`` deprecation warnings for Python >= 3.10.

4.5.4 (2024-04-08)
Expand Down
2 changes: 1 addition & 1 deletion docs/library.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ You can write such a compiler as a closure:
def uppercase_expression(string):
def compiler(target, engine):
uppercased = self.string.uppercase()
value = ast.Str(uppercased)
value = ast.Constant(uppercased)
return [ast.Assign(targets=[target], value=value)]
return compiler
Expand Down
2 changes: 1 addition & 1 deletion src/chameleon/astutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def subscript(
) -> ast.Subscript:
return ast.Subscript(
value=value,
slice=ast.Index(value=ast.Str(s=name)),
slice=ast.Constant(name),
ctx=ctx,
)

Expand Down
51 changes: 36 additions & 15 deletions src/chameleon/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import builtins
import re
import sys
import textwrap
import types
from ast import AST
Expand All @@ -14,6 +15,8 @@
from ast import Module
from ast import NodeTransformer
from ast import alias
from ast import copy_location
from ast import fix_missing_locations
from ast import unparse
from typing import TYPE_CHECKING
from typing import Any
Expand Down Expand Up @@ -57,18 +60,34 @@ def wrapper(*vargs, **kwargs):
symbols.update(kwargs)

class Transformer(NodeTransformer):
def visit_FunctionDef(self, node) -> AST:
name = symbols.get(node.name, self)
if name is self:
def visit_FunctionDef(self, node: ast.FunctionDef) -> AST:
if node.name not in symbols:
return self.generic_visit(node)

return FunctionDef(
name=name,
args=node.args,
body=list(map(self.visit, node.body)),
decorator_list=getattr(node, "decorator_list", []),
lineno=None,
)
name = symbols[node.name]
assert isinstance(name, str)
body: list[ast.stmt] = [self.visit(stmt) for stmt in node.body]
if sys.version_info >= (3, 12):
# mypy complains if type_params is missing
funcdef = FunctionDef(
name=name,
args=node.args,
body=body,
decorator_list=node.decorator_list,
returns=node.returns,
type_params=node.type_params,
)
else:
funcdef = FunctionDef(
name=name,
args=node.args,
body=body,
decorator_list=node.decorator_list,
returns=node.returns,
)
copy_location(funcdef, node)
fix_missing_locations(funcdef)
return funcdef

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

for name, node in self.defines.items():
assignment = Assign(targets=[store(name)], value=node, lineno=None)
assignment = Assign(targets=[store(name)], value=node)
copy_location(assignment, node)
fix_missing_locations(assignment)
preamble.append(self.visit(assignment))

imports: list[AST] = []
imports: list[ast.stmt] = []
for value, node in self.imports.items():
stmt: AST
stmt: ast.stmt

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

imports.append(stmt)

return Module(imports + preamble + module.body, ())
return Module(imports + preamble + module.body, [])

def visit_Comment(self, node: Comment) -> AST:
self.comments.append(node.text)
Expand Down
Loading

0 comments on commit 2b14af4

Please sign in to comment.