Skip to content

Commit e1328b6

Browse files
committed
Run lints
1 parent a902748 commit e1328b6

File tree

2 files changed

+45
-29
lines changed

2 files changed

+45
-29
lines changed

src/asyncapi_python_codegen/generators/messages.py

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -94,25 +94,32 @@ def load_schemas_from_file(file_path: Path) -> None:
9494
if schema_name not in all_schemas:
9595
# Check if this schema is itself a reference
9696
if isinstance(schema_def, dict) and "$ref" in schema_def:
97-
ref = schema_def["$ref"]
97+
ref_value: Any = schema_def["$ref"] # type: ignore[misc]
9898
# Resolve the reference using ParseContext utilities
99-
try:
100-
context = ParseContext(abs_path)
101-
target_context = context.resolve_reference(ref)
102-
103-
# Load and navigate to the referenced schema
104-
with target_context.filepath.open("r") as ref_file:
105-
ref_spec = yaml.safe_load(ref_file)
106-
107-
if target_context.json_pointer:
108-
resolved_schema = navigate_json_pointer(ref_spec, target_context.json_pointer)
109-
else:
110-
resolved_schema = ref_spec
111-
112-
all_schemas[schema_name] = resolved_schema
113-
except Exception as e:
114-
print(f"Warning: Could not resolve reference {ref} in {abs_path}: {e}")
115-
all_schemas[schema_name] = schema_def
99+
if isinstance(ref_value, str):
100+
try:
101+
context = ParseContext(abs_path)
102+
target_context = context.resolve_reference(
103+
ref_value
104+
)
105+
106+
# Load and navigate to the referenced schema
107+
with target_context.filepath.open("r") as ref_file:
108+
ref_spec = yaml.safe_load(ref_file)
109+
110+
if target_context.json_pointer:
111+
resolved_schema = navigate_json_pointer(
112+
ref_spec, target_context.json_pointer
113+
)
114+
else:
115+
resolved_schema = ref_spec
116+
117+
all_schemas[schema_name] = resolved_schema
118+
except Exception as e:
119+
print(
120+
f"Warning: Could not resolve reference {ref_value} in {abs_path}: {e}"
121+
)
122+
all_schemas[schema_name] = schema_def
116123
else:
117124
all_schemas[schema_name] = schema_def
118125

@@ -124,7 +131,9 @@ def load_schemas_from_file(file_path: Path) -> None:
124131
all_schemas[schema_name] = msg_def["payload"]
125132

126133
# Find and process all external file references
127-
self._find_and_process_refs(spec, abs_path.parent, load_schemas_from_file)
134+
self._find_and_process_refs(
135+
spec, abs_path.parent, load_schemas_from_file
136+
)
128137

129138
except Exception as e:
130139
print(f"Warning: Could not load component schemas from {abs_path}: {e}")
@@ -141,26 +150,27 @@ def _find_and_process_refs(
141150
if isinstance(data, dict):
142151
# Check if this is a reference
143152
if "$ref" in data:
144-
ref = data["$ref"]
145-
if isinstance(ref, str) and not ref.startswith("#"):
153+
ref_value: Any = data["$ref"] # type: ignore[misc]
154+
if isinstance(ref_value, str) and not ref_value.startswith("#"):
146155
# External reference - extract file path
147-
if "#" in ref:
148-
file_part = ref.split("#")[0]
156+
file_part: str
157+
if "#" in ref_value:
158+
file_part = ref_value.split("#")[0]
149159
else:
150-
file_part = ref
160+
file_part = ref_value
151161

152162
if file_part:
153163
# Resolve relative path
154164
ref_path = (base_dir / file_part).resolve()
155165
process_file(ref_path)
156166

157167
# Recurse into all dict values
158-
for value in data.values():
168+
for value in data.values(): # type: ignore[misc]
159169
self._find_and_process_refs(value, base_dir, process_file)
160170

161171
elif isinstance(data, list):
162172
# Recurse into all list items
163-
for item in data:
173+
for item in data: # type: ignore[misc]
164174
self._find_and_process_refs(item, base_dir, process_file)
165175

166176
def _resolve_references(self, schemas: dict[str, Any]) -> dict[str, Any]:

tests/codegen/test_parser.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,15 @@ def test_four_level_deep_recursion():
283283
# Without recursive file loading, we would only get Level1Schema
284284
# With recursive loading, we should get schemas from all 4 files
285285
assert "Level1Schema" in schemas, "Level1Schema from main file not found"
286-
assert "Level2Schema" in schemas, "Level2Schema from level2.yaml not found (recursive loading failed)"
287-
assert "Level3Schema" in schemas, "Level3Schema from level3.yaml not found (recursive loading failed)"
288-
assert "Level4Schema" in schemas, "Level4Schema from level4.yaml not found (recursive loading failed)"
286+
assert (
287+
"Level2Schema" in schemas
288+
), "Level2Schema from level2.yaml not found (recursive loading failed)"
289+
assert (
290+
"Level3Schema" in schemas
291+
), "Level3Schema from level3.yaml not found (recursive loading failed)"
292+
assert (
293+
"Level4Schema" in schemas
294+
), "Level4Schema from level4.yaml not found (recursive loading failed)"
289295
assert "DataMessage" in schemas, "DataMessage from level3.yaml not found"
290296

291297
# Verify the deepest level schema has correct structure

0 commit comments

Comments
 (0)