Skip to content

Commit 2f68c91

Browse files
committed
fix(core): never write unparseable entity.py from fetch_schema
- strip datamodel-code-generator's repr'd `object()` default sentinel - widen oold's regex so a sentinel wrapped in parse_obj() also repairs - validate generated content with ast.parse before overwriting the file - keep the previous valid entity.py in place when generation is broken - log black/isort failures instead of swallowing them silently
1 parent a867b65 commit 2f68c91

2 files changed

Lines changed: 160 additions & 2 deletions

File tree

‎src/osw/core.py‎

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import ast
34
import importlib
45
import json
56
import logging
@@ -111,6 +112,56 @@ def collect_messages(
111112
return target
112113

113114

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+
114165
# Reusable type definitions
115166
class OverwriteOptions(Enum):
116167
"""Options for overwriting properties"""
@@ -840,6 +891,9 @@ def _fetch_schema(
840891
# are not v1 compatible mainly by using update_model()
841892
content = re.sub(r"(,?\s*unique_items=True\s*)", "", content)
842893

894+
# fix unserializable defaults from datamodel-code-generator (#125)
895+
content = remove_unserializable_default_sentinels(content)
896+
843897
# Detect empty subclasses, replaces their occurrences with base classes,
844898
# and removes the empty class definitions.
845899
# Only processes subclasses that follow naming patterns:
@@ -1048,8 +1102,15 @@ def _fetch_schema(
10481102
content = black.format_str(content, mode=black.Mode())
10491103
# run isort to sort imports using Vertical Hanging Indent style
10501104
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)
10531114

10541115
with open(result_model_path, "w", encoding="utf-8") as f:
10551116
f.write(content)
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
"""Unit tests for the sentinel-cleanup and syntax-validation guards in osw.core.
2+
3+
Regression guard for #125: datamodel-code-generator uses a bare
4+
`UNDEFINED = object()` sentinel with `is` identity checks. oold's merge_deep
5+
deep-copies schema dicts during allOf composition, which clones that sentinel
6+
into a look-alike object, defeating the identity guard, so the generator
7+
repr's it into source (see oold.generator.Generator.generate() for oold's own
8+
workaround). _fetch_schema used to write that content straight to entity.py
9+
and reload it, raising SyntaxError and poisoning every later `import
10+
osw.core` in the process.
11+
12+
These tests exercise the extracted helpers directly, fully offline, and
13+
never call the real (wiki- and network-backed) `_fetch_schema`.
14+
"""
15+
16+
import ast
17+
18+
import pytest
19+
20+
from osw.core import ensure_valid_python_source, remove_unserializable_default_sentinels
21+
22+
23+
def test_sentinel_default_is_rewritten_to_valid_python():
24+
"""A repr'd sentinel object breaks ast.parse until the substitution runs."""
25+
bad = (
26+
"risk_assessment: RiskAssessmentProcess | None = Field("
27+
"default_factory=lambda :<object object at 0x000001A2B3C4D5E6>)\n"
28+
)
29+
with pytest.raises(SyntaxError):
30+
ast.parse(bad)
31+
32+
fixed = remove_unserializable_default_sentinels(bad)
33+
34+
assert "<object object at" not in fixed
35+
ast.parse(fixed) # must not raise
36+
37+
38+
def test_sentinel_wrapped_in_a_parse_obj_call_is_rewritten():
39+
"""The shape actually reported in #125, where the sentinel sits inside a
40+
`parse_obj(...)` call and the field carries further keyword arguments.
41+
42+
oold's own regex stops at the first `)` after the address, which is the
43+
one belonging to `parse_obj(`, and so leaves a dangling `)` behind.
44+
"""
45+
bad = (
46+
"risk_assessment: RiskAssessmentProcess | None = Field("
47+
"default_factory=lambda :RiskAssessmentProcess.parse_obj("
48+
"<object object at 0x000001A2B3C4D5E6>), options={'a': 1})\n"
49+
)
50+
with pytest.raises(SyntaxError):
51+
ast.parse(bad)
52+
53+
fixed = remove_unserializable_default_sentinels(bad)
54+
55+
assert "<object object at" not in fixed
56+
assert "options={'a': 1}" in fixed # trailing kwargs are preserved
57+
ast.parse(fixed) # must not raise
58+
59+
60+
def test_legitimate_default_factory_lambda_is_not_mangled():
61+
"""A normal `default_factory=lambda: uuid4()` must survive untouched."""
62+
legit = "id: UUID = Field(default_factory=lambda: uuid4())\n"
63+
64+
assert remove_unserializable_default_sentinels(legit) == legit
65+
66+
67+
def test_ensure_valid_python_source_accepts_valid_source():
68+
ensure_valid_python_source("class Foo:\n pass\n", "entity.py") # no raise
69+
70+
71+
def test_ensure_valid_python_source_raises_with_a_useful_message():
72+
bad = "class Foo(:\n pass\n"
73+
74+
with pytest.raises(SyntaxError) as exc_info:
75+
ensure_valid_python_source(bad, "entity.py")
76+
77+
message = str(exc_info.value)
78+
assert "entity.py" in message
79+
assert "line 1" in message
80+
assert "class Foo(:" in message
81+
82+
83+
def test_validation_failure_leaves_an_existing_target_untouched(tmp_path):
84+
"""Mirrors the guarded write in _fetch_schema: validation runs before the
85+
file is ever opened for writing, so a bad generation never touches the
86+
previous, valid content sitting at the target path.
87+
"""
88+
target = tmp_path / "entity.py"
89+
target.write_text("previous_valid_content = 1\n", encoding="utf-8")
90+
91+
bad = "class Foo(:\n pass\n"
92+
93+
with pytest.raises(SyntaxError):
94+
ensure_valid_python_source(bad, str(target))
95+
target.write_text("corrupted", encoding="utf-8") # never reached
96+
97+
assert target.read_text(encoding="utf-8") == "previous_valid_content = 1\n"

0 commit comments

Comments
 (0)