Skip to content

Commit ed73d1d

Browse files
chore(internal): bump pydantic dependency (#1413)
1 parent 906ece0 commit ed73d1d

File tree

5 files changed

+40
-26
lines changed

5 files changed

+40
-26
lines changed

requirements-dev.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ attrs==23.1.0
2424
azure-core==1.30.1
2525
# via azure-identity
2626
azure-identity==1.15.0
27-
black==24.4.0
27+
black==24.4.2
2828
# via inline-snapshot
2929
certifi==2023.7.22
3030
# via httpcore
@@ -39,7 +39,7 @@ click==8.1.7
3939
# via inline-snapshot
4040
colorlog==6.7.0
4141
# via nox
42-
cryptography==42.0.5
42+
cryptography==42.0.7
4343
# via azure-identity
4444
# via msal
4545
# via pyjwt
@@ -111,9 +111,9 @@ py==1.11.0
111111
# via pytest
112112
pycparser==2.22
113113
# via cffi
114-
pydantic==2.4.2
114+
pydantic==2.7.1
115115
# via openai
116-
pydantic-core==2.10.1
116+
pydantic-core==2.18.2
117117
# via pydantic
118118
pyjwt==2.8.0
119119
# via msal

requirements.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ numpy==1.26.4
3333
# via openai
3434
# via pandas
3535
# via pandas-stubs
36-
pandas==2.2.1
36+
pandas==2.2.2
3737
# via openai
3838
pandas-stubs==2.2.1.240316
3939
# via openai
40-
pydantic==2.4.2
40+
pydantic==2.7.1
4141
# via openai
42-
pydantic-core==2.10.1
42+
pydantic-core==2.18.2
4343
# via pydantic
4444
python-dateutil==2.9.0.post0
4545
# via pandas
@@ -53,7 +53,7 @@ sniffio==1.3.0
5353
# via openai
5454
tqdm==4.66.1
5555
# via openai
56-
types-pytz==2024.1.0.20240203
56+
types-pytz==2024.1.0.20240417
5757
# via pandas-stubs
5858
typing-extensions==4.8.0
5959
# via openai

src/openai/_models.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
from ._constants import RAW_RESPONSE_HEADER
6363

6464
if TYPE_CHECKING:
65-
from pydantic_core.core_schema import ModelField, ModelFieldsSchema
65+
from pydantic_core.core_schema import ModelField, LiteralSchema, ModelFieldsSchema
6666

6767
__all__ = ["BaseModel", "GenericModel"]
6868

@@ -251,7 +251,9 @@ def model_dump(
251251
exclude_defaults: bool = False,
252252
exclude_none: bool = False,
253253
round_trip: bool = False,
254-
warnings: bool = True,
254+
warnings: bool | Literal["none", "warn", "error"] = True,
255+
context: dict[str, Any] | None = None,
256+
serialize_as_any: bool = False,
255257
) -> dict[str, Any]:
256258
"""Usage docs: https://docs.pydantic.dev/2.4/concepts/serialization/#modelmodel_dump
257259
@@ -279,6 +281,10 @@ def model_dump(
279281
raise ValueError("round_trip is only supported in Pydantic v2")
280282
if warnings != True:
281283
raise ValueError("warnings is only supported in Pydantic v2")
284+
if context is not None:
285+
raise ValueError("context is only supported in Pydantic v2")
286+
if serialize_as_any != False:
287+
raise ValueError("serialize_as_any is only supported in Pydantic v2")
282288
return super().dict( # pyright: ignore[reportDeprecated]
283289
include=include,
284290
exclude=exclude,
@@ -300,7 +306,9 @@ def model_dump_json(
300306
exclude_defaults: bool = False,
301307
exclude_none: bool = False,
302308
round_trip: bool = False,
303-
warnings: bool = True,
309+
warnings: bool | Literal["none", "warn", "error"] = True,
310+
context: dict[str, Any] | None = None,
311+
serialize_as_any: bool = False,
304312
) -> str:
305313
"""Usage docs: https://docs.pydantic.dev/2.4/concepts/serialization/#modelmodel_dump_json
306314
@@ -324,6 +332,10 @@ def model_dump_json(
324332
raise ValueError("round_trip is only supported in Pydantic v2")
325333
if warnings != True:
326334
raise ValueError("warnings is only supported in Pydantic v2")
335+
if context is not None:
336+
raise ValueError("context is only supported in Pydantic v2")
337+
if serialize_as_any != False:
338+
raise ValueError("serialize_as_any is only supported in Pydantic v2")
327339
return super().json( # type: ignore[reportDeprecated]
328340
indent=indent,
329341
include=include,
@@ -550,7 +562,7 @@ def _build_discriminated_union_meta(*, union: type, meta_annotations: tuple[Any,
550562
field_schema = field["schema"]
551563

552564
if field_schema["type"] == "literal":
553-
for entry in field_schema["expected"]:
565+
for entry in cast("LiteralSchema", field_schema)["expected"]:
554566
if isinstance(entry, str):
555567
mapping[entry] = variant
556568
else:

tests/test_models.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class NestedModel(BaseModel):
3131

3232
# mismatched types
3333
m = NestedModel.construct(nested="hello!")
34-
assert m.nested == "hello!"
34+
assert cast(Any, m.nested) == "hello!"
3535

3636

3737
def test_optional_nested_model() -> None:
@@ -48,7 +48,7 @@ class NestedModel(BaseModel):
4848
# mismatched types
4949
m3 = NestedModel.construct(nested={"foo"})
5050
assert isinstance(cast(Any, m3.nested), set)
51-
assert m3.nested == {"foo"}
51+
assert cast(Any, m3.nested) == {"foo"}
5252

5353

5454
def test_list_nested_model() -> None:
@@ -323,7 +323,7 @@ class Model(BaseModel):
323323
assert len(m.items) == 2
324324
assert isinstance(m.items[0], Submodel1)
325325
assert m.items[0].level == -1
326-
assert m.items[1] == 156
326+
assert cast(Any, m.items[1]) == 156
327327

328328

329329
def test_union_of_lists() -> None:
@@ -355,7 +355,7 @@ class Model(BaseModel):
355355
assert len(m.items) == 2
356356
assert isinstance(m.items[0], SubModel1)
357357
assert m.items[0].level == -1
358-
assert m.items[1] == 156
358+
assert cast(Any, m.items[1]) == 156
359359

360360

361361
def test_dict_of_union() -> None:

tests/test_transform.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -260,20 +260,22 @@ class MyModel(BaseModel):
260260
@parametrize
261261
@pytest.mark.asyncio
262262
async def test_pydantic_model_to_dictionary(use_async: bool) -> None:
263-
assert await transform(MyModel(foo="hi!"), Any, use_async) == {"foo": "hi!"}
264-
assert await transform(MyModel.construct(foo="hi!"), Any, use_async) == {"foo": "hi!"}
263+
assert cast(Any, await transform(MyModel(foo="hi!"), Any, use_async)) == {"foo": "hi!"}
264+
assert cast(Any, await transform(MyModel.construct(foo="hi!"), Any, use_async)) == {"foo": "hi!"}
265265

266266

267267
@parametrize
268268
@pytest.mark.asyncio
269269
async def test_pydantic_empty_model(use_async: bool) -> None:
270-
assert await transform(MyModel.construct(), Any, use_async) == {}
270+
assert cast(Any, await transform(MyModel.construct(), Any, use_async)) == {}
271271

272272

273273
@parametrize
274274
@pytest.mark.asyncio
275275
async def test_pydantic_unknown_field(use_async: bool) -> None:
276-
assert await transform(MyModel.construct(my_untyped_field=True), Any, use_async) == {"my_untyped_field": True}
276+
assert cast(Any, await transform(MyModel.construct(my_untyped_field=True), Any, use_async)) == {
277+
"my_untyped_field": True
278+
}
277279

278280

279281
@parametrize
@@ -285,7 +287,7 @@ async def test_pydantic_mismatched_types(use_async: bool) -> None:
285287
params = await transform(model, Any, use_async)
286288
else:
287289
params = await transform(model, Any, use_async)
288-
assert params == {"foo": True}
290+
assert cast(Any, params) == {"foo": True}
289291

290292

291293
@parametrize
@@ -297,7 +299,7 @@ async def test_pydantic_mismatched_object_type(use_async: bool) -> None:
297299
params = await transform(model, Any, use_async)
298300
else:
299301
params = await transform(model, Any, use_async)
300-
assert params == {"foo": {"hello": "world"}}
302+
assert cast(Any, params) == {"foo": {"hello": "world"}}
301303

302304

303305
class ModelNestedObjects(BaseModel):
@@ -309,7 +311,7 @@ class ModelNestedObjects(BaseModel):
309311
async def test_pydantic_nested_objects(use_async: bool) -> None:
310312
model = ModelNestedObjects.construct(nested={"foo": "stainless"})
311313
assert isinstance(model.nested, MyModel)
312-
assert await transform(model, Any, use_async) == {"nested": {"foo": "stainless"}}
314+
assert cast(Any, await transform(model, Any, use_async)) == {"nested": {"foo": "stainless"}}
313315

314316

315317
class ModelWithDefaultField(BaseModel):
@@ -325,19 +327,19 @@ async def test_pydantic_default_field(use_async: bool) -> None:
325327
model = ModelWithDefaultField.construct()
326328
assert model.with_none_default is None
327329
assert model.with_str_default == "foo"
328-
assert await transform(model, Any, use_async) == {}
330+
assert cast(Any, await transform(model, Any, use_async)) == {}
329331

330332
# should be included when the default value is explicitly given
331333
model = ModelWithDefaultField.construct(with_none_default=None, with_str_default="foo")
332334
assert model.with_none_default is None
333335
assert model.with_str_default == "foo"
334-
assert await transform(model, Any, use_async) == {"with_none_default": None, "with_str_default": "foo"}
336+
assert cast(Any, await transform(model, Any, use_async)) == {"with_none_default": None, "with_str_default": "foo"}
335337

336338
# should be included when a non-default value is explicitly given
337339
model = ModelWithDefaultField.construct(with_none_default="bar", with_str_default="baz")
338340
assert model.with_none_default == "bar"
339341
assert model.with_str_default == "baz"
340-
assert await transform(model, Any, use_async) == {"with_none_default": "bar", "with_str_default": "baz"}
342+
assert cast(Any, await transform(model, Any, use_async)) == {"with_none_default": "bar", "with_str_default": "baz"}
341343

342344

343345
class TypedDictIterableUnion(TypedDict):

0 commit comments

Comments
 (0)