codegen-2 2.16.7__tar.gz → 2.18.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: codegen-2
3
- Version: 2.16.7
3
+ Version: 2.18.1
4
4
  Summary: Meta-programming
5
5
  Author: Binh Vu
6
6
  Author-email: Binh Vu <binh@toan2.com>
@@ -2,6 +2,7 @@ import codegen.models.expr as expr
2
2
  import codegen.models.statement as stmt
3
3
  from codegen.models.ast import AST
4
4
  from codegen.models.expr import PredefinedFn
5
+ from codegen.models.imports import ImportArea
5
6
  from codegen.models.program import ImportHelper, Program
6
7
  from codegen.models.types import KEY, AstID, ImportFn
7
8
  from codegen.models.var import DeferredVar, Var, VarScope
@@ -17,6 +18,7 @@ __all__ = [
17
18
  "PredefinedFn",
18
19
  "AstID",
19
20
  "KEY",
21
+ "ImportArea",
20
22
  "ImportHelper",
21
23
  "ImportFn",
22
24
  ]
@@ -5,6 +5,7 @@ from dataclasses import dataclass, field
5
5
  from typing import TYPE_CHECKING, Any, Callable, Literal, Optional, Sequence
6
6
 
7
7
  from codegen.models.expr import ExceptionExpr, Expr
