Skip to content

Commit c8261c9

Browse files
fix(models): mark unknown fields as set in pydantic v1 (#179)
1 parent d06251d commit c8261c9

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

src/finch/_models.py

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ def construct(
121121
if PYDANTIC_V2:
122122
_extra[key] = value
123123
else:
124+
_fields_set.add(key)
124125
fields_values[key] = value
125126

126127
object.__setattr__(m, "__dict__", fields_values)

tests/test_client.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ class TestFinch:
4141

4242
@pytest.mark.respx(base_url=base_url)
4343
def test_raw_response(self, respx_mock: MockRouter) -> None:
44-
respx_mock.post("/foo").mock(return_value=httpx.Response(200, json='{"foo": "bar"}'))
44+
respx_mock.post("/foo").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
4545

4646
response = self.client.post("/foo", cast_to=httpx.Response)
4747
assert response.status_code == 200
4848
assert isinstance(response, httpx.Response)
49-
assert response.json() == '{"foo": "bar"}'
49+
assert response.json() == {"foo": "bar"}
5050

5151
@pytest.mark.respx(base_url=base_url)
5252
def test_raw_response_for_binary(self, respx_mock: MockRouter) -> None:
@@ -57,7 +57,7 @@ def test_raw_response_for_binary(self, respx_mock: MockRouter) -> None:
5757
response = self.client.post("/foo", cast_to=httpx.Response)
5858
assert response.status_code == 200
5959
assert isinstance(response, httpx.Response)
60-
assert response.json() == '{"foo": "bar"}'
60+
assert response.json() == {"foo": "bar"}
6161

6262
def test_copy(self) -> None:
6363
copied = self.client.copy()
@@ -683,12 +683,12 @@ class TestAsyncFinch:
683683
@pytest.mark.respx(base_url=base_url)
684684
@pytest.mark.asyncio
685685
async def test_raw_response(self, respx_mock: MockRouter) -> None:
686-
respx_mock.post("/foo").mock(return_value=httpx.Response(200, json='{"foo": "bar"}'))
686+
respx_mock.post("/foo").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
687687

688688
response = await self.client.post("/foo", cast_to=httpx.Response)
689689
assert response.status_code == 200
690690
assert isinstance(response, httpx.Response)
691-
assert response.json() == '{"foo": "bar"}'
691+
assert response.json() == {"foo": "bar"}
692692

693693
@pytest.mark.respx(base_url=base_url)
694694
@pytest.mark.asyncio
@@ -700,7 +700,7 @@ async def test_raw_response_for_binary(self, respx_mock: MockRouter) -> None:
700700
response = await self.client.post("/foo", cast_to=httpx.Response)
701701
assert response.status_code == 200
702702
assert isinstance(response, httpx.Response)
703-
assert response.json() == '{"foo": "bar"}'
703+
assert response.json() == {"foo": "bar"}
704704

705705
def test_copy(self) -> None:
706706
copied = self.client.copy()

tests/test_transform.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import pytest
88

99
from finch._utils import PropertyInfo, transform, parse_datetime
10+
from finch._compat import PYDANTIC_V2
1011
from finch._models import BaseModel
1112

1213

@@ -210,14 +211,20 @@ def test_pydantic_unknown_field() -> None:
210211

211212
def test_pydantic_mismatched_types() -> None:
212213
model = MyModel.construct(foo=True)
213-
with pytest.warns(UserWarning):
214+
if PYDANTIC_V2:
215+
with pytest.warns(UserWarning):
216+
params = transform(model, Any)
217+
else:
214218
params = transform(model, Any)
215219
assert params == {"foo": True}
216220

217221

218222
def test_pydantic_mismatched_object_type() -> None:
219223
model = MyModel.construct(foo=MyModel.construct(hello="world"))
220-
with pytest.warns(UserWarning):
224+
if PYDANTIC_V2:
225+
with pytest.warns(UserWarning):
226+
params = transform(model, Any)
227+
else:
221228
params = transform(model, Any)
222229
assert params == {"foo": {"hello": "world"}}
223230

0 commit comments

Comments
 (0)