Skip to content

release: 0.16.1 #305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "0.16.0"
".": "0.16.1"
}
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## 0.16.1 (2024-03-04)

Full Changelog: [v0.16.0...v0.16.1](https://github.com/Finch-API/finch-api-python/compare/v0.16.0...v0.16.1)

### Chores

* **internal:** split up transforms into sync / async ([#304](https://github.com/Finch-API/finch-api-python/issues/304)) ([665c4de](https://github.com/Finch-API/finch-api-python/commit/665c4de271703a90b063ce955992d1c74fcca832))
* **internal:** support more input types ([#306](https://github.com/Finch-API/finch-api-python/issues/306)) ([5c3f635](https://github.com/Finch-API/finch-api-python/commit/5c3f6355d8595517b16f2da8e82a71dea7a0e130))

## 0.16.0 (2024-03-01)

Full Changelog: [v0.15.0...v0.16.0](https://github.com/Finch-API/finch-api-python/compare/v0.15.0...v0.16.0)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "finch-api"
version = "0.16.0"
version = "0.16.1"
description = "The official Python library for the Finch API"
readme = "README.md"
license = "Apache-2.0"
Expand Down
5 changes: 5 additions & 0 deletions src/finch/_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@
FileContent,
RequestFiles,
HttpxFileTypes,
Base64FileInput,
HttpxFileContent,
HttpxRequestFiles,
)
from ._utils import is_tuple_t, is_mapping_t, is_sequence_t


def is_base64_file_input(obj: object) -> TypeGuard[Base64FileInput]:
return isinstance(obj, io.IOBase) or isinstance(obj, os.PathLike)


def is_file_content(obj: object) -> TypeGuard[FileContent]:
return (
isinstance(obj, bytes) or isinstance(obj, tuple) or isinstance(obj, io.IOBase) or isinstance(obj, os.PathLike)
Expand Down
2 changes: 2 additions & 0 deletions src/finch/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@
ProxiesDict = Dict["str | URL", Union[None, str, URL, Proxy]]
ProxiesTypes = Union[str, Proxy, ProxiesDict]
if TYPE_CHECKING:
Base64FileInput = Union[IO[bytes], PathLike[str]]
FileContent = Union[IO[bytes], bytes, PathLike[str]]
else:
Base64FileInput = Union[IO[bytes], PathLike]
FileContent = Union[IO[bytes], bytes, PathLike] # PathLike is not subscriptable in Python 3.8.
FileTypes = Union[
# file (or bytes)
Expand Down
2 changes: 2 additions & 0 deletions src/finch/_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,7 @@
from ._transform import (
PropertyInfo as PropertyInfo,
transform as transform,
async_transform as async_transform,
maybe_transform as maybe_transform,
async_maybe_transform as async_maybe_transform,
)
167 changes: 161 additions & 6 deletions src/finch/_utils/_transform.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
from __future__ import annotations

import io
import base64
import pathlib
from typing import Any, Mapping, TypeVar, cast
from datetime import date, datetime
from typing_extensions import Literal, get_args, override, get_type_hints

import anyio
import pydantic

from ._utils import (
is_list,
is_mapping,
is_iterable,
)
from .._files import is_base64_file_input
from ._typing import (
is_list_type,
is_union_type,
Expand All @@ -29,7 +34,7 @@
# TODO: ensure works correctly with forward references in all cases


PropertyFormat = Literal["iso8601", "custom"]
PropertyFormat = Literal["iso8601", "base64", "custom"]


class PropertyInfo:
Expand Down Expand Up @@ -180,11 +185,7 @@ def _transform_recursive(
if isinstance(data, pydantic.BaseModel):
return model_dump(data, exclude_unset=True)

return _transform_value(data, annotation)


def _transform_value(data: object, type_: type) -> object:
annotated_type = _get_annotated_type(type_)
annotated_type = _get_annotated_type(annotation)
if annotated_type is None:
return data

Expand All @@ -205,6 +206,22 @@ def _format_data(data: object, format_: PropertyFormat, format_template: str | N
if format_ == "custom" and format_template is not None:
return data.strftime(format_template)

if format_ == "base64" and is_base64_file_input(data):
binary: str | bytes | None = None

if isinstance(data, pathlib.Path):
binary = data.read_bytes()
elif isinstance(data, io.IOBase):
binary = data.read()

if isinstance(binary, str): # type: ignore[unreachable]
binary = binary.encode()

if not isinstance(binary, bytes):
raise RuntimeError(f"Could not read bytes from {data}; Received {type(binary)}")

return base64.b64encode(binary).decode("ascii")

return data


Expand All @@ -222,3 +239,141 @@ def _transform_typeddict(
else:
result[_maybe_transform_key(key, type_)] = _transform_recursive(value, annotation=type_)
return result


async def async_maybe_transform(
data: object,
expected_type: object,
) -> Any | None:
"""Wrapper over `async_transform()` that allows `None` to be passed.

See `async_transform()` for more details.
"""
if data is None:
return None
return await async_transform(data, expected_type)


async def async_transform(
data: _T,
expected_type: object,
) -> _T:
"""Transform dictionaries based off of type information from the given type, for example:

```py
class Params(TypedDict, total=False):
card_id: Required[Annotated[str, PropertyInfo(alias="cardID")]]


transformed = transform({"card_id": "<my card ID>"}, Params)
# {'cardID': '<my card ID>'}
```

Any keys / data that does not have type information given will be included as is.

It should be noted that the transformations that this function does are not represented in the type system.
"""
transformed = await _async_transform_recursive(data, annotation=cast(type, expected_type))
return cast(_T, transformed)


async def _async_transform_recursive(
data: object,
*,
annotation: type,
inner_type: type | None = None,
) -> object:
"""Transform the given data against the expected type.

Args:
annotation: The direct type annotation given to the particular piece of data.
This may or may not be wrapped in metadata types, e.g. `Required[T]`, `Annotated[T, ...]` etc

inner_type: If applicable, this is the "inside" type. This is useful in certain cases where the outside type
is a container type such as `List[T]`. In that case `inner_type` should be set to `T` so that each entry in
the list can be transformed using the metadata from the container type.

Defaults to the same value as the `annotation` argument.
"""
if inner_type is None:
inner_type = annotation

stripped_type = strip_annotated_type(inner_type)
if is_typeddict(stripped_type) and is_mapping(data):
return await _async_transform_typeddict(data, stripped_type)

if (
# List[T]
(is_list_type(stripped_type) and is_list(data))
# Iterable[T]
or (is_iterable_type(stripped_type) and is_iterable(data) and not isinstance(data, str))
):
inner_type = extract_type_arg(stripped_type, 0)
return [await _async_transform_recursive(d, annotation=annotation, inner_type=inner_type) for d in data]

if is_union_type(stripped_type):
# For union types we run the transformation against all subtypes to ensure that everything is transformed.
#
# TODO: there may be edge cases where the same normalized field name will transform to two different names
# in different subtypes.
for subtype in get_args(stripped_type):
data = await _async_transform_recursive(data, annotation=annotation, inner_type=subtype)
return data

if isinstance(data, pydantic.BaseModel):
return model_dump(data, exclude_unset=True)

annotated_type = _get_annotated_type(annotation)
if annotated_type is None:
return data

# ignore the first argument as it is the actual type
annotations = get_args(annotated_type)[1:]
for annotation in annotations:
if isinstance(annotation, PropertyInfo) and annotation.format is not None:
return await _async_format_data(data, annotation.format, annotation.format_template)

return data


async def _async_format_data(data: object, format_: PropertyFormat, format_template: str | None) -> object:
if isinstance(data, (date, datetime)):
if format_ == "iso8601":
return data.isoformat()

if format_ == "custom" and format_template is not None:
return data.strftime(format_template)

if format_ == "base64" and is_base64_file_input(data):
binary: str | bytes | None = None

if isinstance(data, pathlib.Path):
binary = await anyio.Path(data).read_bytes()
elif isinstance(data, io.IOBase):
binary = data.read()

if isinstance(binary, str): # type: ignore[unreachable]
binary = binary.encode()

if not isinstance(binary, bytes):
raise RuntimeError(f"Could not read bytes from {data}; Received {type(binary)}")

return base64.b64encode(binary).decode("ascii")

return data


async def _async_transform_typeddict(
data: Mapping[str, object],
expected_type: type,
) -> Mapping[str, object]:
result: dict[str, object] = {}
annotations = get_type_hints(expected_type, include_extras=True)
for key, value in data.items():
type_ = annotations.get(key)
if type_ is None:
# we do not have a type annotation for this field, leave it as is
result[key] = value
else:
result[_maybe_transform_key(key, type_)] = await _async_transform_recursive(value, annotation=type_)
return result
2 changes: 1 addition & 1 deletion src/finch/_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless.

__title__ = "finch"
__version__ = "0.16.0" # x-release-please-version
__version__ = "0.16.1" # x-release-please-version
9 changes: 6 additions & 3 deletions src/finch/resources/hris/benefits/benefits.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

from .... import _legacy_response
from ...._types import NOT_GIVEN, Body, Query, Headers, NotGiven
from ...._utils import maybe_transform
from ...._utils import (
maybe_transform,
async_maybe_transform,
)
from ...._compat import cached_property
from .individuals import (
Individuals,
Expand Down Expand Up @@ -267,7 +270,7 @@ async def create(
"""
return await self._post(
"/employer/benefits",
body=maybe_transform(
body=await async_maybe_transform(
{
"description": description,
"frequency": frequency,
Expand Down Expand Up @@ -348,7 +351,7 @@ async def update(
raise ValueError(f"Expected a non-empty value for `benefit_id` but received {benefit_id!r}")
return await self._post(
f"/employer/benefits/{benefit_id}",
body=maybe_transform({"description": description}, benefit_update_params.BenefitUpdateParams),
body=await async_maybe_transform({"description": description}, benefit_update_params.BenefitUpdateParams),
options=make_request_options(
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
),
Expand Down
7 changes: 5 additions & 2 deletions src/finch/resources/jobs/automated.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

from ... import _legacy_response
from ..._types import NOT_GIVEN, Body, Query, Headers, NotGiven
from ..._utils import maybe_transform
from ..._utils import (
maybe_transform,
async_maybe_transform,
)
from ..._compat import cached_property
from ..._resource import SyncAPIResource, AsyncAPIResource
from ..._response import to_streamed_response_wrapper, async_to_streamed_response_wrapper
Expand Down Expand Up @@ -205,7 +208,7 @@ async def create(
"""
return await self._post(
"/jobs/automated",
body=maybe_transform({"type": type}, automated_create_params.AutomatedCreateParams),
body=await async_maybe_transform({"type": type}, automated_create_params.AutomatedCreateParams),
options=make_request_options(
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
),
Expand Down
7 changes: 5 additions & 2 deletions src/finch/resources/request_forwarding.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
from .. import _legacy_response
from ..types import RequestForwardingForwardResponse, request_forwarding_forward_params
from .._types import NOT_GIVEN, Body, Query, Headers, NotGiven
from .._utils import maybe_transform
from .._utils import (
maybe_transform,
async_maybe_transform,
)
from .._compat import cached_property
from .._resource import SyncAPIResource, AsyncAPIResource
from .._response import to_streamed_response_wrapper, async_to_streamed_response_wrapper
Expand Down Expand Up @@ -155,7 +158,7 @@ async def forward(
"""
return await self._post(
"/forward",
body=maybe_transform(
body=await async_maybe_transform(
{
"method": method,
"route": route,
Expand Down
7 changes: 5 additions & 2 deletions src/finch/resources/sandbox/company.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
from ... import _legacy_response
from ...types import LocationParam
from ..._types import NOT_GIVEN, Body, Query, Headers, NotGiven
from ..._utils import maybe_transform
from ..._utils import (
maybe_transform,
async_maybe_transform,
)
from ..._compat import cached_property
from ..._resource import SyncAPIResource, AsyncAPIResource
from ..._response import to_streamed_response_wrapper, async_to_streamed_response_wrapper
Expand Down Expand Up @@ -151,7 +154,7 @@ async def update(
"""
return await self._put(
"/sandbox/company",
body=maybe_transform(
body=await async_maybe_transform(
{
"accounts": accounts,
"departments": departments,
Expand Down
Loading