8
+ from codegen.models.imports import ImportArea
8
9
  from codegen.models.statement import (
9
10
  AssignStatement,
10
11
  BlockStatement,
@@ -77,6 +78,8 @@ class AST:
77
78
 
78
79
  def freeze(self):
79
80
  self._is_frozen = True
81
+ if isinstance(self.stmt, ImportArea):
82
+ self.stmt.freeze()
80
83
  for child in self.children:
81
84
  child.freeze()
82
85
 
@@ -98,8 +101,21 @@ class AST:
98
101
  child.move(source, target)
99
102
  return self
100
103
 
101
- def import_(self, module: str, is_import_attr: bool, alias: Optional[str] = None):
102
- self._add_stmt(ImportStatement(module, is_import_attr, alias))
104
+ def import_(
105
+ self,
106
+ module: str,
107
+ is_import_attr: bool,
108
+ alias: Optional[str] = None,
109
+ is_import_type: bool = False,
110
+ ):
111
+ self._add_stmt(
112
+ ImportStatement(
113
+ module,
114
+ is_import_attr=is_import_attr,
115
+ alias=alias,
116
+ is_import_type=is_import_type,
117
+ )
118
+ )
103
119
 
104
120
  def return_(self, expr: Expr):
105
121
  self._add_stmt(ReturnStatement(expr))
@@ -0,0 +1,139 @@
1
+ from __future__ import annotations
2
+
3
+ from dataclasses import dataclass, field
4
+
5
+ from codegen.models.statement import ImportStatement, Statement
6
+
7
+ ImportTarget = tuple[str, bool, str | None]
8
+ ImportKey = tuple[str, bool, str | None, bool]
9
+ LocalBindings = dict[str, tuple[tuple[str, bool], ImportTarget]]
10
+
11
+
12
+ @dataclass(eq=False)
13
+ class ImportArea(Statement):
14
+ """Ordered program-level imports and the indexes used to check them."""
15
+
16
+ imports: list[ImportKey] = field(default_factory=list, init=False)
17
+ import_types: dict[ImportTarget, bool] = field(
18
+ default_factory=dict, init=False, repr=False
19
+ )
20
+ local_bindings: LocalBindings = field(default_factory=dict, init=False, repr=False)
21
+ is_frozen: bool = field(default=False, init=False)
22
+
23
+ def freeze(self) -> None:
24
+ self.is_frozen = True
25
+
26
+ def has_import(
27
+ self,
28
+ module: str,
29
+ is_import_attr: bool,
30
+ alias: str | None = None,
31
+ is_import_type: bool = False,
32
+ ) -> bool:
33
+ """Check for an import, allowing a runtime import to satisfy a type-only request."""
34
+ target = self._normalize_import_target(module, is_import_attr, alias)
35
+ existing_type = self.import_types.get(target)
36
+ return existing_type is False or (is_import_type and existing_type is True)
37
+
38
+ def import_(
39
+ self,
40
+ module: str,
41
+ is_import_attr: bool,
42
+ alias: str | None = None,
43
+ is_import_type: bool = False,
44
+ ) -> None:
45
+ # A satisfied request leaves a frozen import area unchanged, just as in extend().
46
+ if self.has_import(module, is_import_attr, alias, is_import_type):
47
+ return
48
+ if self.is_frozen:
49
+ raise Exception("The AST is frozen and cannot be modified")
50
+
51
+ target = self._normalize_import_target(module, is_import_attr, alias)
52
+ entry = (*target, is_import_type)
53
+ self._add_binding(entry, self.local_bindings)
54
+
55
+ if self.import_types.get(target) is True:
56
+ # Place a promoted import at the point where it was requested at runtime.
57
+ self.imports.remove((*target, True))
58
+ self.imports.append(entry)
59
+ self.import_types[target] = is_import_type
60
+
61
+ def extend(self, *sources: ImportArea) -> None:
62
+ """Merge import areas in order after checking the entire batch."""
63
+ if not sources:
64
+ return
65
+ if self.is_frozen:
66
+ if any(
67
+ not self.has_import(*entry)
68
+ for source in sources
69
+ for entry in source.imports
70
+ ):
71
+ raise Exception("The AST is frozen and cannot be modified")
72
+ return
73
+
74
+ bindings = self.local_bindings.copy()
75
+ for source in sources:
76
+ for entry in source.imports:
77
+ self._add_binding(entry, bindings)
78
+
79
+ for source in sources:
80
+ for entry in source.imports:
81
+ self.import_(*entry)
82
+
83
+ def to_python(self) -> str:
84
+ future_lines: list[str] = []
85
+ runtime_lines: list[str] = []
86
+ type_lines: list[str] = []
87
+ for entry in self.imports:
88
+ stmt = ImportStatement(*entry)
89
+ if stmt.is_import_attr and stmt.module.rpartition(".")[0] == "__future__":
90
+ if stmt.is_import_type:
91
+ raise ValueError("Python future imports cannot be type-only")
92
+ future_lines.append(stmt.to_python())
93
+ elif stmt.is_import_type:
94
+ type_lines.append("\t" + stmt.to_python())
95
+ else:
96
+ runtime_lines.append(stmt.to_python())
97
+
98
+ if type_lines:
99
+ if not self.has_import("typing.TYPE_CHECKING", True):
100
+ runtime_lines.append("from typing import TYPE_CHECKING")
101
+ runtime_lines.append("if TYPE_CHECKING:")
102
+ runtime_lines.extend(type_lines)
103
+ return "\n".join(future_lines + runtime_lines)
104
+
105
+ def to_typescript(self) -> str:
106
+ return "\n".join(
107
+ ImportStatement(*entry).to_typescript() for entry in self.imports
108
+ )
109
+
110
+ @staticmethod
111
+ def _add_binding(entry: ImportKey, bindings: LocalBindings) -> None:
112
+ module, is_import_attr, alias, _ = entry
113
+ if is_import_attr and "." in module:
114
+ default_name = module.rsplit(".", 1)[1]
115
+ binding = (module, True)
116
+ else:
117
+ # Unaliased submodule imports bind their top-level package.
118
+ default_name = module.split(".", 1)[0]
119
+ binding = (module if alias is not None else default_name, False)
120
+
121
+ name = alias if alias is not None else default_name
122
+ if name == "*": # A wildcard does not declare an explicit local name.
123
+ return
124
+ target = (module, is_import_attr, alias)
125
+ previous_binding, previous_target = bindings.setdefault(name, (binding, target))
126
+ if previous_binding != binding:
127
+ previous = ImportStatement(*previous_target).to_python()
128
+ current = ImportStatement(*target).to_python()
129
+ raise ValueError(
130
+ f"Imports {previous!r} and {current!r} both bind local name {name!r}"
131
+ )
132
+
133
+ @staticmethod
134
+ def _normalize_import_target(
135
+ module: str, is_import_attr: bool, alias: str | None
136
+ ) -> ImportTarget:
137
+ # For plain imports, aliasing a dotted module binds the submodule itself.
138
+ source_name = module.rsplit(".", 1)[-1] if is_import_attr else module
139
+ return (module, is_import_attr, None if alias == source_name else alias)
@@ -6,7 +6,7 @@ from typing import Any, Optional
6
6
 
7
7
  from codegen.models.ast import AST
8
8
  from codegen.models.expr import ExprIdent # You may need to adjust this import path
9
- from codegen.models.statement import BlockStatement, IfStatement
9
+ from codegen.models.imports import ImportArea
10
10
  from codegen.models.types import KEY, AstID
11
11
  from codegen.models.var import Var, VarScope
12
12
 
@@ -15,19 +15,33 @@ from codegen.models.var import Var, VarScope
15
15
  class Program:
16
16
  root: AST
17
17
  vars: VarRegisters
18
- import_area: AST
19
- imported_modules: set[str]
18
+ import_area: ImportArea
20
19
 
21
20
  def __init__(self):
22
21
  self.vars = VarRegisters(self)
23
22
  self.root = AST.root(self)
24
- self.import_area = self.root._add_stmt(BlockStatement(has_owned_env=False))
25
- self.imported_modules = set()
23
+ self.import_area = ImportArea()
24
+ # Keep the import header at (0,) so existing body statement IDs stay stable.
25
+ self.root(self.import_area)
26
26
 
27
- def import_(self, module: str, is_import_attr: bool, alias: Optional[str] = None):
28
- if module not in self.imported_modules:
29
- self.import_area.import_(module, is_import_attr, alias)
30
- self.imported_modules.add(module)
27
+ def import_(
28
+ self,
29
+ module: str,
30
+ is_import_attr: bool,
31
+ alias: Optional[str] = None,
32
+ is_import_type: bool = False,
33
+ ) -> None:
34
+ self.import_area.import_(module, is_import_attr, alias, is_import_type)
35
+
36
+ def has_import(
37
+ self,
38
+ module: str,
39
+ is_import_attr: bool,
40
+ alias: Optional[str] = None,
41
+ is_import_type: bool = False,
42
+ ) -> bool:
43
+ """Check for an import, allowing a runtime import to satisfy a type-only request."""
44
+ return self.import_area.has_import(module, is_import_attr, alias, is_import_type)
31
45
 
32
46
  def get_ast_by_id(self, id: AstID) -> AST:
33
47
  ast = self.root
@@ -92,6 +106,11 @@ class Program:
92
106
 
93
107
  def extend(self, *others: Program) -> None:
94
108
  """Append other programs to this program."""
109
+ if self.root._is_frozen:
110
+ raise Exception("The AST is frozen and cannot be modified")
111
+
112
+ self.import_area.extend(*(source.import_area for source in others))
113
+
95
114
  for source in others:
96
115
  if source is self:
97
116
  # Snapshot self-extension so we don't iterate source lists while appending to them.
@@ -112,20 +131,14 @@ class Program:
112
131
  type=copy.deepcopy(var.type),
113
132
  )
