| 81 | |
| 82 | |
| 83 | class BaseModel(pydantic.BaseModel): |
| 84 | if PYDANTIC_V2: |
| 85 | model_config: ClassVar[ConfigDict] = ConfigDict( |
| 86 | extra="allow", defer_build=coerce_boolean(os.environ.get("DEFER_PYDANTIC_BUILD", "true")) |
| 87 | ) |
| 88 | else: |
| 89 | |
| 90 | @property |
| 91 | @override |
| 92 | def model_fields_set(self) -> set[str]: |
| 93 | # a forwards-compat shim for pydantic v2 |
| 94 | return self.__fields_set__ # type: ignore |
| 95 | |
| 96 | class Config(pydantic.BaseConfig): # pyright: ignore[reportDeprecated] |
| 97 | extra: Any = pydantic.Extra.allow # type: ignore |
| 98 | |
| 99 | def to_dict( |
| 100 | self, |
| 101 | *, |
| 102 | mode: Literal["json", "python"] = "python", |
| 103 | use_api_names: bool = True, |
| 104 | exclude_unset: bool = True, |
| 105 | exclude_defaults: bool = False, |
| 106 | exclude_none: bool = False, |
| 107 | warnings: bool = True, |
| 108 | ) -> dict[str, object]: |
| 109 | """Recursively generate a dictionary representation of the model, optionally specifying which fields to include or exclude. |
| 110 | |
| 111 | By default, fields that were not set by the API will not be included, |
| 112 | and keys will match the API response, *not* the property names from the model. |
| 113 | |
| 114 | For example, if the API responds with `"fooBar": true` but we've defined a `foo_bar: bool` property, |
| 115 | the output will use the `"fooBar"` key (unless `use_api_names=False` is passed). |
| 116 | |
| 117 | Args: |
| 118 | mode: |
| 119 | If mode is 'json', the dictionary will only contain JSON serializable types. e.g. `datetime` will be turned into a string, `"2024-3-22T18:11:19.117000Z"`. |
| 120 | If mode is 'python', the dictionary may contain any Python objects. e.g. `datetime(2024, 3, 22)` |
| 121 | |
| 122 | use_api_names: Whether to use the key that the API responded with or the property name. Defaults to `True`. |
| 123 | exclude_unset: Whether to exclude fields that have not been explicitly set. |
| 124 | exclude_defaults: Whether to exclude fields that are set to their default value from the output. |
| 125 | exclude_none: Whether to exclude fields that have a value of `None` from the output. |
| 126 | warnings: Whether to log warnings when invalid fields are encountered. This is only supported in Pydantic v2. |
| 127 | """ |
| 128 | return self.model_dump( |
| 129 | mode=mode, |
| 130 | by_alias=use_api_names, |
| 131 | exclude_unset=exclude_unset, |
| 132 | exclude_defaults=exclude_defaults, |
| 133 | exclude_none=exclude_none, |
| 134 | warnings=warnings, |
| 135 | ) |
| 136 | |
| 137 | def to_json( |
| 138 | self, |
| 139 | *, |
| 140 | indent: int | None = 2, |
nothing calls this directly
no test coverage detected