erdify 1.4.0__tar.gz → 1.4.2__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.
Files changed (27) hide show
  1. {erdify-1.4.0 → erdify-1.4.2}/PKG-INFO +1 -1
  2. {erdify-1.4.0 → erdify-1.4.2}/pyproject.toml +1 -1
  3. {erdify-1.4.0 → erdify-1.4.2}/pyproject.toml.orig +1 -1
  4. erdify-1.4.2/src/erdify/parser/aliases.py +447 -0
  5. {erdify-1.4.0 → erdify-1.4.2}/src/erdify/parser/annotations.py +10 -15
  6. {erdify-1.4.0 → erdify-1.4.2}/src/erdify/parser/core.py +15 -79
  7. {erdify-1.4.0 → erdify-1.4.2}/src/erdify/parser/dialects/annotated.py +6 -4
  8. {erdify-1.4.0 → erdify-1.4.2}/src/erdify/parser/dialects/base.py +5 -4
  9. {erdify-1.4.0 → erdify-1.4.2}/LICENSE +0 -0
  10. {erdify-1.4.0 → erdify-1.4.2}/README.md +0 -0
  11. {erdify-1.4.0 → erdify-1.4.2}/src/erdify/__init__.py +0 -0
  12. {erdify-1.4.0 → erdify-1.4.2}/src/erdify/__main__.py +0 -0
  13. {erdify-1.4.0 → erdify-1.4.2}/src/erdify/cli.py +0 -0
  14. {erdify-1.4.0 → erdify-1.4.2}/src/erdify/config.py +0 -0
  15. {erdify-1.4.0 → erdify-1.4.2}/src/erdify/generator.py +0 -0
  16. {erdify-1.4.0 → erdify-1.4.2}/src/erdify/inject.py +0 -0
  17. {erdify-1.4.0 → erdify-1.4.2}/src/erdify/parser/__init__.py +0 -0
  18. {erdify-1.4.0 → erdify-1.4.2}/src/erdify/parser/common.py +0 -0
  19. {erdify-1.4.0 → erdify-1.4.2}/src/erdify/parser/dialects/__init__.py +0 -0
  20. {erdify-1.4.0 → erdify-1.4.2}/src/erdify/parser/dialects/django.py +0 -0
  21. {erdify-1.4.0 → erdify-1.4.2}/src/erdify/parser/dialects/keyless.py +0 -0
  22. {erdify-1.4.0 → erdify-1.4.2}/src/erdify/parser/dialects/sqlalchemy.py +0 -0
  23. {erdify-1.4.0 → erdify-1.4.2}/src/erdify/parser/discovery.py +0 -0
  24. {erdify-1.4.0 → erdify-1.4.2}/src/erdify/parser/inheritance.py +0 -0
  25. {erdify-1.4.0 → erdify-1.4.2}/src/erdify/py.typed +0 -0
  26. {erdify-1.4.0 → erdify-1.4.2}/src/erdify/pyproject.py +0 -0
  27. {erdify-1.4.0 → erdify-1.4.2}/src/erdify/sql_parser.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: erdify
3
- Version: 1.4.0
3
+ Version: 1.4.2
4
4
  Summary: A zero-dependency ERD generator for your Python models — pure AST, no database required.
5
5
  Keywords: sqlmodel,sqlalchemy,django,pydantic,dataclass,erd,plantuml,diagram,database,erdify,sql,ddl
6
6
  Author: devsuit GmbH, Fabian Clemenz
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "erdify"
3
- version = "1.4.0"
3
+ version = "1.4.2"
4
4
  description = "A zero-dependency ERD generator for your Python models — pure AST, no database required."
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "erdify"
3
- version = "1.4.0"
3
+ version = "1.4.2"
4
4
  description = "A zero-dependency ERD generator for your Python models — pure AST, no database required."
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -0,0 +1,447 @@
1
+ """Column type aliases, resolved per module like Python names (#241, #249, #250).
2
+
3
+ SQLAlchemy's ``intpk = Annotated[int, mapped_column(primary_key=True)]``
4
+ lets ``id: Mapped[intpk]`` declare a primary key. Which object ``intpk``
5
+ names depends on the module and on where the class is defined in it, so it
6
+ is resolved like Python resolves the name when the class body runs:
7
+
8
+ - the module's last module-level binding of the name *before the class*
9
+ counts: an alias (``X = Annotated[...]``, ``X = other_alias``), an import,
10
+ a star import, or anything else (a function, a class, ``X = int``);
11
+ - an import is followed into the scanned file it names; a star import
12
+ provides the names that module exports (``__all__``, else its public names);
13
+ - an alias built on another one (``Annotated[intpk, ...]``) resolves that one
14
+ where the alias is defined;
15
+ - a name the module does not bind at all (e.g. imported under
16
+ ``TYPE_CHECKING``) falls back to an alias of that name defined identically
17
+ wherever it is defined, unless a model class has that name.
18
+ """
19
+
20
+ import ast
21
+ from bisect import bisect_left
22
+ from pathlib import Path
23
+ from types import MappingProxyType
24
+ from typing import Dict, Iterable, List, Mapping, NamedTuple, Set, Tuple
25
+
26
+ from .annotations import defines_column_alias, expand_aliases, parse_quoted
27
+
28
+ #: A name looked up at a line of a file (``None``: the whole module).
29
+ _Lookup = Tuple[Path, str, "int | None"]
30
+
31
+ #: ``_find_module`` result for an absolute import several scanned files match.
32
+ _AMBIGUOUS = Path("<ambiguous>")
33
+
34
+
35
+ class _Binding(NamedTuple):
36
+ """One module-level binding of a name."""
37
+
38
+ #: ``"alias"`` (a possible alias value), ``"import"`` or ``"other"``.
39
+ kind: str
40
+ #: Line of the statement; a class only sees bindings before its own line.
41
+ line: int
42
+ #: The assigned value (``"alias"``).
43
+ value: ast.expr | None = None
44
+ #: The imported module as written, ``None`` for ``from . import x`` (``"import"``).
45
+ module: str | None = None
46
+ #: Leading dots of a relative import (``"import"``).
47
+ level: int = 0
48
+ #: The name imported from that module (``"import"``).
49
+ original: str = ""
50
+
51
+
52
+ class AliasResolver:
53
+ """Resolves column type aliases per scanned module.
54
+
55
+ Args:
56
+ trees: Each scanned file's syntax tree.
57
+ classes: Names of the parsed model classes; the by-name fallback never
58
+ turns one into an alias.
59
+ """
60
+
61
+ def __init__(self, trees: Mapping[Path, ast.Module], classes: Iterable[str]):
62
+ self._trees = trees
63
+ self._classes = set(classes)
64
+ self._events: Dict[Path, List[Tuple[str, _Binding]]] = {}
65
+ #: Per file, each name's bindings in statement order (star imports expanded).
66
+ self._histories: Dict[Path, Dict[str, List[_Binding]]] = {}
67
+ #: Per file, names with a binding that may be an alias.
68
+ self._candidates: Dict[Path, List[str]] = {}
69
+ self._scoping: Set[Path] = set()
70
+ #: Star-import cut-offs so far; a history built across one is not cached.
71
+ self._star_cycles = 0
72
+ self._resolved_paths: Dict[Path, Path] = {}
73
+ self._modules: Dict[Tuple[Path, str | None, int], Path | None] = {}
74
+ self._resolved: Dict[Tuple[Path, str, int | None], ast.expr | None] = {}
75
+ #: Cycle cut-offs so far; a result computed across one is not cached.
76
+ self._cycles = 0
77
+ self._views: Dict[Tuple[Path, int | None], Mapping[str, ast.expr]] = {}
78
+ #: Name -> its by-name fallback alias (or None), once known (#254).
79
+ self._fallback: Dict[str, ast.expr | None] = {}
80
+ #: Name -> files whose last binding of it may be an alias (built once).
81
+ self._definers: Dict[str, List[Path]] | None = None
82
+ #: Candidate modules of each ambiguous absolute import.
83
+ self._ambiguous: Dict[Tuple[Path, str | None, int], List[Path]] = {}
84
+ self._suffixes: Dict[Tuple[str, ...], List[Path]] | None = None
85
+
86
+ def aliases_for(self, file: Path | None, line: int | None = None) -> Mapping[str, ast.expr]:
87
+ """Alias name -> fully expanded ``Annotated[...]``, as seen at ``line`` of ``file``."""
88
+ if file is None or file not in self._trees:
89
+ return MappingProxyType(self._all_fallbacks())
90
+ key = (file, line)
91
+ view = self._views.get(key)
92
+ if view is not None:
93
+ return view
94
+ result: Dict[str, ast.expr] = {}
95
+ for name in self._candidate_names(file):
96
+ value = self._resolve(file, name, line, ())
97
+ if value is not None:
98
+ result[name] = value
99
+ for name in self._fallback_candidates():
100
+ # A name bound before the class (also via `*`) never falls back.
101
+ if name not in result and self._binding_at(file, name, line) is None:
102
+ value = self._fallback_for(name)
103
+ if value is not None:
104
+ result[name] = value
105
+ view = MappingProxyType(result)
106
+ self._views[key] = view
107
+ return view
108
+
109
+ # -- module scope ----------------------------------------------------------
110
+
111
+ def _module_events(self, file: Path) -> List[Tuple[str, _Binding]]:
112
+ """``(name, binding)`` per module-level binding, in statement order.
113
+
114
+ A star import is recorded as name ``"*"`` and expanded by ``_scope``.
115
+ """
116
+ events = self._events.get(file)
117
+ if events is not None:
118
+ return events
119
+ events = []
120
+ for node in self._trees[file].body:
121
+ line = node.lineno
122
+ if isinstance(node, ast.Assign):
123
+ if len(node.targets) == 1 and isinstance(node.targets[0], ast.Name):
124
+ events.append((node.targets[0].id, self._assigned(node.value, line)))
125
+ else: # chained or unpacking: whatever it binds is not an alias
126
+ events.extend(
127
+ (sub.id, _Binding("other", line))
128
+ for target in node.targets
129
+ for sub in ast.walk(target)
130
+ if isinstance(sub, ast.Name) and isinstance(sub.ctx, ast.Store)
131
+ )
132
+ elif isinstance(node, ast.AnnAssign) and isinstance(node.target, ast.Name):
133
+ if node.value is not None: # a bare `x: int` binds nothing
134
+ events.append((node.target.id, self._assigned(node.value, line)))
135
+ elif isinstance(node, ast.ImportFrom):
136
+ for alias in node.names:
137
+ binding = _Binding(
138
+ "import", line, module=node.module, level=node.level, original=alias.name
139
+ )
140
+ events.append(
141
+ ("*" if alias.name == "*" else alias.asname or alias.name, binding)
142
+ )
143
+ elif isinstance(node, ast.Import):
144
+ for alias in node.names:
145
+ name = alias.asname or alias.name.split(".")[0]
146
+ events.append((name, _Binding("other", line)))
147
+ elif isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)):
148
+ events.append((node.name, _Binding("other", line)))
149
+ elif type(node).__name__ == "TypeAlias":
150
+ # PEP 695 `type X = ...`: SQLAlchemy takes no mapped_column() from it.
151
+ alias_name = getattr(node, "name", None)
152
+ if isinstance(alias_name, ast.Name):
153
+ events.append((alias_name.id, _Binding("other", line)))
154
+ self._events[file] = events
155
+ return events
156
+
157
+ @staticmethod
158
+ def _assigned(value: ast.expr, line: int) -> _Binding:
159
+ """``X = Annotated[...]`` or ``X = other_alias`` may be an alias; anything else is not."""
160
+ if isinstance(value, (ast.Subscript, ast.Name)):
161
+ return _Binding("alias", line, value=value)
162
+ return _Binding("other", line)
163
+
164
+ def _history(self, file: Path) -> Dict[str, List[_Binding]]:
165
+ """Each name's module-level bindings in statement order, star imports expanded.
166
+
167
+ Not cached while a circular star import cut it short, so the cut-off
168
+ never becomes a permanently missing binding.
169
+ """
170
+ cached = self._histories.get(file)
171
+ if cached is not None:
172
+ return cached
173
+ history: Dict[str, List[_Binding]] = {}
174
+ cycles = self._star_cycles
175
+ self._scoping.add(file)
176
+ try:
177
+ for name, binding in self._module_events(file):
178
+ if name != "*":
179
+ history.setdefault(name, []).append(binding)
180
+ continue
181
+ target = self._find_module(file, binding.module, binding.level)
182
+ if target is None or target is _AMBIGUOUS:
183
+ continue
184
+ if target in self._scoping:
185
+ self._star_cycles += 1
186
+ continue
187
+ for exported in self._exports(target):
188
+ history.setdefault(exported, []).append(binding._replace(original=exported))
189
+ finally:
190
+ self._scoping.discard(file)
191
+ if self._star_cycles == cycles:
192
+ self._histories[file] = history
193
+ return history
194
+
195
+ def _candidate_names(self, file: Path) -> List[str]:
196
+ """Names with a binding in ``file`` that may be an alias (few, usually)."""
197
+ cached = self._candidates.get(file)
198
+ if cached is not None:
199
+ return cached
200
+ names = [
201
+ name
202
+ for name, bindings in self._history(file).items()
203
+ if any(b.kind != "other" for b in bindings)
204
+ ]
205
+ if file in self._histories: # only once the history itself is final
206
+ self._candidates[file] = names
207
+ return names
208
+
209
+ def _binding_at(self, file: Path, name: str, line: int | None) -> _Binding | None:
210
+ """The last module-level binding of ``name`` before ``line`` (any, if None)."""
211
+ bindings = self._history(file).get(name)
212
+ if not bindings:
213
+ return None
214
+ if line is None:
215
+ return bindings[-1]
216
+ index = bisect_left([b.line for b in bindings], line)
217
+ return bindings[index - 1] if index else None
218
+
219
+ def _exports(self, file: Path) -> List[str]:
220
+ """What ``from <file> import *`` binds: the last literal ``__all__``, else public names."""
221
+ exports: List[str] | None = None
222
+ for node in self._trees[file].body:
223
+ targets = (
224
+ node.targets
225
+ if isinstance(node, ast.Assign)
226
+ else [node.target]
227
+ if isinstance(node, ast.AnnAssign)
228
+ else []
229
+ )
230
+ if any(isinstance(t, ast.Name) and t.id == "__all__" for t in targets):
231
+ value = node.value if isinstance(node, (ast.Assign, ast.AnnAssign)) else None
232
+ if value is None: # `__all__: list[str]` alone binds nothing
233
+ continue
234
+ exports = (
235
+ [
236
+ elt.value
237
+ for elt in value.elts
238
+ if isinstance(elt, ast.Constant) and isinstance(elt.value, str)
239
+ ]
240
+ if isinstance(value, (ast.List, ast.Tuple))
241
+ else None # not a literal: unknown, fall back to public names
242
+ )
243
+ if exports is not None:
244
+ return exports
245
+ return [name for name in self._history(file) if not name.startswith("_")]
246
+
247
+ # -- resolution ----------------------------------------------------------------
248
+
249
+ def _resolve(
250
+ self, file: Path, name: str, line: int | None, stack: Tuple[_Lookup, ...]
251
+ ) -> ast.expr | None:
252
+ """The expanded column alias ``name`` refers to at ``line`` of ``file``, or None.
253
+
254
+ The stack holds ``(file, name, line)``, so ``intpk = Annotated[intpk, ...]``
255
+ can refer to the earlier ``intpk`` while a real cycle (mutual imports)
256
+ still stops.
257
+ """
258
+ key = (file, name, line)
259
+ if key in self._resolved:
260
+ return self._resolved[key]
261
+ if key in stack:
262
+ self._cycles += 1 # results computed under this cut-off depend on the stack
263
+ return None
264
+ cycles = self._cycles
265
+ result = None
266
+ binding = self._binding_at(file, name, line)
267
+ if binding is not None:
268
+ result = self._resolve_binding(file, binding, stack + (key,))
269
+ if self._cycles == cycles:
270
+ self._resolved[key] = result
271
+ return result
272
+
273
+ def _resolve_binding(
274
+ self, file: Path, binding: _Binding, stack: Tuple[_Lookup, ...]
275
+ ) -> ast.expr | None:
276
+ if binding.kind == "import":
277
+ target = self._find_module(file, binding.module, binding.level)
278
+ if target is _AMBIGUOUS: # several scanned modules match
279
+ return self._agreed_import(file, binding, stack)
280
+ if target is None: # not a scanned module (stdlib, third party)
281
+ return None
282
+ return self._resolve(target, binding.original, None, stack)
283
+ if binding.kind != "alias" or binding.value is None:
284
+ return None
285
+ # Names inside the alias resolve where, and when, it is defined (#249),
286
+ # also inside a quoted base (`Annotated["intpk", ...]`).
287
+ local: Dict[str, ast.expr] = {}
288
+ for name in self._referenced_names(binding.value):
289
+ if name not in local:
290
+ inner = self._resolve(file, name, binding.line, stack)
291
+ if inner is not None:
292
+ local[name] = inner
293
+ expanded = expand_aliases(binding.value, local)
294
+ return expanded if defines_column_alias(expanded) else None
295
+
296
+ @classmethod
297
+ def _referenced_names(cls, node: ast.expr) -> List[str]:
298
+ """Names ``node`` mentions, also inside quoted type expressions."""
299
+ names: List[str] = []
300
+ for sub in ast.walk(node):
301
+ if isinstance(sub, ast.Name):
302
+ names.append(sub.id)
303
+ elif isinstance(sub, ast.Constant) and isinstance(sub.value, str):
304
+ parsed = parse_quoted(sub)
305
+ if parsed is not None:
306
+ names.extend(cls._referenced_names(parsed))
307
+ return names
308
+
309
+ def _find_module(self, file: Path, module: str | None, level: int) -> Path | None:
310
+ """The scanned file an import refers to, ``_AMBIGUOUS``, or None (not scanned)."""
311
+ key = (file, module, level)
312
+ if key in self._modules:
313
+ return self._modules[key]
314
+ parts = module.split(".") if module else []
315
+ found: Path | None = None
316
+ if level:
317
+ base = file.parent
318
+ for _ in range(level - 1):
319
+ base = base.parent
320
+ if parts:
321
+ stem = base.joinpath(*parts)
322
+ # A package wins over a module file of the same name, as in Python.
323
+ candidates = [stem / "__init__.py", stem.parent / f"{stem.name}.py"]
324
+ else: # `from . import x`: the package itself
325
+ candidates = [base / "__init__.py"]
326
+ found = next((c for c in candidates if c in self._trees), None)
327
+ elif parts:
328
+ # Only a module whose import root contains the importing file can be
329
+ # what it imports: a scanned `pkg/uuid.py` is not `import uuid`.
330
+ matches = [
331
+ m
332
+ for m in self._suffix_index().get(tuple(parts), [])
333
+ if self._root_contains(m, len(parts), file)
334
+ ]
335
+ packages = {m.parent for m in matches if m.name == "__init__.py"}
336
+ # `coltypes.py` beside a `coltypes/` package is shadowed by it.
337
+ matches = [
338
+ m for m in matches if m.name == "__init__.py" or m.with_suffix("") not in packages
339
+ ]
340
+ found = matches[0] if len(matches) == 1 else (_AMBIGUOUS if matches else None)
341
+ if found is _AMBIGUOUS:
342
+ self._ambiguous[key] = matches
343
+ self._modules[key] = found
344
+ return found
345
+
346
+ def _suffix_index(self) -> Dict[Tuple[str, ...], List[Path]]:
347
+ """Dotted-module suffix -> scanned files, e.g. ``("app", "models")``."""
348
+ if self._suffixes is None:
349
+ index: Dict[Tuple[str, ...], List[Path]] = {}
350
+ for path in self._trees:
351
+ dotted = self._dotted(path)
352
+ for size in range(1, len(dotted) + 1):
353
+ index.setdefault(tuple(dotted[-size:]), []).append(path)
354
+ self._suffixes = index
355
+ return self._suffixes
356
+
357
+ def _absolute(self, path: Path) -> Path:
358
+ """``path`` resolved, so a relative scan root (``erdify .``) keeps its packages."""
359
+ resolved = self._resolved_paths.get(path)
360
+ if resolved is None:
361
+ resolved = self._resolved_paths[path] = path.resolve()
362
+ return resolved
363
+
364
+ def _dotted(self, path: Path) -> Tuple[str, ...]:
365
+ """The module path parts of a scanned file (``app/models.py`` -> app, models)."""
366
+ absolute = self._absolute(path)
367
+ return (
368
+ absolute.parent.parts
369
+ if absolute.name == "__init__.py"
370
+ else absolute.with_suffix("").parts
371
+ )
372
+
373
+ def _root_contains(self, module_file: Path, depth: int, importer: Path) -> bool:
374
+ """Whether ``importer`` lies under the root ``module_file`` is imported from."""
375
+ dotted = self._dotted(module_file)
376
+ root = dotted[: len(dotted) - depth]
377
+ return self._absolute(importer).parts[: len(root)] == root
378
+
379
+ def _definer_index(self) -> Dict[str, List[Path]]:
380
+ """Name -> files whose last module-level binding of it may be an alias.
381
+
382
+ Built in one pass over the modules; resolving the values stays lazy.
383
+ """
384
+ if self._definers is None:
385
+ definers: Dict[str, List[Path]] = {}
386
+ for file in self._trees:
387
+ for name, bindings in self._history(file).items():
388
+ if bindings[-1].kind == "alias" and name not in self._classes:
389
+ definers.setdefault(name, []).append(file)
390
+ self._definers = dict(sorted(definers.items()))
391
+ return self._definers
392
+
393
+ def _fallback_candidates(self) -> List[str]:
394
+ """Names some scanned module ends up defining as a possible alias."""
395
+ return list(self._definer_index())
396
+
397
+ def _agreed_import(
398
+ self, file: Path, binding: _Binding, stack: Tuple[_Lookup, ...]
399
+ ) -> ast.expr | None:
400
+ """An ambiguous absolute import: the alias every matching module defines alike.
401
+
402
+ Only the modules the import could name are consulted (#254), never
403
+ other modules that merely define the same name.
404
+ """
405
+ candidates = self._ambiguous[(file, binding.module, binding.level)]
406
+ values = [self._resolve(m, binding.original, None, stack) for m in candidates]
407
+ if any(v is None for v in values):
408
+ return None # one candidate defines no such alias: unknown which applies
409
+ return self._agreed([v for v in values if v is not None])
410
+
411
+ def _fallback_for(self, name: str) -> ast.expr | None:
412
+ """The alias ``name`` names wherever it is defined, if all definitions agree.
413
+
414
+ For a name a model module does not bind (e.g. imported under
415
+ ``TYPE_CHECKING``). Resolved on demand, per name, so an alias built on
416
+ another one (``UserId = intpk``) is included (#254).
417
+ """
418
+ if name in self._fallback:
419
+ return self._fallback[name]
420
+ result = None
421
+ if name not in self._classes:
422
+ values = [
423
+ value
424
+ for file in self._definitions_of(name)
425
+ if (value := self._resolve(file, name, None, ())) is not None
426
+ ]
427
+ result = self._agreed(values)
428
+ self._fallback[name] = result
429
+ return result
430
+
431
+ def _definitions_of(self, name: str) -> List[Path]:
432
+ """Files whose last module-level binding of ``name`` may be an alias."""
433
+ return self._definer_index().get(name, [])
434
+
435
+ @staticmethod
436
+ def _agreed(values: List[ast.expr]) -> ast.expr | None:
437
+ """The common value, if every definition gives the same one."""
438
+ return values[0] if values and len({ast.dump(v) for v in values}) == 1 else None
439
+
440
+ def _all_fallbacks(self) -> Dict[str, ast.expr]:
441
+ """Every by-name fallback alias (for a class whose file is unknown)."""
442
+ table: Dict[str, ast.expr] = {}
443
+ for name in self._fallback_candidates():
444
+ value = self._fallback_for(name)
445
+ if value is not None:
446
+ table[name] = value
447
+ return table
@@ -152,9 +152,7 @@ def column_type(node: ast.expr) -> Tuple[str, bool]:
152
152
  return ast.unparse(node), False