114
133
 
115
- self._copy_children(source.import_area, self.import_area, var_memo)
116
134
  for child in source.root.children:
117
- if child is source.import_area:
135
+ if child.stmt is source.import_area:
118
136
  continue
119
137
  self.root.children.append(
120
- child.clone(self, var_memo).move(child.id, self.root.next_child_id())
138
+ child.clone(self, var_memo).move(
139
+ child.id, self.root.next_child_id()
140
+ )
121
141
  )
122
- self.imported_modules.update(source.imported_modules)
123
-
124
- def _copy_children(self, source_parent: AST, dest_parent: AST, var_memo: Optional[dict[int, Any]] = None):
125
- for child in source_parent.children:
126
- dest_parent.children.append(
127
- child.clone(self, var_memo).move(source_parent.id, dest_parent.next_child_id())
128
- )
129
142
 
130
143
  @classmethod
131
144
  def _collect_program_vars(cls, program: Program) -> list[Var]:
@@ -135,7 +148,8 @@ class Program:
135
148
 
136
149
  @classmethod
137
150
  def _collect_ast_vars(cls, ast: AST, vars: list[Var]):
138
- cls._collect_vars(ast.stmt, vars)
151
+ if not isinstance(ast.stmt, ImportArea):
152
+ cls._collect_vars(ast.stmt, vars)
139
153
  for child in ast.children:
140
154
  cls._collect_ast_vars(child, vars)
141
155
 
@@ -187,12 +201,15 @@ class VarRegisters:
187
201
  new_scope = VarScope(
188
202
  ast=(),
189
203
  child_index_start=reg.scope.child_index_start + body_child_offset,
190
- child_index_end=(reg.scope.child_index_end + body_child_offset) if reg.scope.child_index_end is not None else None
204
+ child_index_end=(
205
+ (reg.scope.child_index_end + body_child_offset)
206
+ if reg.scope.child_index_end is not None
207
+ else None
208
+ ),
191
209
  )
192
210
  else:
193
211
  new_scope = reg.scope.move(
194
- (reg.scope.ast[0],),
195
- (reg.scope.ast[0] + body_child_offset,)
212
+ (reg.scope.ast[0],), (reg.scope.ast[0] + body_child_offset,)
196
213
  )
197
214
 
