|
| 1 | +"""Data-model map — what shape the data this project moves around actually has. |
| 2 | +
|
| 3 | +The symbol index tells you *where* a class named ``User`` is declared. It does |
| 4 | +not tell you what a ``User`` *is*: which fields it carries, and of what type. |
| 5 | +For someone trying to understand an unfamiliar codebase without reading every |
| 6 | +file, that second question is often the important one — "an Order has a total, a |
| 7 | +list of items and a status" says more about the program than any call graph. |
| 8 | +
|
| 9 | +This map reads those shapes straight from the source CodeABC already loaded — no |
| 10 | +LLM, nothing to install or run — by recognising the four ways Python code |
| 11 | +declares a data record explicitly: |
| 12 | +
|
| 13 | + dataclass ``@dataclass class User: id: int; name: str = ""`` |
| 14 | + pydantic ``class Order(BaseModel): total: float`` |
| 15 | + TypedDict ``class Movie(TypedDict): title: str`` |
| 16 | + NamedTuple ``class Coord(NamedTuple): lat: float`` |
| 17 | +
|
| 18 | +Each model carries its name, which of the four kinds it is, the fields it |
| 19 | +declares (name, type annotation, and whether it has a default), and the file and |
| 20 | +line where it is defined. |
| 21 | +
|
| 22 | +:func:`find_data_models` is pure over the file contents, so it is unit-testable |
| 23 | +with plain strings and needs no repository. |
| 24 | +
|
| 25 | +Limitations (kept honest on purpose): |
| 26 | +
|
| 27 | + * Only the four class-based declaration styles above. The functional forms |
| 28 | + (``TypedDict("Movie", {...})``, ``namedtuple("Coord", [...])``) and ORM |
| 29 | + bases (SQLAlchemy ``Base``, Django ``models.Model``) are out of scope — the |
| 30 | + latter are too easy to confuse with ordinary classes to flag safely. |
| 31 | + * Only statically-declared, annotated fields. Attributes assigned in |
| 32 | + ``__init__`` or built dynamically are not counted. |
| 33 | + * Inherited fields are not flattened in; each model lists only what it |
| 34 | + declares itself. |
| 35 | + * A file that does not parse as Python is skipped, not guessed at. |
| 36 | +""" |
| 37 | + |
| 38 | +from __future__ import annotations |
| 39 | + |
| 40 | +import ast |
| 41 | + |
| 42 | +# Base-class names that mark a class as a typed data record, mapped to the kind |
| 43 | +# label we report for them. |
| 44 | +_BASE_KINDS = { |
| 45 | + "BaseModel": "pydantic", |
| 46 | + "TypedDict": "typeddict", |
| 47 | + "NamedTuple": "namedtuple", |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +def _base_name(node: ast.expr) -> str | None: |
| 52 | + """Return the right-most attribute name of a base/decorator expression. |
| 53 | +
|
| 54 | + ``BaseModel`` -> ``BaseModel``; ``pydantic.BaseModel`` -> ``BaseModel``. |
| 55 | + """ |
| 56 | + if isinstance(node, ast.Name): |
| 57 | + return node.id |
| 58 | + if isinstance(node, ast.Attribute): |
| 59 | + return node.attr |
| 60 | + return None |
| 61 | + |
| 62 | + |
| 63 | +def _is_dataclass(cls: ast.ClassDef) -> bool: |
| 64 | + """True when the class carries a ``@dataclass`` decorator in any form.""" |
| 65 | + for dec in cls.decorator_list: |
| 66 | + target = dec.func if isinstance(dec, ast.Call) else dec |
| 67 | + if _base_name(target) == "dataclass": |
| 68 | + return True |
| 69 | + return False |
| 70 | + |
| 71 | + |
| 72 | +def _classify(cls: ast.ClassDef) -> str | None: |
| 73 | + """Resolve which data-model kind a class is, or ``None`` if it is not one.""" |
| 74 | + if _is_dataclass(cls): |
| 75 | + return "dataclass" |
| 76 | + for base in cls.bases: |
| 77 | + name = _base_name(base) |
| 78 | + if name and name in _BASE_KINDS: |
| 79 | + return _BASE_KINDS[name] |
| 80 | + return None |
| 81 | + |
| 82 | + |
| 83 | +def _annotation_str(node: ast.expr | None) -> str: |
| 84 | + if node is None: |
| 85 | + return "" |
| 86 | + try: |
| 87 | + return ast.unparse(node) |
| 88 | + except Exception: |
| 89 | + return "" |
| 90 | + |
| 91 | + |
| 92 | +def _fields(cls: ast.ClassDef) -> list[dict]: |
| 93 | + """Collect the annotated fields declared directly in the class body.""" |
| 94 | + fields: list[dict] = [] |
| 95 | + for stmt in cls.body: |
| 96 | + if not isinstance(stmt, ast.AnnAssign) or not isinstance(stmt.target, ast.Name): |
| 97 | + continue |
| 98 | + name = stmt.target.id |
| 99 | + # Skip private (_x) and dunder (__x__) names: ORM table hints like |
| 100 | + # __tablename__ and internal bookkeeping are not part of the data shape. |
| 101 | + if name.startswith("_"): |
| 102 | + continue |
| 103 | + fields.append( |
| 104 | + { |
| 105 | + "name": name, |
| 106 | + "type": _annotation_str(stmt.annotation), |
| 107 | + "has_default": stmt.value is not None, |
| 108 | + } |
| 109 | + ) |
| 110 | + return fields |
| 111 | + |
| 112 | + |
| 113 | +def find_data_models(file_contents: dict[str, str], *, limit: int = 60) -> dict: |
| 114 | + """Collect the explicitly-declared data models a project defines. |
| 115 | +
|
| 116 | + Args: |
| 117 | + file_contents: mapping of path to file text (as CodeABC already read it). |
| 118 | + limit: how many models to return in the sorted list. |
| 119 | +
|
| 120 | + Returns ``{"total", "kinds", "models"}`` where each model is |
| 121 | + ``{"name", "kind", "fields", "path", "line"}``, ``kind`` is one of |
| 122 | + ``dataclass`` / ``pydantic`` / ``typeddict`` / ``namedtuple``, and each field |
| 123 | + is ``{"name", "type", "has_default"}``. |
| 124 | + """ |
| 125 | + seen: set[tuple[str, str, int]] = set() |
| 126 | + models: list[dict] = [] |
| 127 | + |
| 128 | + for path, content in file_contents.items(): |
| 129 | + if not content or not path.endswith(".py"): |
| 130 | + continue |
| 131 | + try: |
| 132 | + tree = ast.parse(content) |
| 133 | + except (SyntaxError, ValueError): |
| 134 | + continue |
| 135 | + |
| 136 | + for node in ast.walk(tree): |
| 137 | + if not isinstance(node, ast.ClassDef): |
| 138 | + continue |
| 139 | + kind = _classify(node) |
| 140 | + if kind is None: |
| 141 | + continue |
| 142 | + key = (path, node.name, node.lineno) |
| 143 | + if key in seen: |
| 144 | + continue |
| 145 | + seen.add(key) |
| 146 | + models.append( |
| 147 | + { |
| 148 | + "name": node.name, |
| 149 | + "kind": kind, |
| 150 | + "fields": _fields(node), |
| 151 | + "path": path, |
| 152 | + "line": node.lineno, |
| 153 | + } |
| 154 | + ) |
| 155 | + |
| 156 | + models.sort(key=lambda m: (m["path"], m["line"])) |
| 157 | + kinds = sorted({m["kind"] for m in models}) |
| 158 | + return {"total": len(models), "kinds": kinds, "models": models[:limit]} |
| 159 | + |
| 160 | + |
| 161 | +def render_data_models_markdown(project_name: str, data: dict | None) -> str: |
| 162 | + """Render the data-model map as Markdown, or ``""`` if none were found.""" |
| 163 | + models = (data or {}).get("models") or [] |
| 164 | + if not models: |
| 165 | + return "" |
| 166 | + |
| 167 | + lines = [ |
| 168 | + f"# {project_name} — 数据长什么样(数据模型)", |
| 169 | + "", |
| 170 | + "> 符号索引告诉你一个类“在哪声明”,这里告诉你它“装了什么”——" |
| 171 | + "项目用 dataclass / pydantic / TypedDict / NamedTuple 显式声明的数据记录," |
| 172 | + "以及每条记录有哪些字段、各是什么类型。", |
| 173 | + "", |
| 174 | + ] |
| 175 | + for model in models: |
| 176 | + lines.append(f"## `{model['name']}` ({model['kind']},{model['path']}:{model['line']})") |
| 177 | + if not model["fields"]: |
| 178 | + lines.append("- (未声明带类型标注的字段)") |
| 179 | + lines.append("") |
| 180 | + continue |
| 181 | + for field in model["fields"]: |
| 182 | + entry = f"- `{field['name']}`" |
| 183 | + if field["type"]: |
| 184 | + entry += f":{field['type']}" |
| 185 | + if field["has_default"]: |
| 186 | + entry += "(有默认值)" |
| 187 | + lines.append(entry) |
| 188 | + lines.append("") |
| 189 | + |
| 190 | + return "\n".join(lines).rstrip() + "\n" |
0 commit comments