From 26dc12308a289f74c822b7506ed95c714aa8590a Mon Sep 17 00:00:00 2001 From: Stainless Bot Date: Fri, 3 Nov 2023 22:16:27 +0000 Subject: [PATCH] chore(internal): remove unused int/float conversion --- src/finch/_models.py | 15 ++++++--------- src/finch/pagination.py | 6 ++++-- tests/test_models.py | 29 ++++++++++++++++++++--------- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/finch/_models.py b/src/finch/_models.py index 40245ac9..00d787ca 100644 --- a/src/finch/_models.py +++ b/src/finch/_models.py @@ -313,16 +313,13 @@ def construct_type(*, value: object, type_: type) -> object: return [construct_type(value=entry, type_=inner_type) for entry in value] if origin == float: - try: - return float(cast(Any, value)) - except Exception: - return value + if isinstance(value, int): + coerced = float(value) + if coerced != value: + return value + return coerced - if origin == int: - try: - return int(cast(Any, value)) - except Exception: - return value + return value if type_ == datetime: try: diff --git a/src/finch/pagination.py b/src/finch/pagination.py index 8162b275..3cb9ed96 100644 --- a/src/finch/pagination.py +++ b/src/finch/pagination.py @@ -41,9 +41,10 @@ def next_page_info(self) -> None: @classmethod def build(cls: Type[_BaseModelT], *, response: Response, data: object) -> _BaseModelT: # noqa: ARG003 return cls.construct( + None, **{ **(cast(Mapping[str, Any], data) if is_mapping(data) else {"items": data}), - } + }, ) @@ -65,9 +66,10 @@ def next_page_info(self) -> None: @classmethod def build(cls: Type[_BaseModelT], *, response: Response, data: object) -> _BaseModelT: # noqa: ARG003 return cls.construct( + None, **{ **(cast(Mapping[str, Any], data) if is_mapping(data) else {"items": data}), - } + }, ) diff --git a/tests/test_models.py b/tests/test_models.py index e9d5cac4..ecdf47a1 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -439,21 +439,32 @@ class Model(BaseModel): assert model_json(model) == expected_json -def test_coerces_int() -> None: +def test_does_not_coerce_int() -> None: class Model(BaseModel): bar: int assert Model.construct(bar=1).bar == 1 - assert Model.construct(bar=10.9).bar == 10 - assert Model.construct(bar="19").bar == 19 - assert Model.construct(bar=False).bar == 0 + assert Model.construct(bar=10.9).bar == 10.9 + assert Model.construct(bar="19").bar == "19" # type: ignore[comparison-overlap] + assert Model.construct(bar=False).bar is False - # TODO: support this - # assert Model.construct(bar="True").bar == 1 - # mismatched types are left as-is - m = Model.construct(bar={"foo": "bar"}) - assert m.bar == {"foo": "bar"} # type: ignore[comparison-overlap] +def test_int_to_float_safe_conversion() -> None: + class Model(BaseModel): + float_field: float + + m = Model.construct(float_field=10) + assert m.float_field == 10.0 + assert isinstance(m.float_field, float) + + m = Model.construct(float_field=10.12) + assert m.float_field == 10.12 + assert isinstance(m.float_field, float) + + # number too big + m = Model.construct(float_field=2**53 + 1) + assert m.float_field == 2**53 + 1 + assert isinstance(m.float_field, int) def test_deprecated_alias() -> None: