|
13 | 13 | from enum import IntEnum |
14 | 14 | from io import TextIOBase |
15 | 15 | from pathlib import Path |
16 | | -from typing import TYPE_CHECKING, Any, Optional, Union, cast |
| 16 | +from typing import TYPE_CHECKING, Any, ClassVar, Optional, Union, cast |
17 | 17 | from urllib.parse import ParseResult, urlparse |
18 | 18 |
|
19 | 19 | import argcomplete |
@@ -136,48 +136,6 @@ def validate_url(cls, value: Any) -> ParseResult | None: # noqa: N805 |
136 | 136 | msg = f"This protocol doesn't support only http/https. --input={value}" |
137 | 137 | raise Error(msg) # pragma: no cover |
138 | 138 |
|
139 | | - @model_validator() |
140 | | - def validate_original_field_name_delimiter(cls, values: dict[str, Any]) -> dict[str, Any]: # noqa: N805 |
141 | | - if values.get("original_field_name_delimiter") is not None and not values.get("snake_case_field"): |
142 | | - msg = "`--original-field-name-delimiter` can not be used without `--snake-case-field`." |
143 | | - raise Error(msg) |
144 | | - return values |
145 | | - |
146 | | - @model_validator() |
147 | | - def validate_custom_file_header(cls, values: dict[str, Any]) -> dict[str, Any]: # noqa: N805 |
148 | | - if values.get("custom_file_header") and values.get("custom_file_header_path"): |
149 | | - msg = "`--custom_file_header_path` can not be used with `--custom_file_header`." |
150 | | - raise Error(msg) # pragma: no cover |
151 | | - return values |
152 | | - |
153 | | - @model_validator() |
154 | | - def validate_keyword_only(cls, values: dict[str, Any]) -> dict[str, Any]: # noqa: N805 |
155 | | - output_model_type: DataModelType = values.get("output_model_type") # pyright: ignore[reportAssignmentType] |
156 | | - python_target: PythonVersion = values.get("target_python_version") # pyright: ignore[reportAssignmentType] |
157 | | - if ( |
158 | | - values.get("keyword_only") |
159 | | - and output_model_type == DataModelType.DataclassesDataclass |
160 | | - and not python_target.has_kw_only_dataclass |
161 | | - ): |
162 | | - msg = f"`--keyword-only` requires `--target-python-version` {PythonVersion.PY_310.value} or higher." |
163 | | - raise Error(msg) |
164 | | - return values |
165 | | - |
166 | | - @model_validator() |
167 | | - def validate_output_datetime_class(cls, values: dict[str, Any]) -> dict[str, Any]: # noqa: N805 |
168 | | - datetime_class_type: DatetimeClassType | None = values.get("output_datetime_class") |
169 | | - if ( |
170 | | - datetime_class_type |
171 | | - and datetime_class_type is not DatetimeClassType.Datetime |
172 | | - and values.get("output_model_type") == DataModelType.DataclassesDataclass |
173 | | - ): |
174 | | - msg = ( |
175 | | - '`--output-datetime-class` only allows "datetime" for ' |
176 | | - f"`--output-model-type` {DataModelType.DataclassesDataclass.value}" |
177 | | - ) |
178 | | - raise Error(msg) |
179 | | - return values |
180 | | - |
181 | 139 | # Pydantic 1.5.1 doesn't support each_item=True correctly |
182 | 140 | @field_validator("http_headers", mode="before") |
183 | 141 | def validate_http_headers(cls, value: Any) -> list[tuple[str, str]] | None: # noqa: N805 |
@@ -225,18 +183,104 @@ def validate_custom_formatters(cls, values: dict[str, Any]) -> dict[str, Any]: |
225 | 183 | values["custom_formatters"] = custom_formatters.split(",") |
226 | 184 | return values |
227 | 185 |
|
| 186 | + __validate_output_datetime_class_err: ClassVar[str] = ( |
| 187 | + '`--output-datetime-class` only allows "datetime" for ' |
| 188 | + f"`--output-model-type` {DataModelType.DataclassesDataclass.value}" |
| 189 | + ) |
| 190 | + |
| 191 | + __validate_original_field_name_delimiter_err: ClassVar[str] = ( |
| 192 | + "`--original-field-name-delimiter` can not be used without `--snake-case-field`." |
| 193 | + ) |
| 194 | + |
| 195 | + __validate_custom_file_header_err: ClassVar[str] = ( |
| 196 | + "`--custom_file_header_path` can not be used with `--custom_file_header`." |
| 197 | + ) |
| 198 | + __validate_keyword_only_err: ClassVar[str] = ( |
| 199 | + f"`--keyword-only` requires `--target-python-version` {PythonVersion.PY_310.value} or higher." |
| 200 | + ) |
| 201 | + |
228 | 202 | if PYDANTIC_V2: |
229 | 203 |
|
230 | 204 | @model_validator() # pyright: ignore[reportArgumentType] |
231 | | - def validate_root(self: Self) -> Self: |
| 205 | + def validate_output_datetime_class(self: Self) -> Self: # pyright: ignore[reportRedeclaration] |
| 206 | + datetime_class_type: DatetimeClassType | None = self.output_datetime_class |
| 207 | + if ( |
| 208 | + datetime_class_type |
| 209 | + and datetime_class_type is not DatetimeClassType.Datetime |
| 210 | + and self.output_model_type == DataModelType.DataclassesDataclass |
| 211 | + ): |
| 212 | + raise Error(self.__validate_output_datetime_class_err) |
| 213 | + return self |
| 214 | + |
| 215 | + @model_validator() # pyright: ignore[reportArgumentType] |
| 216 | + def validate_original_field_name_delimiter(self: Self) -> Self: # pyright: ignore[reportRedeclaration] |
| 217 | + if self.original_field_name_delimiter is not None and not self.snake_case_field: |
| 218 | + raise Error(self.__validate_original_field_name_delimiter_err) |
| 219 | + return self |
| 220 | + |
| 221 | + @model_validator() # pyright: ignore[reportArgumentType] |
| 222 | + def validate_custom_file_header(self: Self) -> Self: # pyright: ignore[reportRedeclaration] |
| 223 | + if self.custom_file_header and self.custom_file_header_path: |
| 224 | + raise Error(self.__validate_custom_file_header_err) |
| 225 | + return self |
| 226 | + |
| 227 | + @model_validator() # pyright: ignore[reportArgumentType] |
| 228 | + def validate_keyword_only(self: Self) -> Self: # pyright: ignore[reportRedeclaration] |
| 229 | + output_model_type: DataModelType = self.output_model_type |
| 230 | + python_target: PythonVersion = self.target_python_version |
| 231 | + if ( |
| 232 | + self.keyword_only |
| 233 | + and output_model_type == DataModelType.DataclassesDataclass |
| 234 | + and not python_target.has_kw_only_dataclass |
| 235 | + ): |
| 236 | + raise Error(self.__validate_keyword_only_err) |
| 237 | + return self |
| 238 | + |
| 239 | + @model_validator() # pyright: ignore[reportArgumentType] |
| 240 | + def validate_root(self: Self) -> Self: # pyright: ignore[reportRedeclaration] |
232 | 241 | if self.use_annotated: |
233 | 242 | self.field_constraints = True |
234 | 243 | return self |
235 | 244 |
|
236 | 245 | else: |
237 | 246 |
|
238 | | - @model_validator() |
239 | | - def validate_root(cls, values: Any) -> Any: # noqa: N805 |
| 247 | + @model_validator() # pyright: ignore[reportArgumentType] |
| 248 | + def validate_output_datetime_class(cls, values: dict[str, Any]) -> dict[str, Any]: # noqa: N805 |
| 249 | + datetime_class_type: DatetimeClassType | None = values.get("output_datetime_class") |
| 250 | + if ( |
| 251 | + datetime_class_type |
| 252 | + and datetime_class_type is not DatetimeClassType.Datetime |
| 253 | + and values.get("output_model_type") == DataModelType.DataclassesDataclass |
| 254 | + ): |
| 255 | + raise Error(cls.__validate_output_datetime_class_err) |
| 256 | + return values |
| 257 | + |
| 258 | + @model_validator() # pyright: ignore[reportArgumentType] |
| 259 | + def validate_original_field_name_delimiter(cls, values: dict[str, Any]) -> dict[str, Any]: # noqa: N805 |
| 260 | + if values.get("original_field_name_delimiter") is not None and not values.get("snake_case_field"): |
| 261 | + raise Error(cls.__validate_original_field_name_delimiter_err) |
| 262 | + return values |
| 263 | + |
| 264 | + @model_validator() # pyright: ignore[reportArgumentType] |
| 265 | + def validate_custom_file_header(cls, values: dict[str, Any]) -> dict[str, Any]: # noqa: N805 |
| 266 | + if values.get("custom_file_header") and values.get("custom_file_header_path"): |
| 267 | + raise Error(cls.__validate_custom_file_header_err) |
| 268 | + return values |
| 269 | + |
| 270 | + @model_validator() # pyright: ignore[reportArgumentType] |
| 271 | + def validate_keyword_only(cls, values: dict[str, Any]) -> dict[str, Any]: # noqa: N805 |
| 272 | + output_model_type: DataModelType = cast("DataModelType", values.get("output_model_type")) |
| 273 | + python_target: PythonVersion = cast("PythonVersion", values.get("target_python_version")) |
| 274 | + if ( |
| 275 | + values.get("keyword_only") |
| 276 | + and output_model_type == DataModelType.DataclassesDataclass |
| 277 | + and not python_target.has_kw_only_dataclass |
| 278 | + ): |
| 279 | + raise Error(cls.__validate_keyword_only_err) |
| 280 | + return values |
| 281 | + |
| 282 | + @model_validator() # pyright: ignore[reportArgumentType] |
| 283 | + def validate_root(cls, values: dict[str, Any]) -> dict[str, Any]: # noqa: N805 |
240 | 284 | if values.get("use_annotated"): |
241 | 285 | values["field_constraints"] = True |
242 | 286 | return values |
|
0 commit comments