Skip to content

Commit c04f379

Browse files
committed
fixing parameter type errors
1 parent 48eadc6 commit c04f379

File tree

1 file changed

+17
-27
lines changed

1 file changed

+17
-27
lines changed

packages/schema_wrapper/src/generate_schema_wrapper.py

+17-27
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,18 @@ def download_schemafile(
3434
return schemapath
3535

3636
def generate_class(class_name: str, class_schema: Dict[str, Any]) -> str:
37-
38-
# Define a list of primitive types
39-
# primitive_types = ['string', 'number', 'integer', 'boolean']
4037
class_name = get_valid_identifier(class_name)
4138

4239
# Check if the schema defines a simple type (like string, number) without properties
43-
if 'type' in class_schema and 'properties' not in class_schema: #DISCUSS: Change this to not isinstance(class_schema, Iterable)
44-
#assert not isinstance(class_schema, Iterable)
40+
if 'type' in class_schema and 'properties' not in class_schema:
4541
return f"class {class_name}:\n def __init__(self):\n pass\n"
4642

4743
# Check for '$ref' and handle it
4844
if '$ref' in class_schema:
4945
ref_class_name = class_schema['$ref'].split('/')[-1]
5046
return f"\nclass {class_name}:\n pass # This is a reference to '{ref_class_name}'\n"
51-
52-
5347
if 'anyOf' in class_schema:
54-
return generate_any_of_class(class_name, class_schema['anyOf'])
48+
return generate_any_of_class(class_name, class_schema['anyOf'])
5549

5650
# Extract properties and required fields
5751
properties = class_schema.get('properties', {})
@@ -64,32 +58,32 @@ def generate_class(class_name: str, class_schema: Dict[str, Any]) -> str:
6458
optional_params = []
6559

6660
# Ensuring all the property names are valid Python identifiers
67-
property_items = list(properties.items())
68-
for prop, prop_schema in property_items:
61+
valid_properties = {}
62+
for prop, prop_schema in properties.items():
6963
valid_prop = get_valid_identifier(prop)
70-
if valid_prop != prop:
71-
properties.pop(prop)
72-
properties[valid_prop] = prop_schema
64+
valid_properties[valid_prop] = prop_schema
7365

74-
for prop, prop_schema in properties.items():
75-
type_hint = get_type_hint(prop_schema)
66+
for prop, prop_schema in valid_properties.items():
67+
if 'anyOf' in prop_schema:
68+
# Handle anyOf case
69+
type_hint = f"Union[{', '.join(get_type_hint(item) for item in prop_schema['anyOf'])}]"
70+
else:
71+
type_hint = get_type_hint(prop_schema)
7672

7773
if prop in required:
7874
# Required parameters should not have default values
7975
class_def += f", {prop}: {type_hint}"
8076
else:
8177
# Ensure we add optional parameters last
82-
optional_params.append((prop, prop_schema))
78+
optional_params.append((prop, type_hint))
8379

84-
for prop, prop_schema in optional_params:
85-
# Optional parameters should have a default value of None
80+
for prop, type_hint in optional_params:
8681
class_def += f", {prop}: {type_hint} = None"
8782

88-
8983
class_def += "):\n"
9084

9185
# Generate attribute assignments in __init__
92-
for prop in properties:
86+
for prop in valid_properties:
9387
class_def += f" self.{prop} = {prop}\n"
9488

9589
return class_def
@@ -103,13 +97,12 @@ def get_type_union(types: List[str]):
10397
if "Any" in unique_types:
10498
unique_types.remove("Any")
10599
unique_types.append("Any")
106-
107100
return f'Union[{", ".join(unique_types)}]'
108101

109102
def generate_any_of_class(class_name: str, any_of_schemas: List[Dict[str, Any]]) -> str:
110103
types = [get_type_hint(schema) for schema in any_of_schemas]
111104
type_union = get_type_union(types)
112-
105+
113106
class_def = f"class {class_name}:\n"
114107
class_def += f" def __init__(self, value: {type_union}):\n"
115108
class_def += " self.value = value\n"
@@ -126,9 +119,9 @@ def get_type_hint(type_schema: Dict[str, Any]) -> str:
126119

127120
datatype = get_type_hint(items_schema)
128121
return f"List[{datatype}]"
129-
122+
130123
if 'type' in type_schema:
131-
if isinstance(type_schema['type'], Iterable):
124+
if isinstance(type_schema['type'], list):
132125
types = []
133126
for t in type_schema['type']:
134127
datatype = KNOWN_PRIMITIVES.get(t)
@@ -144,7 +137,6 @@ def get_type_hint(type_schema: Dict[str, Any]) -> str:
144137
return 'Any'
145138
return datatype
146139
elif 'anyOf' in type_schema:
147-
#print(f"anyOf to iterate: {type_schema}")
148140
types = [get_type_hint(option) for option in type_schema['anyOf']]
149141
return get_type_union(types)
150142
elif '$ref' in type_schema:
@@ -168,7 +160,6 @@ def generate_schema_wrapper(schema_file: Path, output_file: Path) -> str:
168160
# output_file.parent.mkdir(parents=True, exist_ok=True)
169161

170162
for name, schema in rootschema_definitions.items():
171-
#print(name)
172163
dependencies = get_dependencies(schema)
173164
if dependencies:
174165
ts.add(name, *dependencies)
@@ -186,7 +177,6 @@ def generate_schema_wrapper(schema_file: Path, output_file: Path) -> str:
186177

187178
generated_classes = "\n\n".join(definitions.values())
188179
generated_classes = "from typing import List, Dict, Any, Union\n\n" + generated_classes
189-
#print(generated_classes)
190180

191181
with open(output_file, 'w') as f:
192182
f.write(generated_classes)

0 commit comments

Comments
 (0)