|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import ast |
3 | 4 | import importlib |
4 | 5 | import json |
5 | 6 | import logging |
@@ -111,6 +112,56 @@ def collect_messages( |
111 | 112 | return target |
112 | 113 |
|
113 | 114 |
|
| 115 | +def remove_unserializable_default_sentinels(content: str) -> str: |
| 116 | + """Replaces defaults that repr a datamodel-code-generator sentinel object |
| 117 | +
|
| 118 | + datamodel-code-generator uses a bare `UNDEFINED = object()` sentinel with |
| 119 | + `is` identity checks. oold's merge_deep deep-copies schema dicts during |
| 120 | + allOf composition, which clones that sentinel into a look-alike object, |
| 121 | + defeating the identity guard, so the generator reprs it into source as |
| 122 | + `default_factory=lambda :Foo.parse_obj(<object object at 0x...>)`, which |
| 123 | + is not valid Python. |
| 124 | +
|
| 125 | + Based on oold.generator.Generator.generate(), but matching the factory |
| 126 | + expression itself rather than everything up to the next closing paren: |
| 127 | + oold's pattern consumes the paren belonging to `parse_obj(`, which leaves |
| 128 | + a dangling `)` behind whenever the sentinel is wrapped in a call. |
| 129 | + """ |
| 130 | + return re.sub( |
| 131 | + r"default_factory=lambda\s*:\s*" |
| 132 | + r"(?:[\w.]+\(<object object at 0x[0-9a-fA-F]+>\)" |
| 133 | + r"|<object object at 0x[0-9a-fA-F]+>)", |
| 134 | + "default=None", |
| 135 | + content, |
| 136 | + ) |
| 137 | + |
| 138 | + |
| 139 | +def ensure_valid_python_source(content: str, path: str) -> None: |
| 140 | + """Raises a descriptive SyntaxError if content is not valid Python |
| 141 | +
|
| 142 | + _fetch_schema writes the generated model to a file that is imported right |
| 143 | + after (or, for non-final calls, on the next process start). A corrupt |
| 144 | + write poisons every later import of osw.model.entity, and there is |
| 145 | + previously-valid content sitting at `path` that would otherwise still |
| 146 | + work. Validating before opening the file for writing means a bad |
| 147 | + generation leaves that previous, valid content in place instead. |
| 148 | + """ |
| 149 | + try: |
| 150 | + ast.parse(content) |
| 151 | + except SyntaxError as e: |
| 152 | + offending_line = "" |
| 153 | + if e.lineno is not None: |
| 154 | + lines = content.splitlines() |
| 155 | + if 0 < e.lineno <= len(lines): |
| 156 | + offending_line = lines[e.lineno - 1] |
| 157 | + message = ( |
| 158 | + f"Generated model for '{path}' is not valid Python: " |
| 159 | + f"{e.msg} (line {e.lineno}): {offending_line!r}" |
| 160 | + ) |
| 161 | + _logger.error(message) |
| 162 | + raise SyntaxError(message) from e |
| 163 | + |
| 164 | + |
114 | 165 | # Reusable type definitions |
115 | 166 | class OverwriteOptions(Enum): |
116 | 167 | """Options for overwriting properties""" |
@@ -840,6 +891,9 @@ def _fetch_schema( |
840 | 891 | # are not v1 compatible mainly by using update_model() |
841 | 892 | content = re.sub(r"(,?\s*unique_items=True\s*)", "", content) |
842 | 893 |
|
| 894 | + # fix unserializable defaults from datamodel-code-generator (#125) |
| 895 | + content = remove_unserializable_default_sentinels(content) |
| 896 | + |
843 | 897 | # Detect empty subclasses, replaces their occurrences with base classes, |
844 | 898 | # and removes the empty class definitions. |
845 | 899 | # Only processes subclasses that follow naming patterns: |
@@ -1048,8 +1102,15 @@ def _fetch_schema( |
1048 | 1102 | content = black.format_str(content, mode=black.Mode()) |
1049 | 1103 | # run isort to sort imports using Vertical Hanging Indent style |
1050 | 1104 | content = isort.code(content, profile="black") |
1051 | | - except Exception: |
1052 | | - pass # black is optional, continue without formatting |
| 1105 | + except Exception as e: |
| 1106 | + # black/isort are optional, continue without formatting, but |
| 1107 | + # do not hide a signal that the generated content is broken |
| 1108 | + _logger.warning(f"Failed to format generated model content: {e}") |
| 1109 | + |
| 1110 | + # validate before writing: a corrupt write poisons every later |
| 1111 | + # import of this file, so leaving the previous valid content in |
| 1112 | + # place is strictly better than writing invalid syntax (#125) |
| 1113 | + ensure_valid_python_source(content, result_model_path) |
1053 | 1114 |
|
1054 | 1115 | with open(result_model_path, "w", encoding="utf-8") as f: |
1055 | 1116 | f.write(content) |
|
0 commit comments