153
153
 
154
154
 
155
- def expand_aliases(
156
- node: ast.expr, aliases: Mapping[str, ast.expr], stack: Tuple[str, ...] = ()
157
- ) -> ast.expr:
155
+ def expand_aliases(node: ast.expr, aliases: Mapping[str, ast.expr]) -> ast.expr:
158
156
  """``node`` with each column type alias replaced by its definition (#241).
159
157
 
160
158
  ``id: Mapped[intpk]`` with ``intpk = Annotated[int, mapped_column(...)]``
@@ -162,26 +160,25 @@ def expand_aliases(
162
160
  (``Mapped["intpk"]``). Only type positions are expanded: ``Annotated``
163
161
  metadata, ``Literal`` values and call arguments (``ForeignKey("user.id")``)
164
162
  are left alone, and so is a subscripted (generic) alias such as
165
- ``Pk[int]``. An alias referring to itself is left as is. ``node`` itself
166
- is never modified.
163
+ ``Pk[int]``. Each value in ``aliases`` must already be resolved where
164
+ it is defined: it is inserted as is, never expanded again with the
165
+ names of the place that uses it. ``node`` itself is never modified.
167
166
  """
168
167
  if not aliases or not _mentions_alias(node, aliases):
169
168
  return node
170
169
  if isinstance(node, ast.Name):
