diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 43283d9..80834ef 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,7 +21,7 @@ jobs: runs-on: ${{ github.repository == 'stainless-sdks/knock-python' && 'depot-ubuntu-24.04' || 'ubuntu-latest' }} if: (github.event_name == 'push' || github.event.pull_request.head.repo.fork) && (github.event_name != 'push' || github.event.head_commit.message != 'codegen metadata') steps: - - uses: actions/checkout@v6 + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 - name: Install Rye run: | @@ -46,7 +46,7 @@ jobs: id-token: write runs-on: ${{ github.repository == 'stainless-sdks/knock-python' && 'depot-ubuntu-24.04' || 'ubuntu-latest' }} steps: - - uses: actions/checkout@v6 + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 - name: Install Rye run: | @@ -67,7 +67,7 @@ jobs: github.repository == 'stainless-sdks/knock-python' && !startsWith(github.ref, 'refs/heads/stl/') id: github-oidc - uses: actions/github-script@v8 + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 with: script: core.setOutput('github_token', await core.getIDToken()); @@ -87,7 +87,7 @@ jobs: runs-on: ${{ github.repository == 'stainless-sdks/knock-python' && 'depot-ubuntu-24.04' || 'ubuntu-latest' }} if: github.event_name == 'push' || github.event.pull_request.head.repo.fork steps: - - uses: actions/checkout@v6 + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 - name: Install Rye run: | diff --git a/.github/workflows/publish-pypi.yml b/.github/workflows/publish-pypi.yml index 02565d3..3b46ace 100644 --- a/.github/workflows/publish-pypi.yml +++ b/.github/workflows/publish-pypi.yml @@ -14,7 +14,7 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v6 + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 - name: Install Rye run: | diff --git a/.github/workflows/release-doctor.yml b/.github/workflows/release-doctor.yml index 7c384aa..0ae2ffd 100644 --- a/.github/workflows/release-doctor.yml +++ b/.github/workflows/release-doctor.yml @@ -12,7 +12,7 @@ jobs: if: github.repository == 'knocklabs/knock-python' && (github.event_name == 'push' || github.event_name == 'workflow_dispatch' || startsWith(github.head_ref, 'release-please') || github.head_ref == 'next') steps: - - uses: actions/checkout@v6 + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 - name: Check release environment run: | diff --git a/.release-please-manifest.json b/.release-please-manifest.json index 0c0c0c3..f3dbfd2 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -1,3 +1,3 @@ { - ".": "1.25.0" + ".": "1.26.0" } \ No newline at end of file diff --git a/.stats.yml b/.stats.yml index 86d162e..a18062d 100644 --- a/.stats.yml +++ b/.stats.yml @@ -1,4 +1,4 @@ configured_endpoints: 94 -openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/knock%2Fknock-3e8f3a4664d48b3d546339018b451a356f8e20c223a2d21e7c3824fad4cddc7b.yml -openapi_spec_hash: c2b6637451a63e39c1f1042c6a7cc7f7 +openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/knock/knock-1081b58ad42a969e5cd51e7c80d7e13fa5bf4973cbf1dfebbef4772caed7be80.yml +openapi_spec_hash: 58773164753990875fff9468a1f16429 config_hash: 625db64572b7ee0ee1dd00546e53fc5f diff --git a/CHANGELOG.md b/CHANGELOG.md index ef3d5ea..6bf1a5d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,28 @@ # Changelog +## 1.26.0 (2026-05-14) + +Full Changelog: [v1.25.0...v1.26.0](https://github.com/knocklabs/knock-python/compare/v1.25.0...v1.26.0) + +### Features + +* **api:** api update ([39e6add](https://github.com/knocklabs/knock-python/commit/39e6add4ed39335399c26b1c87968bf68709330f)) +* **api:** api update ([0c514bf](https://github.com/knocklabs/knock-python/commit/0c514bf839ee8134227869b00cf52fb07deeeae6)) +* **api:** api update ([c3f2f4a](https://github.com/knocklabs/knock-python/commit/c3f2f4a2e93139c627d3745ea4eb7f5142f3153b)) +* **internal/types:** support eagerly validating pydantic iterators ([b128ac3](https://github.com/knocklabs/knock-python/commit/b128ac31c39b01c758eabda4003373c513f59138)) +* support setting headers via env ([e22462b](https://github.com/knocklabs/knock-python/commit/e22462bd64af8c7ddaa8e84c76aca28e411d9734)) + + +### Bug Fixes + +* **client:** add missing f-string prefix in file type error message ([04b3154](https://github.com/knocklabs/knock-python/commit/04b3154865a445887eb791b9c94d2c3586eadf3a)) +* use correct field name format for multipart file arrays ([4d33037](https://github.com/knocklabs/knock-python/commit/4d33037881f97e2b3e99892b31af059ae22c4bb8)) + + +### Chores + +* **internal:** reformat pyproject.toml ([8e64806](https://github.com/knocklabs/knock-python/commit/8e6480650f67a83f923f45485ff2f0d79e719d0b)) + ## 1.25.0 (2026-04-23) Full Changelog: [v1.24.1...v1.25.0](https://github.com/knocklabs/knock-python/compare/v1.24.1...v1.25.0) diff --git a/pyproject.toml b/pyproject.toml index e23661b..d1819d1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "knockapi" -version = "1.25.0" +version = "1.26.0" description = "The official Python library for the knock API" dynamic = ["readme"] license = "Apache-2.0" @@ -168,7 +168,7 @@ show_error_codes = true # # We also exclude our `tests` as mypy doesn't always infer # types correctly and Pyright will still catch any type errors. -exclude = ['src/knockapi/_files.py', '_dev/.*.py', 'tests/.*'] +exclude = ["src/knockapi/_files.py", "_dev/.*.py", "tests/.*"] strict_equality = true implicit_reexport = true diff --git a/src/knockapi/_client.py b/src/knockapi/_client.py index 6f3e90b..4549136 100644 --- a/src/knockapi/_client.py +++ b/src/knockapi/_client.py @@ -19,7 +19,11 @@ RequestOptions, not_given, ) -from ._utils import is_given, get_async_library +from ._utils import ( + is_given, + is_mapping_t, + get_async_library, +) from ._compat import cached_property from ._version import __version__ from ._streaming import Stream as Stream, AsyncStream as AsyncStream @@ -113,6 +117,15 @@ def __init__( if base_url is None: base_url = f"https://api.knock.app" + custom_headers_env = os.environ.get("KNOCK_CUSTOM_HEADERS") + if custom_headers_env is not None: + parsed: dict[str, str] = {} + for line in custom_headers_env.split("\n"): + colon = line.find(":") + if colon >= 0: + parsed[line[:colon].strip()] = line[colon + 1 :].strip() + default_headers = {**parsed, **(default_headers if is_mapping_t(default_headers) else {})} + super().__init__( version=__version__, base_url=base_url, @@ -388,6 +401,15 @@ def __init__( if base_url is None: base_url = f"https://api.knock.app" + custom_headers_env = os.environ.get("KNOCK_CUSTOM_HEADERS") + if custom_headers_env is not None: + parsed: dict[str, str] = {} + for line in custom_headers_env.split("\n"): + colon = line.find(":") + if colon >= 0: + parsed[line[:colon].strip()] = line[colon + 1 :].strip() + default_headers = {**parsed, **(default_headers if is_mapping_t(default_headers) else {})} + super().__init__( version=__version__, base_url=base_url, diff --git a/src/knockapi/_files.py b/src/knockapi/_files.py index 0fdce17..76da9e0 100644 --- a/src/knockapi/_files.py +++ b/src/knockapi/_files.py @@ -99,7 +99,7 @@ async def async_to_httpx_files(files: RequestFiles | None) -> HttpxRequestFiles elif is_sequence_t(files): files = [(key, await _async_transform_file(file)) for key, file in files] else: - raise TypeError("Unexpected file type input {type(files)}, expected mapping or sequence") + raise TypeError(f"Unexpected file type input {type(files)}, expected mapping or sequence") return files diff --git a/src/knockapi/_models.py b/src/knockapi/_models.py index 29070e0..8c5ab26 100644 --- a/src/knockapi/_models.py +++ b/src/knockapi/_models.py @@ -25,7 +25,9 @@ ClassVar, Protocol, Required, + Annotated, ParamSpec, + TypeAlias, TypedDict, TypeGuard, final, @@ -79,7 +81,15 @@ from ._constants import RAW_RESPONSE_HEADER if TYPE_CHECKING: + from pydantic import GetCoreSchemaHandler, ValidatorFunctionWrapHandler + from pydantic_core import CoreSchema, core_schema from pydantic_core.core_schema import ModelField, ModelSchema, LiteralSchema, ModelFieldsSchema +else: + try: + from pydantic_core import CoreSchema, core_schema + except ImportError: + CoreSchema = None + core_schema = None __all__ = ["BaseModel", "GenericModel"] @@ -396,6 +406,76 @@ def model_dump_json( ) +class _EagerIterable(list[_T], Generic[_T]): + """ + Accepts any Iterable[T] input (including generators), consumes it + eagerly, and validates all items upfront. + + Validation preserves the original container type where possible + (e.g. a set[T] stays a set[T]). Serialization (model_dump / JSON) + always emits a list — round-tripping through model_dump() will not + restore the original container type. + """ + + @classmethod + def __get_pydantic_core_schema__( + cls, + source_type: Any, + handler: GetCoreSchemaHandler, + ) -> CoreSchema: + (item_type,) = get_args(source_type) or (Any,) + item_schema: CoreSchema = handler.generate_schema(item_type) + list_of_items_schema: CoreSchema = core_schema.list_schema(item_schema) + + return core_schema.no_info_wrap_validator_function( + cls._validate, + list_of_items_schema, + serialization=core_schema.plain_serializer_function_ser_schema( + cls._serialize, + info_arg=False, + ), + ) + + @staticmethod + def _validate(v: Iterable[_T], handler: "ValidatorFunctionWrapHandler") -> Any: + original_type: type[Any] = type(v) + + # Normalize to list so list_schema can validate each item + if isinstance(v, list): + items: list[_T] = v + else: + try: + items = list(v) + except TypeError as e: + raise TypeError("Value is not iterable") from e + + # Validate items against the inner schema + validated: list[_T] = handler(items) + + # Reconstruct original container type + if original_type is list: + return validated + # str(list) produces the list's repr, not a string built from items, + # so skip reconstruction for str and its subclasses. + if issubclass(original_type, str): + return validated + try: + return original_type(validated) + except (TypeError, ValueError): + # If the type cannot be reconstructed, just return the validated list + return validated + + @staticmethod + def _serialize(v: Iterable[_T]) -> list[_T]: + """Always serialize as a list so Pydantic's JSON encoder is happy.""" + if isinstance(v, list): + return v + return list(v) + + +EagerIterable: TypeAlias = Annotated[Iterable[_T], _EagerIterable] + + def _construct_field(value: object, field: FieldInfo, key: str) -> object: if value is None: return field_get_default(field) diff --git a/src/knockapi/_qs.py b/src/knockapi/_qs.py index de8c99b..4127c19 100644 --- a/src/knockapi/_qs.py +++ b/src/knockapi/_qs.py @@ -2,17 +2,13 @@ from typing import Any, List, Tuple, Union, Mapping, TypeVar from urllib.parse import parse_qs, urlencode -from typing_extensions import Literal, get_args +from typing_extensions import get_args -from ._types import NotGiven, not_given +from ._types import NotGiven, ArrayFormat, NestedFormat, not_given from ._utils import flatten _T = TypeVar("_T") - -ArrayFormat = Literal["comma", "repeat", "indices", "brackets"] -NestedFormat = Literal["dots", "brackets"] - PrimitiveData = Union[str, int, float, bool, None] # this should be Data = Union[PrimitiveData, "List[Data]", "Tuple[Data]", "Mapping[str, Data]"] # https://github.com/microsoft/pyright/issues/3555 diff --git a/src/knockapi/_types.py b/src/knockapi/_types.py index 8059ac4..6e24b33 100644 --- a/src/knockapi/_types.py +++ b/src/knockapi/_types.py @@ -47,6 +47,9 @@ ModelT = TypeVar("ModelT", bound=pydantic.BaseModel) _T = TypeVar("_T") +ArrayFormat = Literal["comma", "repeat", "indices", "brackets"] +NestedFormat = Literal["dots", "brackets"] + # Approximates httpx internal ProxiesTypes and RequestFiles types # while adding support for `PathLike` instances diff --git a/src/knockapi/_utils/_utils.py b/src/knockapi/_utils/_utils.py index 771859f..199cd23 100644 --- a/src/knockapi/_utils/_utils.py +++ b/src/knockapi/_utils/_utils.py @@ -17,11 +17,11 @@ ) from pathlib import Path from datetime import date, datetime -from typing_extensions import TypeGuard +from typing_extensions import TypeGuard, get_args import sniffio -from .._types import Omit, NotGiven, FileTypes, HeadersLike +from .._types import Omit, NotGiven, FileTypes, ArrayFormat, HeadersLike _T = TypeVar("_T") _TupleT = TypeVar("_TupleT", bound=Tuple[object, ...]) @@ -40,25 +40,45 @@ def extract_files( query: Mapping[str, object], *, paths: Sequence[Sequence[str]], + array_format: ArrayFormat = "brackets", ) -> list[tuple[str, FileTypes]]: """Recursively extract files from the given dictionary based on specified paths. A path may look like this ['foo', 'files', '', 'data']. + ``array_format`` controls how ```` segments contribute to the emitted + field name. Supported values: ``"brackets"`` (``foo[]``), ``"repeat"`` and + ``"comma"`` (``foo``), ``"indices"`` (``foo[0]``, ``foo[1]``). + Note: this mutates the given dictionary. """ files: list[tuple[str, FileTypes]] = [] for path in paths: - files.extend(_extract_items(query, path, index=0, flattened_key=None)) + files.extend(_extract_items(query, path, index=0, flattened_key=None, array_format=array_format)) return files +def _array_suffix(array_format: ArrayFormat, array_index: int) -> str: + if array_format == "brackets": + return "[]" + if array_format == "indices": + return f"[{array_index}]" + if array_format == "repeat" or array_format == "comma": + # Both repeat the bare field name for each file part; there is no + # meaningful way to comma-join binary parts. + return "" + raise NotImplementedError( + f"Unknown array_format value: {array_format}, choose from {', '.join(get_args(ArrayFormat))}" + ) + + def _extract_items( obj: object, path: Sequence[str], *, index: int, flattened_key: str | None, + array_format: ArrayFormat, ) -> list[tuple[str, FileTypes]]: try: key = path[index] @@ -75,9 +95,11 @@ def _extract_items( if is_list(obj): files: list[tuple[str, FileTypes]] = [] - for entry in obj: - assert_is_file_content(entry, key=flattened_key + "[]" if flattened_key else "") - files.append((flattened_key + "[]", cast(FileTypes, entry))) + for array_index, entry in enumerate(obj): + suffix = _array_suffix(array_format, array_index) + emitted_key = (flattened_key + suffix) if flattened_key else suffix + assert_is_file_content(entry, key=emitted_key) + files.append((emitted_key, cast(FileTypes, entry))) return files assert_is_file_content(obj, key=flattened_key) @@ -106,6 +128,7 @@ def _extract_items( path, index=index, flattened_key=flattened_key, + array_format=array_format, ) elif is_list(obj): if key != "": @@ -117,9 +140,12 @@ def _extract_items( item, path, index=index, - flattened_key=flattened_key + "[]" if flattened_key is not None else "[]", + flattened_key=( + (flattened_key if flattened_key is not None else "") + _array_suffix(array_format, array_index) + ), + array_format=array_format, ) - for item in obj + for array_index, item in enumerate(obj) ] ) diff --git a/src/knockapi/_version.py b/src/knockapi/_version.py index 083c80a..e1ed972 100644 --- a/src/knockapi/_version.py +++ b/src/knockapi/_version.py @@ -1,4 +1,4 @@ # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. __title__ = "knockapi" -__version__ = "1.25.0" # x-release-please-version +__version__ = "1.26.0" # x-release-please-version diff --git a/src/knockapi/resources/workflows.py b/src/knockapi/resources/workflows.py index 6080eb8..8706094 100644 --- a/src/knockapi/resources/workflows.py +++ b/src/knockapi/resources/workflows.py @@ -70,11 +70,11 @@ def cancel( pair. Can optionally be provided one or more recipients to scope the request to. Args: - cancellation_key: An optional key that is used to reference a specific workflow trigger request - when issuing a [workflow cancellation](/send-notifications/canceling-workflows) - request. Must be provided while triggering a workflow in order to enable - subsequent cancellation. Should be unique across trigger requests to avoid - unintentional cancellations. + cancellation_key: A key that is used to reference a specific workflow trigger request when issuing + a [workflow cancellation](/send-notifications/canceling-workflows) request. Must + be provided while triggering a workflow in order to enable subsequent + cancellation. Should be unique across trigger requests to avoid unintentional + cancellations. recipients: A list of recipients to cancel the notification for. If omitted, cancels for all recipients associated with the cancellation key. @@ -143,11 +143,11 @@ def trigger( (string), an inline user request (object), or an inline object request, which is determined by the presence of a `collection` property. - cancellation_key: An optional key that is used to reference a specific workflow trigger request - when issuing a [workflow cancellation](/send-notifications/canceling-workflows) - request. Must be provided while triggering a workflow in order to enable - subsequent cancellation. Should be unique across trigger requests to avoid - unintentional cancellations. + cancellation_key: A key that is used to reference a specific workflow trigger request when issuing + a [workflow cancellation](/send-notifications/canceling-workflows) request. Must + be provided while triggering a workflow in order to enable subsequent + cancellation. Should be unique across trigger requests to avoid unintentional + cancellations. data: An optional map of data to pass into the workflow execution. There is a 10MB limit on the size of the full `data` payload. Any individual string value @@ -235,11 +235,11 @@ async def cancel( pair. Can optionally be provided one or more recipients to scope the request to. Args: - cancellation_key: An optional key that is used to reference a specific workflow trigger request - when issuing a [workflow cancellation](/send-notifications/canceling-workflows) - request. Must be provided while triggering a workflow in order to enable - subsequent cancellation. Should be unique across trigger requests to avoid - unintentional cancellations. + cancellation_key: A key that is used to reference a specific workflow trigger request when issuing + a [workflow cancellation](/send-notifications/canceling-workflows) request. Must + be provided while triggering a workflow in order to enable subsequent + cancellation. Should be unique across trigger requests to avoid unintentional + cancellations. recipients: A list of recipients to cancel the notification for. If omitted, cancels for all recipients associated with the cancellation key. @@ -308,11 +308,11 @@ async def trigger( (string), an inline user request (object), or an inline object request, which is determined by the presence of a `collection` property. - cancellation_key: An optional key that is used to reference a specific workflow trigger request - when issuing a [workflow cancellation](/send-notifications/canceling-workflows) - request. Must be provided while triggering a workflow in order to enable - subsequent cancellation. Should be unique across trigger requests to avoid - unintentional cancellations. + cancellation_key: A key that is used to reference a specific workflow trigger request when issuing + a [workflow cancellation](/send-notifications/canceling-workflows) request. Must + be provided while triggering a workflow in order to enable subsequent + cancellation. Should be unique across trigger requests to avoid unintentional + cancellations. data: An optional map of data to pass into the workflow execution. There is a 10MB limit on the size of the full `data` payload. Any individual string value diff --git a/src/knockapi/types/message_event.py b/src/knockapi/types/message_event.py index fe1a72c..17faa7e 100644 --- a/src/knockapi/types/message_event.py +++ b/src/knockapi/types/message_event.py @@ -34,22 +34,22 @@ class MessageEvent(BaseModel): """ type: Literal[ - "message.archived", - "message.bounced", + "message.read", + "message.sent", + "message.seen", "message.created", + "message.queued", "message.delivered", + "message.bounced", + "message.undelivered", + "message.not_sent", "message.delivery_attempted", - "message.interacted", + "message.archived", "message.link_clicked", - "message.not_sent", - "message.queued", - "message.read", - "message.seen", - "message.sent", - "message.unarchived", - "message.undelivered", - "message.unread", + "message.interacted", "message.unseen", + "message.unread", + "message.unarchived", ] """The type of event that occurred.""" diff --git a/src/knockapi/types/workflow_cancel_params.py b/src/knockapi/types/workflow_cancel_params.py index ad674e6..13a260a 100644 --- a/src/knockapi/types/workflow_cancel_params.py +++ b/src/knockapi/types/workflow_cancel_params.py @@ -14,11 +14,11 @@ class WorkflowCancelParams(TypedDict, total=False): cancellation_key: Required[str] """ - An optional key that is used to reference a specific workflow trigger request - when issuing a [workflow cancellation](/send-notifications/canceling-workflows) - request. Must be provided while triggering a workflow in order to enable - subsequent cancellation. Should be unique across trigger requests to avoid - unintentional cancellations. + A key that is used to reference a specific workflow trigger request when issuing + a [workflow cancellation](/send-notifications/canceling-workflows) request. Must + be provided while triggering a workflow in order to enable subsequent + cancellation. Should be unique across trigger requests to avoid unintentional + cancellations. """ recipients: Optional[SequenceNotStr[RecipientReferenceParam]] diff --git a/src/knockapi/types/workflow_recipient_run.py b/src/knockapi/types/workflow_recipient_run.py index 884b684..4217f64 100644 --- a/src/knockapi/types/workflow_recipient_run.py +++ b/src/knockapi/types/workflow_recipient_run.py @@ -7,7 +7,6 @@ from pydantic import Field as FieldInfo from .._models import BaseModel -from .recipient import Recipient from .recipient_reference import RecipientReference __all__ = ["WorkflowRecipientRun", "TriggerSource"] @@ -74,8 +73,11 @@ class WorkflowRecipientRun(BaseModel): single trigger. """ - actor: Optional[Recipient] = None - """A recipient of a notification, which is either a user or an object.""" + actor: Optional[RecipientReference] = None + """ + A reference to a recipient, either a user identifier (string) or an object + reference (ID, collection). + """ error_count: Optional[int] = None """The number of errors encountered during the workflow recipient run.""" diff --git a/src/knockapi/types/workflow_trigger_params.py b/src/knockapi/types/workflow_trigger_params.py index 604f319..24088b4 100644 --- a/src/knockapi/types/workflow_trigger_params.py +++ b/src/knockapi/types/workflow_trigger_params.py @@ -30,11 +30,11 @@ class WorkflowTriggerParams(TypedDict, total=False): cancellation_key: Optional[str] """ - An optional key that is used to reference a specific workflow trigger request - when issuing a [workflow cancellation](/send-notifications/canceling-workflows) - request. Must be provided while triggering a workflow in order to enable - subsequent cancellation. Should be unique across trigger requests to avoid - unintentional cancellations. + A key that is used to reference a specific workflow trigger request when issuing + a [workflow cancellation](/send-notifications/canceling-workflows) request. Must + be provided while triggering a workflow in order to enable subsequent + cancellation. Should be unique across trigger requests to avoid unintentional + cancellations. """ data: Optional[Dict[str, object]] diff --git a/tests/test_extract_files.py b/tests/test_extract_files.py index dc13e21..eeb7608 100644 --- a/tests/test_extract_files.py +++ b/tests/test_extract_files.py @@ -4,7 +4,7 @@ import pytest -from knockapi._types import FileTypes +from knockapi._types import FileTypes, ArrayFormat from knockapi._utils import extract_files @@ -37,10 +37,7 @@ def test_multiple_files() -> None: def test_top_level_file_array() -> None: query = {"files": [b"file one", b"file two"], "title": "hello"} - assert extract_files(query, paths=[["files", ""]]) == [ - ("files[]", b"file one"), - ("files[]", b"file two"), - ] + assert extract_files(query, paths=[["files", ""]]) == [("files[]", b"file one"), ("files[]", b"file two")] assert query == {"title": "hello"} @@ -71,3 +68,24 @@ def test_ignores_incorrect_paths( expected: list[tuple[str, FileTypes]], ) -> None: assert extract_files(query, paths=paths) == expected + + +@pytest.mark.parametrize( + "array_format,expected_top_level,expected_nested", + [ + ("brackets", [("files[]", b"a"), ("files[]", b"b")], [("items[][file]", b"a"), ("items[][file]", b"b")]), + ("repeat", [("files", b"a"), ("files", b"b")], [("items[file]", b"a"), ("items[file]", b"b")]), + ("comma", [("files", b"a"), ("files", b"b")], [("items[file]", b"a"), ("items[file]", b"b")]), + ("indices", [("files[0]", b"a"), ("files[1]", b"b")], [("items[0][file]", b"a"), ("items[1][file]", b"b")]), + ], +) +def test_array_format_controls_file_field_names( + array_format: ArrayFormat, + expected_top_level: list[tuple[str, FileTypes]], + expected_nested: list[tuple[str, FileTypes]], +) -> None: + top_level = {"files": [b"a", b"b"]} + assert extract_files(top_level, paths=[["files", ""]], array_format=array_format) == expected_top_level + + nested = {"items": [{"file": b"a"}, {"file": b"b"}]} + assert extract_files(nested, paths=[["items", "", "file"]], array_format=array_format) == expected_nested diff --git a/tests/test_files.py b/tests/test_files.py index 641bf93..83cca7b 100644 --- a/tests/test_files.py +++ b/tests/test_files.py @@ -131,7 +131,7 @@ def test_extract_files_does_not_mutate_original_nested_array_path(self) -> None: copied = deepcopy_with_paths(original, [["items", "", "file"]]) extracted = extract_files(copied, paths=[["items", "", "file"]]) - assert extracted == [("items[][file]", file1), ("items[][file]", file2)] + assert [entry for _, entry in extracted] == [file1, file2] assert original == { "items": [ {"file": file1, "extra": 1}, diff --git a/tests/test_models.py b/tests/test_models.py index 36593c9..d3ced95 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -1,7 +1,8 @@ import json -from typing import TYPE_CHECKING, Any, Dict, List, Union, Optional, cast +from typing import TYPE_CHECKING, Any, Dict, List, Union, Iterable, Optional, cast from datetime import datetime, timezone -from typing_extensions import Literal, Annotated, TypeAliasType +from collections import deque +from typing_extensions import Literal, Annotated, TypedDict, TypeAliasType import pytest import pydantic @@ -9,7 +10,7 @@ from knockapi._utils import PropertyInfo from knockapi._compat import PYDANTIC_V1, parse_obj, model_dump, model_json -from knockapi._models import DISCRIMINATOR_CACHE, BaseModel, construct_type +from knockapi._models import DISCRIMINATOR_CACHE, BaseModel, EagerIterable, construct_type class BasicModel(BaseModel): @@ -961,3 +962,56 @@ def __getattr__(self, attr: str) -> Item: ... assert model.a.prop == 1 assert isinstance(model.a, Item) assert model.other == "foo" + + +# NOTE: Workaround for Pydantic Iterable behavior. +# Iterable fields are replaced with a ValidatorIterator and may be consumed +# during serialization, which can cause subsequent dumps to return empty data. +# See: https://github.com/pydantic/pydantic/issues/9541 +@pytest.mark.parametrize( + "data, expected_validated", + [ + ([1, 2, 3], [1, 2, 3]), + ((1, 2, 3), (1, 2, 3)), + (set([1, 2, 3]), set([1, 2, 3])), + (iter([1, 2, 3]), [1, 2, 3]), + ([], []), + ((x for x in [1, 2, 3]), [1, 2, 3]), + (map(lambda x: x, [1, 2, 3]), [1, 2, 3]), + (frozenset([1, 2, 3]), frozenset([1, 2, 3])), + (deque([1, 2, 3]), deque([1, 2, 3])), + ], + ids=["list", "tuple", "set", "iterator", "empty", "generator", "map", "frozenset", "deque"], +) +@pytest.mark.skipif(PYDANTIC_V1, reason="this is only supported in pydantic v2") +def test_iterable_construction(data: Iterable[int], expected_validated: Iterable[int]) -> None: + class TypeWithIterable(TypedDict): + items: EagerIterable[int] + + class Model(BaseModel): + data: TypeWithIterable + + m = Model.model_validate({"data": {"items": data}}) + assert m.data["items"] == expected_validated + + # Verify repeated dumps don't lose data (the original bug) + assert m.model_dump()["data"]["items"] == list(expected_validated) + assert m.model_dump()["data"]["items"] == list(expected_validated) + + +@pytest.mark.skipif(PYDANTIC_V1, reason="this is only supported in pydantic v2") +def test_iterable_construction_str_falls_back_to_list() -> None: + # str is iterable (over chars), but str(list_of_chars) produces the list's repr + # rather than reconstructing a string from items. We special-case str to fall + # back to list instead of attempting reconstruction. + class TypeWithIterable(TypedDict): + items: EagerIterable[str] + + class Model(BaseModel): + data: TypeWithIterable + + m = Model.model_validate({"data": {"items": "hello"}}) + + # falls back to list of chars rather than calling str(["h", "e", "l", "l", "o"]) + assert m.data["items"] == ["h", "e", "l", "l", "o"] + assert m.model_dump()["data"]["items"] == ["h", "e", "l", "l", "o"]