MCPcopy Create free account
hub / github.com/anomalyco/opencode-sdk-python / construct

Method construct

src/opencode_ai/_models.py:181–238  ·  view source on GitHub ↗
(  # pyright: ignore[reportIncompatibleMethodOverride]
        __cls: Type[ModelT],
        _fields_set: set[str] | None = None,
        **values: object,
    )

Source from the content-addressed store, hash-verified

179 @classmethod
180 @override
181 def construct( # pyright: ignore[reportIncompatibleMethodOverride]
182 __cls: Type[ModelT],
183 _fields_set: set[str] | None = None,
184 **values: object,
185 ) -> ModelT:
186 m = __cls.__new__(__cls)
187 fields_values: dict[str, object] = {}
188
189 config = get_model_config(__cls)
190 populate_by_name = (
191 config.allow_population_by_field_name
192 if isinstance(config, _ConfigProtocol)
193 else config.get("populate_by_name")
194 )
195
196 if _fields_set is None:
197 _fields_set = set()
198
199 model_fields = get_model_fields(__cls)
200 for name, field in model_fields.items():
201 key = field.alias
202 if key is None or (key not in values and populate_by_name):
203 key = name
204
205 if key in values:
206 fields_values[name] = _construct_field(value=values[key], field=field, key=key)
207 _fields_set.add(name)
208 else:
209 fields_values[name] = field_get_default(field)
210
211 extra_field_type = _get_extra_fields_type(__cls)
212
213 _extra = {}
214 for key, value in values.items():
215 if key not in model_fields:
216 parsed = construct_type(value=value, type_=extra_field_type) if extra_field_type is not None else value
217
218 if PYDANTIC_V2:
219 _extra[key] = parsed
220 else:
221 _fields_set.add(key)
222 fields_values[key] = parsed
223
224 object.__setattr__(m, "__dict__", fields_values)
225
226 if PYDANTIC_V2:
227 # these properties are copied from Pydantic's `model_construct()` method
228 object.__setattr__(m, "__pydantic_private__", None)
229 object.__setattr__(m, "__pydantic_extra__", _extra)
230 object.__setattr__(m, "__pydantic_fields_set__", _fields_set)
231 else:
232 # init_private_attributes() does not exist in v2
233 m._init_private_attributes() # type: ignore
234
235 # copied from Pydantic v1's `construct()` method
236 object.__setattr__(m, "__fields_set__", _fields_set)
237
238 return m

Callers 15

construct_typeFunction · 0.45
constructMethod · 0.45
getMethod · 0.45
postMethod · 0.45
patchMethod · 0.45
putMethod · 0.45
deleteMethod · 0.45
get_api_listMethod · 0.45
getMethod · 0.45
postMethod · 0.45
patchMethod · 0.45
putMethod · 0.45

Calls 7

get_model_configFunction · 0.85
get_model_fieldsFunction · 0.85
_construct_fieldFunction · 0.85
field_get_defaultFunction · 0.85
_get_extra_fields_typeFunction · 0.85
construct_typeFunction · 0.85
getMethod · 0.45

Tested by 15

test_basicFunction · 0.36
test_list_nested_modelFunction · 0.36
test_raw_dictionaryFunction · 0.36
test_unknown_fieldsFunction · 0.36
test_aliasesFunction · 0.36
test_optional_listFunction · 0.36