codegen-2 2.17.0__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.17.0
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
 
@@ -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,14 +15,14 @@ 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[tuple[str, bool, Optional[str], bool]]
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
27
  def import_(
28
28
  self,
@@ -30,16 +30,18 @@ class Program:
30
30
  is_import_attr: bool,
31
31
  alias: Optional[str] = None,
32
32
  is_import_type: bool = False,
33
- ):
34
- import_key = (module, is_import_attr, alias, is_import_type)
35
- if import_key not in self.imported_modules:
36
- self.import_area.import_(
37
- module,
38
- is_import_attr=is_import_attr,
39
- alias=alias,
40
- is_import_type=is_import_type,
41
- )
42
- self.imported_modules.add(import_key)
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)
43
45
 
44
46
  def get_ast_by_id(self, id: AstID) -> AST:
45
47
  ast = self.root
@@ -104,6 +106,11 @@ class Program:
104
106
 
105
107
  def extend(self, *others: Program) -> None:
106
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
+
107
114
  for source in others:
108
115
  if source is self:
109
116
  # Snapshot self-extension so we don't iterate source lists while appending to them.
@@ -124,29 +131,14 @@ class Program:
124
131
  type=copy.deepcopy(var.type),
125
132
  )
126
133
 
127
- self._copy_children(source.import_area, self.import_area, var_memo)
128
134
  for child in source.root.children:
129
- if child is source.import_area:
135
+ if child.stmt is source.import_area:
130
136
  continue
131
137
  self.root.children.append(
132
138
  child.clone(self, var_memo).move(
133
139
  child.id, self.root.next_child_id()
134
140
  )
135
141
  )
136
- self.imported_modules.update(source.imported_modules)
137
-
138
- def _copy_children(
139
- self,
140
- source_parent: AST,
141
- dest_parent: AST,
142
- var_memo: Optional[dict[int, Any]] = None,
143
- ):
144
- for child in source_parent.children:
145
- dest_parent.children.append(
146
- child.clone(self, var_memo).move(
147
- source_parent.id, dest_parent.next_child_id()
148
- )
149
- )
150
142
 
151
143
  @classmethod
152
144
  def _collect_program_vars(cls, program: Program) -> list[Var]:
@@ -156,7 +148,8 @@ class Program:
156
148
 
157
149
  @classmethod
158
150
  def _collect_ast_vars(cls, ast: AST, vars: list[Var]):
159
- cls._collect_vars(ast.stmt, vars)
151
+ if not isinstance(ast.stmt, ImportArea):
152
+ cls._collect_vars(ast.stmt, vars)
160
153
  for child in ast.children:
161
154
  cls._collect_ast_vars(child, vars)
162
155
 
@@ -304,41 +297,7 @@ class ImportHelper:
304
297
  # mapping from identifiers to their imported paths
305
298
  idents: dict[str, str] = field(default_factory=dict)
306
299
 
307
- def use(self, ident: str):
300
+ def use(self, ident: str, is_import_type: bool = False):
308
301
  assert ident in self.idents, ident
309
- self.program.import_(self.idents[ident], True)
302
+ self.program.import_(self.idents[ident], True, is_import_type=is_import_type)
310
303
  return ExprIdent(ident)
311
-
312
- def python_import_for_hint(
313
- self,
314
- module: str,
315
- is_import_attr: bool,
316
- alias: Optional[str] = None,
317
- ):
318
- """A specific function only for Python to import identifiers that causing circular import error into a same TYPE_CHECKING condition"""
319
- import_key = (module, is_import_attr, alias, False)
320
- if import_key in self.program.imported_modules:
321
- return
322
- self.program.imported_modules.add(import_key)
323
-
324
- self.program.import_("typing.TYPE_CHECKING", True)
325
- for child in self.program.import_area.children:
326
- if isinstance(child.stmt, IfStatement) and child.stmt.cond == ExprIdent(
327
- "TYPE_CHECKING"
328
- ):
329
- child.import_(
330
- module,
331
- is_import_attr=is_import_attr,
332
- alias=alias,
333
- is_import_type=False,
334
- )
335
- break
336
- else:
337
- self.program.import_area.if_(ExprIdent("TYPE_CHECKING"))(
338
- lambda ast: ast.import_(
339
- module,
340
- is_import_attr=is_import_attr,
341
- alias=alias,
342
- is_import_type=False,
343
- )
344
- )
@@ -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.17.0"
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