Skip to content

Commit

Permalink
fix(models): mark unknown fields as set in pydantic v1
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot committed Nov 10, 2023
1 parent 79ff165 commit 954ec1b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/orb/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def construct(
if PYDANTIC_V2:
_extra[key] = value
else:
_fields_set.add(key)
fields_values[key] = value

object.__setattr__(m, "__dict__", fields_values)
Expand Down
12 changes: 6 additions & 6 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ class TestOrb:

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

response = self.client.post("/foo", cast_to=httpx.Response)
assert response.status_code == 200
assert isinstance(response, httpx.Response)
assert response.json() == '{"foo": "bar"}'
assert response.json() == {"foo": "bar"}

@pytest.mark.respx(base_url=base_url)
def test_raw_response_for_binary(self, respx_mock: MockRouter) -> None:
Expand All @@ -56,7 +56,7 @@ def test_raw_response_for_binary(self, respx_mock: MockRouter) -> None:
response = self.client.post("/foo", cast_to=httpx.Response)
assert response.status_code == 200
assert isinstance(response, httpx.Response)
assert response.json() == '{"foo": "bar"}'
assert response.json() == {"foo": "bar"}

def test_copy(self) -> None:
copied = self.client.copy()
Expand Down Expand Up @@ -583,12 +583,12 @@ class TestAsyncOrb:
@pytest.mark.respx(base_url=base_url)
@pytest.mark.asyncio
async def test_raw_response(self, respx_mock: MockRouter) -> None:
respx_mock.post("/foo").mock(return_value=httpx.Response(200, json='{"foo": "bar"}'))
respx_mock.post("/foo").mock(return_value=httpx.Response(200, json={"foo": "bar"}))

response = await self.client.post("/foo", cast_to=httpx.Response)
assert response.status_code == 200
assert isinstance(response, httpx.Response)
assert response.json() == '{"foo": "bar"}'
assert response.json() == {"foo": "bar"}

@pytest.mark.respx(base_url=base_url)
@pytest.mark.asyncio
Expand All @@ -600,7 +600,7 @@ async def test_raw_response_for_binary(self, respx_mock: MockRouter) -> None:
response = await self.client.post("/foo", cast_to=httpx.Response)
assert response.status_code == 200
assert isinstance(response, httpx.Response)
assert response.json() == '{"foo": "bar"}'
assert response.json() == {"foo": "bar"}

def test_copy(self) -> None:
copied = self.client.copy()
Expand Down
11 changes: 9 additions & 2 deletions tests/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pytest

from orb._utils import PropertyInfo, transform, parse_datetime
from orb._compat import PYDANTIC_V2
from orb._models import BaseModel


Expand Down Expand Up @@ -210,14 +211,20 @@ def test_pydantic_unknown_field() -> None:

def test_pydantic_mismatched_types() -> None:
model = MyModel.construct(foo=True)
with pytest.warns(UserWarning):
if PYDANTIC_V2:
with pytest.warns(UserWarning):
params = transform(model, Any)
else:
params = transform(model, Any)
assert params == {"foo": True}


def test_pydantic_mismatched_object_type() -> None:
model = MyModel.construct(foo=MyModel.construct(hello="world"))
with pytest.warns(UserWarning):
if PYDANTIC_V2:
with pytest.warns(UserWarning):
params = transform(model, Any)
else:
params = transform(model, Any)
assert params == {"foo": {"hello": "world"}}

Expand Down

0 comments on commit 954ec1b

Please sign in to comment.