|
3 | 3 | import inspect as inspect_module |
4 | 4 | import ipaddress |
5 | 5 | import uuid |
| 6 | +import warnings |
6 | 7 | import weakref |
7 | 8 | from datetime import date, datetime, time, timedelta |
8 | 9 | from decimal import Decimal |
@@ -262,14 +263,11 @@ def Field( |
262 | 263 | deprecated( |
263 | 264 | """ |
264 | 265 | This parameter is deprecated. |
265 | | - Use `json_schema_extra` to add extra information to the JSON schema. |
266 | | - Use `pydantic_kwargs` to pass additional parameters to `Field` that are not |
267 | | - part of this interface, but accepted by Pydantic's Field. |
| 266 | + Use `json_schema_extra` to add extra information to JSON schema. |
268 | 267 | """ |
269 | 268 | ), |
270 | 269 | ] = None, |
271 | 270 | json_schema_extra: Optional[Dict[str, Any]] = None, |
272 | | - pydantic_kwargs: Optional[Dict[str, Any]] = None, |
273 | 271 | ) -> Any: ... |
274 | 272 |
|
275 | 273 |
|
@@ -320,14 +318,11 @@ def Field( |
320 | 318 | deprecated( |
321 | 319 | """ |
322 | 320 | This parameter is deprecated. |
323 | | - Use `json_schema_extra` to add extra information to the JSON schema. |
324 | | - Use `pydantic_kwargs` to pass additional parameters to `Field` that are not |
325 | | - part of this interface, but accepted by Pydantic's Field. |
| 321 | + Use `json_schema_extra` to add extra information to JSON schema. |
326 | 322 | """ |
327 | 323 | ), |
328 | 324 | ] = None, |
329 | 325 | json_schema_extra: Optional[Dict[str, Any]] = None, |
330 | | - pydantic_kwargs: Optional[Dict[str, Any]] = None, |
331 | 326 | ) -> Any: ... |
332 | 327 |
|
333 | 328 |
|
@@ -378,14 +373,11 @@ def Field( |
378 | 373 | deprecated( |
379 | 374 | """ |
380 | 375 | This parameter is deprecated. |
381 | | - Use `json_schema_extra` to add extra information to the JSON schema. |
382 | | - Use `pydantic_kwargs` to pass additional parameters to `Field` that are not |
383 | | - part of this interface, but accepted by Pydantic's Field. |
| 376 | + Use `json_schema_extra` to add extra information to JSON schema. |
384 | 377 | """ |
385 | 378 | ), |
386 | 379 | ] = None, |
387 | 380 | json_schema_extra: Optional[Dict[str, Any]] = None, |
388 | | - pydantic_kwargs: Optional[Dict[str, Any]] = None, |
389 | 381 | ) -> Any: ... |
390 | 382 |
|
391 | 383 |
|
@@ -434,38 +426,40 @@ def Field( |
434 | 426 | deprecated( |
435 | 427 | """ |
436 | 428 | This parameter is deprecated. |
437 | | - Use `json_schema_extra` to add extra information to the JSON schema. |
438 | | - Use `pydantic_kwargs` to pass additional parameters to `Field` that are not |
439 | | - part of this interface, but accepted by Pydantic's Field. |
| 429 | + Use `json_schema_extra` to add extra information to JSON schema. |
440 | 430 | """ |
441 | 431 | ), |
442 | 432 | ] = None, |
443 | 433 | json_schema_extra: Optional[Dict[str, Any]] = None, |
444 | | - pydantic_kwargs: Optional[Dict[str, Any]] = None, |
445 | 434 | ) -> Any: |
446 | | - if schema_extra and (json_schema_extra or pydantic_kwargs): |
447 | | - raise RuntimeError( |
448 | | - "Passing schema_extra is not supported when " |
449 | | - "also passing a json_schema_extra or pydantic_kwargs" |
| 435 | + if schema_extra: |
| 436 | + warnings.warn( |
| 437 | + "schema_extra parameter is deprecated. " |
| 438 | + "Use json_schema_extra to add extra information to JSON schema.", |
| 439 | + DeprecationWarning, |
| 440 | + stacklevel=1, |
450 | 441 | ) |
451 | 442 |
|
452 | | - current_pydantic_kwargs = pydantic_kwargs or {} |
| 443 | + field_info_kwargs = {} |
453 | 444 | current_json_schema_extra = json_schema_extra or {} |
454 | 445 | current_schema_extra = schema_extra or {} |
455 | 446 |
|
456 | 447 | if IS_PYDANTIC_V2: |
| 448 | + # Handle a workaround when json_schema_extra was passed via schema_extra |
| 449 | + if "json_schema_extra" in current_schema_extra: |
| 450 | + from_schema_extra = current_schema_extra.pop("json_schema_extra") |
| 451 | + current_json_schema_extra = ( |
| 452 | + current_json_schema_extra or from_schema_extra |
| 453 | + ) |
| 454 | + # Split parameters from schema_extra to field_info_kwargs and json_schema_extra |
457 | 455 | for key, value in current_schema_extra.items(): |
458 | | - # if schema_extra={"json_schema_extra": {"x-yy-zz": "zz"}} |
459 | | - if key == "json_schema_extra": |
460 | | - current_json_schema_extra.update(value) |
461 | | - elif key in FIELD_ACCEPTED_KWARGS: |
462 | | - current_pydantic_kwargs[key] = value |
| 456 | + if key in FIELD_ACCEPTED_KWARGS: |
| 457 | + field_info_kwargs[key] = value |
463 | 458 | else: |
464 | 459 | current_json_schema_extra[key] = value |
465 | | - current_pydantic_kwargs["json_schema_extra"] = current_json_schema_extra |
| 460 | + field_info_kwargs["json_schema_extra"] = current_json_schema_extra |
466 | 461 | else: |
467 | | - current_pydantic_kwargs.update(current_json_schema_extra) |
468 | | - current_pydantic_kwargs.update(current_schema_extra) |
| 462 | + field_info_kwargs.update(current_json_schema_extra or current_schema_extra) |
469 | 463 |
|
470 | 464 | field_info = FieldInfo( |
471 | 465 | default, |
@@ -502,7 +496,7 @@ def Field( |
502 | 496 | sa_column=sa_column, |
503 | 497 | sa_column_args=sa_column_args, |
504 | 498 | sa_column_kwargs=sa_column_kwargs, |
505 | | - **current_pydantic_kwargs, |
| 499 | + **field_info_kwargs, |
506 | 500 | ) |
507 | 501 | post_init_field_info(field_info) |
508 | 502 | return field_info |
|
0 commit comments