|
10 | 10 | from pydantic.alias_generators import to_camel |
11 | 11 | from pydantic import Field |
12 | 12 |
|
13 | | - |
14 | | -# def model_fields(model: type[BaseModel]) -> dict[str, tuple[Any, Any]]: |
15 | | -# """Extract Pydantic BaseModel fields. |
16 | | - |
17 | | -# Args: |
18 | | -# model: (Type) The model object for which the fields will be extracted. |
19 | | - |
20 | | -# Returns: |
21 | | -# A dictionary containing the fields of the model along with |
22 | | -# their corresponding types and default values. |
23 | | - |
24 | | -# Example: |
25 | | -# >>> class MyModel(BaseModel): |
26 | | -# ... name: str |
27 | | -# ... age: int = 0 |
28 | | -# ... |
29 | | -# >>> model_fields(MyModel) |
30 | | -# {'name': (str, <default_value>), 'age': (int, 0)} |
31 | | -# """ |
32 | | -# annotations = get_type_hints(model) |
33 | | - |
34 | | -# fields = {} |
35 | | -# for field_name, field in model.model_fields.items(): |
36 | | -# fields[field_name] = (annotations[field_name], field) |
37 | | - |
38 | | -# return fields |
39 | | - |
40 | | -# def model_fields(model: type[BaseModel]) -> dict[str, tuple[Any, Any]]: |
41 | | -# """Return fields suitable for use in create_model with correct types and defaults.""" |
42 | | -# fields = {} |
43 | | -# for field_name, field_info in model.model_fields.items(): |
44 | | -# annotated_type = field_info.annotation |
45 | | -# default = field_info.default if field_info.default is not None else ... |
46 | | -# fields[field_name] = (annotated_type, Field(default, description=field_info.description)) |
47 | | -# return fields |
48 | | - |
49 | 13 | def model_fields(model: type[BaseModel]) -> dict[str, tuple[Any, Any]]: |
50 | | - """Safely extract fields for create_model, preserving optionality and default behavior.""" |
| 14 | + """Extract Pydantic BaseModel fields. |
| 15 | +
|
| 16 | + Args: |
| 17 | + model: (Type) The model object for which the fields will be extracted. |
| 18 | +
|
| 19 | + Returns: |
| 20 | + A dictionary containing the fields of the model along with |
| 21 | + their corresponding types and default values. |
| 22 | +
|
| 23 | + Example: |
| 24 | + >>> class MyModel(BaseModel): |
| 25 | + ... name: str |
| 26 | + ... age: int = 0 |
| 27 | + ... |
| 28 | + >>> model_fields(MyModel) |
| 29 | + {'name': (str, <default_value>), 'age': (int, 0)} |
| 30 | + """ |
51 | 31 | fields = {} |
52 | 32 | for field_name, field_info in model.model_fields.items(): |
53 | 33 | annotated_type = field_info.annotation |
|
0 commit comments