codegen-2 2.16.7__tar.gz → 2.17.0__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.17.0
4
4
  Summary: Meta-programming
5
5
  Author: Binh Vu
6
6
  Author-email: Binh Vu <binh@toan2.com>
@@ -98,8 +98,21 @@ class AST:
98
98
  child.move(source, target)
99
99
  return self
100
100
 
101
- def import_(self, module: str, is_import_attr: bool, alias: Optional[str] = None):
102
- self._add_stmt(ImportStatement(module, is_import_attr, alias))
101
+ def import_(
102
+ self,
103
+ module: str,
104
+ is_import_attr: bool,
105
+ alias: Optional[str] = None,
106
+ is_import_type: bool = False,
107
+ ):
108
+ self._add_stmt(
109
+ ImportStatement(
110
+ module,
111
+ is_import_attr=is_import_attr,
112
+ alias=alias,
113
+ is_import_type=is_import_type,
114
+ )
115
+ )
103
116
 
104
117
  def return_(self, expr: Expr):
105
118
  self._add_stmt(ReturnStatement(expr))
@@ -16,7 +16,7 @@ class Program:
16
16
  root: AST
17
17
  vars: VarRegisters
18
18
  import_area: AST
19
- imported_modules: set[str]
19
+ imported_modules: set[tuple[str, bool, Optional[str], bool]]
20
20
 
21
21
  def __init__(self):
22
22
  self.vars = VarRegisters(self)
@@ -24,10 +24,22 @@ class Program:
24
24
  self.import_area = self.root._add_stmt(BlockStatement(has_owned_env=False))
25
25
  self.imported_modules = set()
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
+ ):
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)
31
43
 
32
44
  def get_ast_by_id(self, id: AstID) -> AST:
33
45
  ast = self.root
@@ -117,14 +129,23 @@ class Program:
117
129
  if child is source.import_area:
118
130
  continue
119
131
  self.root.children.append(
120
- child.clone(self, var_memo).move(child.id, self.root.next_child_id())
132
+ child.clone(self, var_memo).move(
133
+ child.id, self.root.next_child_id()
134
+ )
121
135
  )
122
136
  self.imported_modules.update(source.imported_modules)
123
137
 
124
- def _copy_children(self, source_parent: AST, dest_parent: AST, var_memo: Optional[dict[int, Any]] = None):
138
+ def _copy_children(
139
+ self,
140
+ source_parent: AST,
141
+ dest_parent: AST,
142
+ var_memo: Optional[dict[int, Any]] = None,
143
+ ):
125
144
  for child in source_parent.children:
126
145
  dest_parent.children.append(
127
- child.clone(self, var_memo).move(source_parent.id, dest_parent.next_child_id())
146
+ child.clone(self, var_memo).move(
147
+ source_parent.id, dest_parent.next_child_id()
148
+ )
128
149
  )
129
150
 
130
151
  @classmethod
@@ -187,12 +208,15 @@ class VarRegisters:
187
208
  new_scope = VarScope(
188
209
  ast=(),
189
210
  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
211
+ child_index_end=(
212
+ (reg.scope.child_index_end + body_child_offset)
213
+ if reg.scope.child_index_end is not None
214
+ else None
215
+ ),
191
216
  )
192
217
  else:
193
218
  new_scope = reg.scope.move(
194
- (reg.scope.ast[0],),
195
- (reg.scope.ast[0] + body_child_offset,)
219
+ (reg.scope.ast[0],), (reg.scope.ast[0] + body_child_offset,)
196
220
  )
197
221
 
198
222
  new_reg = VarRegister(
@@ -286,21 +310,35 @@ class ImportHelper:
286
310
  return ExprIdent(ident)
287
311
 
288
312
  def python_import_for_hint(
289
- self, module: str, is_import_attr: bool, alias: Optional[str] = None
313
+ self,
314
+ module: str,
315
+ is_import_attr: bool,
316
+ alias: Optional[str] = None,
290
317
  ):
291
318
  """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:
319
+ import_key = (module, is_import_attr, alias, False)
320
+ if import_key in self.program.imported_modules:
293
321
  return
294
- self.program.imported_modules.add(module)
322
+ self.program.imported_modules.add(import_key)
295
323
 
296
324
  self.program.import_("typing.TYPE_CHECKING", True)
297
325
  for child in self.program.import_area.children:
298
326
  if isinstance(child.stmt, IfStatement) and child.stmt.cond == ExprIdent(
299
327
  "TYPE_CHECKING"
300
328
  ):
301
- child.import_(module, is_import_attr, alias)
329
+ child.import_(
330
+ module,
331
+ is_import_attr=is_import_attr,
332
+ alias=alias,
333
+ is_import_type=False,
334
+ )
302
335
  break
303
336
  else:
304
337
  self.program.import_area.if_(ExprIdent("TYPE_CHECKING"))(
305
- lambda ast: ast.import_(module, is_import_attr, alias)
338
+ lambda ast: ast.import_(
339
+ module,
340
+ is_import_attr=is_import_attr,
341
+ alias=alias,
342
+ is_import_type=False,
343
+ )
306
344
  )
@@ -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: ...
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "codegen-2"
3
- version = "2.16.7"
3
+ version = "2.17.0"
4
4
  description = "Meta-programming"
5
5
  authors = [{ name = "Binh Vu", email = "binh@toan2.com" }]
6
6
  license = { text = "MIT" }
File without changes