171
- if node.id in aliases and node.id not in stack:
172
- return expand_aliases(aliases[node.id], aliases, stack + (node.id,))
173
- return node
170
+ return aliases.get(node.id, node)
174
171
  if isinstance(node, ast.Constant):
175
172
  parsed = parse_quoted(node)
176
173
  if parsed is None:
177
174
  return node
178
- expanded = expand_aliases(parsed, aliases, stack)
175
+ expanded = expand_aliases(parsed, aliases)
179
176
  return expanded if expanded is not parsed else node
180
177
  if isinstance(node, ast.BinOp) and isinstance(node.op, ast.BitOr):
181
178
  return ast.BinOp(
182
- left=expand_aliases(node.left, aliases, stack),
179
+ left=expand_aliases(node.left, aliases),
183
180
  op=node.op,
184
- right=expand_aliases(node.right, aliases, stack),
181
+ right=expand_aliases(node.right, aliases),
185
182
  )
186
183
  if isinstance(node, ast.Subscript):
187
184
  name, args = subscript_parts(node)
@@ -192,10 +189,8 @@ def expand_aliases(
192
189
  ):
193
190
  return node
194
191
  # Annotated: only the type is a type position, not the metadata.
195
- parts: List[ast.expr] = [expand_aliases(args[0], aliases, stack)] + (
196
- args[1:]
197
- if name == "Annotated"
198
- else [expand_aliases(a, aliases, stack) for a in args[1:]]
192
+ parts: List[ast.expr] = [expand_aliases(args[0], aliases)] + (
193
+ args[1:] if name == "Annotated" else [expand_aliases(a, aliases) for a in args[1:]]
199
194
  )
200
195
  slice_: ast.expr = (
201
196
  ast.Tuple(elts=parts, ctx=ast.Load()) if isinstance(node.slice, ast.Tuple) else parts[0]
@@ -5,10 +5,10 @@ import sys
5
5
  from dataclasses import dataclass
6
6
  from fnmatch import fnmatchcase
7
7
  from pathlib import Path
8
- from typing import Dict, List, Tuple
8
+ from typing import Callable, Dict, List, Mapping, Tuple
9
9
 
10
10
  from ..config import EntityInfo, EnumInfo, is_structural_link_table
11
- from .annotations import defines_column_alias
11
+ from .aliases import AliasResolver
12
12
  from .common import is_enum_class, to_snake_case
13
13
  from .dialects import ParseContext
14
14
  from .discovery import discover_model_files
@@ -111,6 +111,8 @@ class ASTDatabaseParser:
111
111
  self._enums: Dict[str, EnumInfo] = {}
112
112
  self._all_classes: Dict[str, ast.ClassDef] = {} # Store all class definitions
113
113
  self._file_trees: Dict[Path, ast.Module] = {}
114
+ #: The file each class definition (by ``id`` of its node) was read from.
115
+ self._class_files: Dict[int, Path] = {}
114
116
  #: Classes and dialects (with their caches); renewed for each parse run.
115
117
  self._ctx = self._new_context()
116
118
 
@@ -139,6 +141,7 @@ class ASTDatabaseParser:
139
141
  for node in ast.walk(tree):
140
142
  if isinstance(node, ast.ClassDef):
141
143
  self._all_classes[node.name] = node
144
+ self._class_files[id(node)] = model_file
142
145
  except Exception as e:
143
146
  print(f"Error parsing {model_file}: {e}", file=sys.stderr)
144
147
  self._parse_errors.append((model_file, str(e)))
@@ -243,85 +246,18 @@ class ASTDatabaseParser:
243
246
  infer_keys=self.infer_keys,
244
247
  django_raw_types=self.django_raw_types,
245
248
  base_classes=self.base_classes,
246
- aliases=self._annotated_aliases(),
249
+ aliases_for=self._alias_lookup(),
247
250
  )
248
251
 
249
- def _annotated_aliases(self) -> Dict[str, ast.expr]:
250
- """Module-level column type aliases of every scanned file, by name (#241).
251
-
252
- ``X = Annotated[..., mapped_column(...)]`` and ``X: TypeAlias = ...``.
253
- A class of the same name wins. A name defined differently in two
254
- places, or also bound to something else at module level (another
255
- assignment, a chained or unpacking one, or an import under that name
256
- other than of the alias itself, or any import of that name in the
257
- module defining the alias), is ambiguous and not used. A PEP 695
258
- ``type X = ...`` alias is not read: SQLAlchemy does not take a
259
- ``mapped_column()`` from it.
260
- """
261
- found: Dict[str, ast.expr] = {}
262
- ambiguous: set[str] = set()
263
- for tree in self._file_trees.values():
264
- # An import of the alias's own name in the module defining it
265
- # replaces it (`UUID = Annotated[...]` then `from uuid import UUID`).
266
- defined = {
267
- target.id
268
- for node in tree.body
269
- if isinstance(node, (ast.Assign, ast.AnnAssign))
270
- for target in (node.targets if isinstance(node, ast.Assign) else [node.target])
271
- if isinstance(target, ast.Name)
272
- }
273
- imported = {
274
- alias.asname or alias.name.split(".")[0]
275
- for node in tree.body
276
- if isinstance(node, (ast.Import, ast.ImportFrom))
277
- for alias in node.names
278
- }
279
- ambiguous.update(defined & imported)
280
- for node in tree.body:
281
- ambiguous.update(self._other_module_bindings(node))
282
- name, value = None, None
283
- if (
284
- isinstance(node, ast.Assign)
285
- and len(node.targets) == 1
286
- and isinstance(node.targets[0], ast.Name)
287
- ):
288
- name, value = node.targets[0].id, node.value
289
- elif isinstance(node, ast.AnnAssign) and isinstance(node.target, ast.Name):
290
- name, value = node.target.id, node.value
291
- if name is None or name in self._all_classes:
292
- continue
293
- if value is None or not defines_column_alias(value):
294
- ambiguous.add(name) # also bound to something else
295
- continue
296
- if name in found and ast.dump(found[name]) != ast.dump(value):
297
- ambiguous.add(name)
298
- found[name] = value
299
- return {name: value for name, value in found.items() if name not in ambiguous}
300
-
301
- @staticmethod
302
- def _other_module_bindings(node: ast.stmt) -> List[str]:
303
- """Names a module-level statement binds other than as a plain ``X = ...``.
304
-
305
- Chained (``a = b = ...``) and unpacking (``a, b = ...``) assignments,
306
- and imports under a different name (``import UUID as intpk``). An
307
- import of the alias itself (``from .types import intpk``) keeps it.
308
- """
309
- if isinstance(node, ast.Assign) and not (
310
- len(node.targets) == 1 and isinstance(node.targets[0], ast.Name)
311
- ):
312
- return [
313
- sub.id
314
- for target in node.targets
315
- for sub in ast.walk(target)
316
- if isinstance(sub, ast.Name) and isinstance(sub.ctx, ast.Store)
317
- ]
318
- if isinstance(node, (ast.Import, ast.ImportFrom)):
319
- return [
320
- alias.asname
321
- for alias in node.names
322
- if alias.asname is not None and alias.asname != alias.name.split(".")[-1]
323
- ]
324
- return []
252
+ def _alias_lookup(self) -> "Callable[[ast.ClassDef], Mapping[str, ast.expr]]":
253
+ """Class -> the column type aliases visible where it is defined (#249, #250)."""
254
+ resolver = AliasResolver(self._file_trees, self._all_classes)
255
+ class_files = self._class_files
256
+
257
+ def aliases_for(class_node: ast.ClassDef) -> Mapping[str, ast.expr]:
258
+ return resolver.aliases_for(class_files.get(id(class_node)), class_node.lineno)
259
+
260
+ return aliases_for
325
261
 
326
262
  def _classify_source(self, class_node: ast.ClassDef) -> str | None:
327
263
  """Classify which model framework a class belongs to (:meth:`ParseContext.classify`).
@@ -41,7 +41,7 @@ class AnnotatedDialect(Dialect):
41
41
  if isinstance(node, ast.AnnAssign) and isinstance(node.target, ast.Name):
42
42
  field_name = node.target.id
43
43
  if self.reads_annotated_columns:
44
- node = self._with_aliases_expanded(node)
44
+ node = self._with_aliases_expanded(node, class_node)
45
45
 
46
46
  # Skip special attributes
47
47
  if field_name.startswith("_"):
@@ -85,9 +85,11 @@ class AnnotatedDialect(Dialect):
85
85
  kw.arg in ("default", "default_factory", "insert_default") for kw in value.keywords
86
86
  )
87
87
 
88
- def _with_aliases_expanded(self, node: ast.AnnAssign) -> ast.AnnAssign:
89
- """``node`` with column type aliases in its annotation expanded (#241)."""
90
- annotation = expand_aliases(node.annotation, self.ctx.aliases)
88
+ def _with_aliases_expanded(
89
+ self, node: ast.AnnAssign, class_node: ast.ClassDef
90
+ ) -> ast.AnnAssign:
91
+ """``node`` with column type aliases expanded, as seen where its class is defined."""
92
+ annotation = expand_aliases(node.annotation, self.ctx.aliases_for(class_node))
91
93
  if annotation is node.annotation:
92
94
  return node
93
95
  expanded = ast.AnnAssign(
@@ -2,7 +2,7 @@
2
2
 
3
3
  import ast
4
4
  from dataclasses import dataclass, field
5
- from typing import ClassVar, Dict, Iterable, List, Set, Tuple
5
+ from typing import Callable, ClassVar, Dict, Iterable, List, Mapping, Set, Tuple
6
6
 
7
7
  from ...config import EntityInfo, FieldInfo
8
8
  from ..annotations import is_call_to
@@ -21,8 +21,9 @@ class ParseContext:
21
21
  base_classes: Extra base-class names treated as Pydantic models.
22
22
  dialects: One instance of every dialect, in ``MODEL_SOURCES`` order;
23
23
  created on first use.
24
- aliases: Module-level ``Annotated`` type aliases by name
25
- (``intpk = Annotated[int, mapped_column(primary_key=True)]``, #241).
24
+ aliases_for: Column type aliases visible where a class is defined
25
+ (``intpk = Annotated[int, mapped_column(primary_key=True)]``,
26
+ #241, #249, #250).
26
27
  """
27
28
 
28
29
  classes: ClassIndex = field(default_factory=lambda: ClassIndex({}))
@@ -30,7 +31,7 @@ class ParseContext:
30
31
  django_raw_types: bool = False
31
32
  base_classes: Set[str] = field(default_factory=set)
32
33
  dialects: List["Dialect"] = field(default_factory=list)
33
- aliases: Dict[str, ast.expr] = field(default_factory=dict)
34
+ aliases_for: Callable[[ast.ClassDef], Mapping[str, ast.expr]] = lambda _class_node: {}
34
35
 
35
36
  def all_dialects(self) -> List["Dialect"]:
36
37
  """Every dialect, in classification order, bound to this context."""
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes