Skip to content

Commit 316ce65

Browse files
committed
fix: exclude comments in OSW-ID replacements
1 parent f09bbaa commit 316ce65

1 file changed

Lines changed: 12 additions & 10 deletions

File tree

‎src/osw/utils/code_postprocessing.py‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,18 @@ def resolve_osw_id_type_hints(content: str) -> str:
8989
if not osw_id_to_class:
9090
return content
9191

92-
# Find bare OSW IDs used as identifiers (not inside "Category:OSW..." strings)
92+
# Replace bare OSW IDs used as identifiers with the resolved class name.
93+
# Skip comment lines: the datamodel-codegen header
94+
# "# filename: OSW....json" contains an OSW ID that is NOT a type hint
95+
# and must stay intact (the lookbehind/lookahead alone do not exclude it,
96+
# since it is preceded by a space and followed by ".json").
9397
bare_osw_pattern = re.compile(r"(?<![:\w])OSW[0-9a-f]{32}(?!\w)")
94-
unresolved = set(bare_osw_pattern.findall(content))
9598

96-
for osw_id in unresolved:
97-
if osw_id in osw_id_to_class:
98-
content = re.sub(
99-
r"(?<![:\w])" + re.escape(osw_id) + r"(?!\w)",
100-
osw_id_to_class[osw_id],
101-
content,
102-
)
99+
def _resolve_line(line: str) -> str:
100+
if line.lstrip().startswith("#"):
101+
return line # never rewrite comments
102+
return bare_osw_pattern.sub(
103+
lambda m: osw_id_to_class.get(m.group(0), m.group(0)), line
104+
)
103105

104-
return content
106+
return "".join(_resolve_line(line) for line in content.splitlines(keepends=True))

0 commit comments

Comments
 (0)