198
215
  new_reg = VarRegister(
@@ -280,27 +297,7 @@ class ImportHelper:
280
297
  # mapping from identifiers to their imported paths
281
298
  idents: dict[str, str] = field(default_factory=dict)
282
299
 
283
- def use(self, ident: str):
300
+ def use(self, ident: str, is_import_type: bool = False):
284
301
  assert ident in self.idents, ident
285
- self.program.import_(self.idents[ident], True)
302
+ self.program.import_(self.idents[ident], True, is_import_type=is_import_type)
286
303
  return ExprIdent(ident)
287
-
288
- def python_import_for_hint(
289
- self, module: str, is_import_attr: bool, alias: Optional[str] = None
290
- ):
291
- """A specific function only for Python to import identifiers that causing circular import error into a same TYPE_CHECKING condition"""
292
- if module in self.program.imported_modules:
293
- return
294
- self.program.imported_modules.add(module)
295
-
296
- self.program.import_("typing.TYPE_CHECKING", True)
297
- for child in self.program.import_area.children:
298
- if isinstance(child.stmt, IfStatement) and child.stmt.cond == ExprIdent(
299
- "TYPE_CHECKING"
300
- ):
301
- child.import_(module, is_import_attr, alias)
302
- break
303
- else:
304
- self.program.import_area.if_(ExprIdent("TYPE_CHECKING"))(
305
- lambda ast: ast.import_(module, is_import_attr, alias)
306
- )
@@ -60,6 +60,7 @@ class ImportStatement(Statement):
60
60
  module: str
61
61
  is_import_attr: bool
62
62
  alias: Optional[str] = None
63
+ is_import_type: bool = False
63
64
 
64
65
  def to_python(self):
65
66
  if self.module.find(".") != -1 and self.is_import_attr:
@@ -76,9 +77,10 @@ class ImportStatement(Statement):
76
77
  if self.module.find(".") != -1 and self.is_import_attr:
77
78
  module, attr = self.module.rsplit(".", 1)
78
79
  module = module.replace(".", "/")
80
+ type_prefix = "type " if self.is_import_type else ""
79
81
  if self.alias is not None:
80
- return f"import {{ {attr} as {self.alias} }} from '{module}';"
81
- return f"import {{ {attr} }} from '{module}';"
82
+ return f"import {{ {type_prefix}{attr} as {self.alias} }} from '{module}';"
83
+ return f"import {{ {type_prefix}{attr} }} from '{module}';"
82
84
  else:
83
85
  raise NotImplementedError(self)
84
86
 
@@ -1,6 +1,6 @@
1
1
  from __future__ import annotations
2
2
 
3
- from typing import Callable, Literal, Optional, Protocol
3
+ from typing import Literal, Optional, Protocol
4
4
 
5
5
  KEY = str | tuple
6
6
  NO_KEY = tuple()
@@ -11,5 +11,9 @@ AstID = tuple[int, ...]
11
11
 
12
12
  class ImportFn(Protocol):
13
13
  def __call__(
14
- self, module: str, is_import_attr: bool, alias: Optional[str] = None
14
+ self,
15
+ module: str,
16
+ is_import_attr: bool,
17
+ alias: Optional[str] = None,
18
+ is_import_type: bool = False,
15
19
  ) -> None: ...
@@ -207,11 +207,13 @@ class _PythonCodeblockParser:
207
207
  target.return_(self.parse_expr(target, node.value))
208
208
 
209
209
  def _parse_if(self, target: AST, node: py_ast.If) -> None:
210
+ # Hoisting imports out of either branch would make them unconditional.
211
+ branch_parser = _PythonCodeblockParser(global_imports=False)
210
212
  if_ast = target.if_(self.parse_expr(target, node.test))
211
- self.parse_body(if_ast, node.body)
213
+ branch_parser.parse_body(if_ast, node.body)
212
214
  if node.orelse:
213
215
  else_ast = target.else_()
214
- self.parse_body(else_ast, node.orelse)
216
+ branch_parser.parse_body(else_ast, node.orelse)
215
217
 
216
218
  def _parse_for(self, target: AST, node: py_ast.For) -> None:
217
219
  if node.orelse:
@@ -0,0 +1,28 @@
1
+ [project]
2
+ name = "codegen-2"
3
+ version = "2.18.1"
4
+ description = "Meta-programming"
5
+ readme = "README.md"
6
+ requires-python = ">=3.11"
7
+ dependencies = []
8
+
9
+ [[project.authors]]
10
+ name = "Binh Vu"
11
+ email = "binh@toan2.com"
12
+
13
+ [project.license]
14
+ text = "MIT"
15
+
16
+ [dependency-groups]
17
+ dev = [
18
+ "pytest>=8.0.0",
19
+ "autoflake>=2.2.1",
20
+ ]
21
+
22
+ [build-system]
23
+ requires = ["uv_build"]
24
+ build-backend = "uv_build"
25
+
26
+ [tool.uv.build-backend]
27
+ module-name = "codegen"
28
+ module-root = ""
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "codegen-2"
3
- version = "2.16.7"
3
+ version = "2.18.1"
4
4
  description = "Meta-programming"
5
5
  authors = [{ name = "Binh Vu", email = "binh@toan2.com" }]
6
6
  license = { text = "MIT" }
File without changes