Skip to content

Commit bc4a909

Browse files
committed
Remove pydantic_kwargs, fix backward compatibility, fix tests
1 parent c2c8598 commit bc4a909

File tree

2 files changed

+52
-71
lines changed

2 files changed

+52
-71
lines changed

sqlmodel/main.py

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import inspect as inspect_module
44
import ipaddress
55
import uuid
6+
import warnings
67
import weakref
78
from datetime import date, datetime, time, timedelta
89
from decimal import Decimal
@@ -262,14 +263,11 @@ def Field(
262263
deprecated(
263264
"""
264265
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.
268267
"""
269268
),
270269
] = None,
271270
json_schema_extra: Optional[Dict[str, Any]] = None,
272-
pydantic_kwargs: Optional[Dict[str, Any]] = None,
273271
) -> Any: ...
274272

275273

@@ -320,14 +318,11 @@ def Field(
320318
deprecated(
321319
"""
322320
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.
326322
"""
327323
),
328324
] = None,
329325
json_schema_extra: Optional[Dict[str, Any]] = None,
330-
pydantic_kwargs: Optional[Dict[str, Any]] = None,
331326
) -> Any: ...
332327

333328

@@ -378,14 +373,11 @@ def Field(
378373
deprecated(
379374
"""
380375
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.
384377
"""
385378
),
386379
] = None,
387380
json_schema_extra: Optional[Dict[str, Any]] = None,
388-
pydantic_kwargs: Optional[Dict[str, Any]] = None,
389381
) -> Any: ...
390382

391383

@@ -434,38 +426,40 @@ def Field(
434426
deprecated(
435427
"""
436428
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.
440430
"""
441431
),
442432
] = None,
443433
json_schema_extra: Optional[Dict[str, Any]] = None,
444-
pydantic_kwargs: Optional[Dict[str, Any]] = None,
445434
) -> 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,
450441
)
451442

452-
current_pydantic_kwargs = pydantic_kwargs or {}
443+
field_info_kwargs = {}
453444
current_json_schema_extra = json_schema_extra or {}
454445
current_schema_extra = schema_extra or {}
455446

456447
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
457455
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
463458
else:
464459
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
466461
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)
469463

470464
field_info = FieldInfo(
471465
default,
@@ -502,7 +496,7 @@ def Field(
502496
sa_column=sa_column,
503497
sa_column_args=sa_column_args,
504498
sa_column_kwargs=sa_column_kwargs,
505-
**current_pydantic_kwargs,
499+
**field_info_kwargs,
506500
)
507501
post_init_field_info(field_info)
508502
return field_info

tests/test_field_pd_and_json_kwarrgs.py

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -21,46 +21,31 @@ class Item(SQLModel):
2121
assert name_schema["x-custom-key"] == "Important Data"
2222

2323

24-
def test_pydantic_kwargs_applied():
25-
"""test pydantic_kwargs is applied to the field"""
26-
27-
class User(SQLModel):
28-
user_name: str = Field(pydantic_kwargs={"validation_alias": "UserNameInInput"})
29-
30-
field_info = User.model_fields["user_name"]
31-
32-
assert field_info.validation_alias == "UserNameInInput"
33-
34-
data = {"UserNameInInput": "KimigaiiWuyi"}
35-
user = User.model_validate(data)
36-
assert user.user_name == "KimigaiiWuyi"
37-
38-
39-
def test_schema_extra_and_new_param_conflict():
40-
"""test that passing schema_extra and new params at the same time raises an error"""
24+
def test_schema_extra_and_new_param_conflict(caplog):
25+
"""
26+
Test that passing schema_extra and json_schema_extra at the same time produces
27+
a warning.
28+
"""
4129

42-
with pytest.raises(RuntimeError) as excinfo:
30+
with pytest.warns(DeprecationWarning, match="schema_extra parameter is deprecated"):
4331
Field(schema_extra={"legacy": 1}, json_schema_extra={"new": 2})
44-
assert "Passing schema_extra is not supported" in str(excinfo.value)
45-
46-
with pytest.raises(RuntimeError) as excinfo:
47-
Field(schema_extra={"legacy": 1}, pydantic_kwargs={"alias": "Alias"})
48-
assert "Passing schema_extra is not supported" in str(excinfo.value)
4932

5033

5134
def test_schema_extra_backward_compatibility():
5235
"""
5336
test that schema_extra is backward compatible with json_schema_extra
5437
"""
5538

56-
class LegacyItem(SQLModel):
57-
name: str = Field(
58-
schema_extra={
59-
"example": "Sword of Old",
60-
"x-custom-key": "Important Data",
61-
"serialization_alias": "id_test",
62-
}
63-
)
39+
with pytest.warns(DeprecationWarning, match="schema_extra parameter is deprecated"):
40+
41+
class LegacyItem(SQLModel):
42+
name: str = Field(
43+
schema_extra={
44+
"example": "Sword of Old",
45+
"x-custom-key": "Important Data",
46+
"serialization_alias": "id_test",
47+
}
48+
)
6449

6550
schema = LegacyItem.model_json_schema()
6651

@@ -76,16 +61,18 @@ class LegacyItem(SQLModel):
7661
def test_json_schema_extra_mix_in_schema_extra():
7762
"""test that json_schema_extra is applied when it is in schema_extra"""
7863

79-
class Item(SQLModel):
80-
name: str = Field(
81-
schema_extra={
82-
"json_schema_extra": {
83-
"example": "Sword of Power",
84-
"x-custom-key": "Important Data",
85-
},
86-
"serialization_alias": "id_test",
87-
}
88-
)
64+
with pytest.warns(DeprecationWarning, match="schema_extra parameter is deprecated"):
65+
66+
class Item(SQLModel):
67+
name: str = Field(
68+
schema_extra={
69+
"json_schema_extra": {
70+
"example": "Sword of Power",
71+
"x-custom-key": "Important Data",
72+
},
73+
"serialization_alias": "id_test",
74+
}
75+
)
8976

9077
schema = Item.model_json_schema()
9178

0 commit comments

Comments
 (0)