Skip to content

Commit 4b8814d

Browse files
chore(types): change optional parameter type from NotGiven to Omit
1 parent bba9fec commit 4b8814d

File tree

10 files changed

+70
-54
lines changed

10 files changed

+70
-54
lines changed

src/miru_agent_sdk/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import typing as _t
44

55
from . import types
6-
from ._types import NOT_GIVEN, Omit, NoneType, NotGiven, Transport, ProxiesTypes
6+
from ._types import NOT_GIVEN, Omit, NoneType, NotGiven, Transport, ProxiesTypes, omit, not_given
77
from ._utils import file_from_path
88
from ._client import Miru, Client, Stream, Timeout, AsyncMiru, Transport, AsyncClient, AsyncStream, RequestOptions
99
from ._models import BaseModel
@@ -38,7 +38,9 @@
3838
"ProxiesTypes",
3939
"NotGiven",
4040
"NOT_GIVEN",
41+
"not_given",
4142
"Omit",
43+
"omit",
4244
"MiruError",
4345
"APIError",
4446
"APIStatusError",

src/miru_agent_sdk/_base_client.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
from ._qs import Querystring
4343
from ._files import to_httpx_files, async_to_httpx_files
4444
from ._types import (
45-
NOT_GIVEN,
4645
Body,
4746
Omit,
4847
Query,
@@ -57,6 +56,7 @@
5756
RequestOptions,
5857
HttpxRequestFiles,
5958
ModelBuilderProtocol,
59+
not_given,
6060
)
6161
from ._utils import is_dict, is_list, asyncify, is_given, lru_cache, is_mapping
6262
from ._compat import PYDANTIC_V1, model_copy, model_dump
@@ -145,9 +145,9 @@ def __init__(
145145
def __init__(
146146
self,
147147
*,
148-
url: URL | NotGiven = NOT_GIVEN,
149-
json: Body | NotGiven = NOT_GIVEN,
150-
params: Query | NotGiven = NOT_GIVEN,
148+
url: URL | NotGiven = not_given,
149+
json: Body | NotGiven = not_given,
150+
params: Query | NotGiven = not_given,
151151
) -> None:
152152
self.url = url
153153
self.json = json
@@ -595,7 +595,7 @@ def _maybe_override_cast_to(self, cast_to: type[ResponseT], options: FinalReques
595595
# we internally support defining a temporary header to override the
596596
# default `cast_to` type for use with `.with_raw_response` and `.with_streaming_response`
597597
# see _response.py for implementation details
598-
override_cast_to = headers.pop(OVERRIDE_CAST_TO_HEADER, NOT_GIVEN)
598+
override_cast_to = headers.pop(OVERRIDE_CAST_TO_HEADER, not_given)
599599
if is_given(override_cast_to):
600600
options.headers = headers
601601
return cast(Type[ResponseT], override_cast_to)
@@ -826,7 +826,7 @@ def __init__(
826826
socket_path: str,
827827
base_url: str | URL,
828828
max_retries: int = DEFAULT_MAX_RETRIES,
829-
timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
829+
timeout: float | Timeout | None | NotGiven = not_given,
830830
http_client: httpx.Client | None = None,
831831
custom_headers: Mapping[str, str] | None = None,
832832
custom_query: Mapping[str, object] | None = None,
@@ -1359,7 +1359,7 @@ def __init__(
13591359
base_url: str | URL,
13601360
_strict_response_validation: bool,
13611361
max_retries: int = DEFAULT_MAX_RETRIES,
1362-
timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
1362+
timeout: float | Timeout | None | NotGiven = not_given,
13631363
http_client: httpx.AsyncClient | None = None,
13641364
custom_headers: Mapping[str, str] | None = None,
13651365
custom_query: Mapping[str, object] | None = None,
@@ -1822,8 +1822,8 @@ def make_request_options(
18221822
extra_query: Query | None = None,
18231823
extra_body: Body | None = None,
18241824
idempotency_key: str | None = None,
1825-
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
1826-
post_parser: PostParser | NotGiven = NOT_GIVEN,
1825+
timeout: float | httpx.Timeout | None | NotGiven = not_given,
1826+
post_parser: PostParser | NotGiven = not_given,
18271827
) -> RequestOptions:
18281828
"""Create a dict of type RequestOptions without keys of NotGiven values."""
18291829
options: RequestOptions = {}

src/miru_agent_sdk/_client.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33
from __future__ import annotations
44

55
import os
6-
from typing import Any, Union, Mapping
6+
from typing import Any, Mapping
77
from typing_extensions import Self, override
88

99
import httpx
1010

1111
from . import _exceptions
1212
from ._qs import Querystring
1313
from ._types import (
14-
NOT_GIVEN,
1514
Omit,
1615
Timeout,
1716
NotGiven,
1817
Transport,
1918
ProxiesTypes,
2019
RequestOptions,
20+
not_given,
2121
)
2222
from ._utils import is_given, get_async_library
2323
from ._version import __version__
@@ -47,7 +47,7 @@ def __init__(
4747
*,
4848
socket_path: str | None = None,
4949
base_url: str | httpx.URL | None = None,
50-
timeout: Union[float, Timeout, None, NotGiven] = NOT_GIVEN,
50+
timeout: float | Timeout | None | NotGiven = not_given,
5151
max_retries: int = DEFAULT_MAX_RETRIES,
5252
default_headers: Mapping[str, str] | None = None,
5353
default_query: Mapping[str, object] | None = None,
@@ -114,9 +114,9 @@ def copy(
114114
*,
115115
socket_path: str | None = None,
116116
base_url: str | httpx.URL | None = None,
117-
timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
117+
timeout: float | Timeout | None | NotGiven = not_given,
118118
http_client: httpx.Client | None = None,
119-
max_retries: int | NotGiven = NOT_GIVEN,
119+
max_retries: int | NotGiven = not_given,
120120
default_headers: Mapping[str, str] | None = None,
121121
set_default_headers: Mapping[str, str] | None = None,
122122
default_query: Mapping[str, object] | None = None,
@@ -208,7 +208,7 @@ def __init__(
208208
*,
209209
socket_path: str | None = None,
210210
base_url: str | httpx.URL | None = None,
211-
timeout: Union[float, Timeout, None, NotGiven] = NOT_GIVEN,
211+
timeout: float | Timeout | None | NotGiven = not_given,
212212
max_retries: int = DEFAULT_MAX_RETRIES,
213213
default_headers: Mapping[str, str] | None = None,
214214
default_query: Mapping[str, object] | None = None,
@@ -275,9 +275,9 @@ def copy(
275275
*,
276276
socket_path: str | None = None,
277277
base_url: str | httpx.URL | None = None,
278-
timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
278+
timeout: float | Timeout | None | NotGiven = not_given,
279279
http_client: httpx.AsyncClient | None = None,
280-
max_retries: int | NotGiven = NOT_GIVEN,
280+
max_retries: int | NotGiven = not_given,
281281
default_headers: Mapping[str, str] | None = None,
282282
set_default_headers: Mapping[str, str] | None = None,
283283
default_query: Mapping[str, object] | None = None,

src/miru_agent_sdk/_qs.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from urllib.parse import parse_qs, urlencode
55
from typing_extensions import Literal, get_args
66

7-
from ._types import NOT_GIVEN, NotGiven, NotGivenOr
7+
from ._types import NotGiven, not_given
88
from ._utils import flatten
99

1010
_T = TypeVar("_T")
@@ -41,8 +41,8 @@ def stringify(
4141
self,
4242
params: Params,
4343
*,
44-
array_format: NotGivenOr[ArrayFormat] = NOT_GIVEN,
45-
nested_format: NotGivenOr[NestedFormat] = NOT_GIVEN,
44+
array_format: ArrayFormat | NotGiven = not_given,
45+
nested_format: NestedFormat | NotGiven = not_given,
4646
) -> str:
4747
return urlencode(
4848
self.stringify_items(
@@ -56,8 +56,8 @@ def stringify_items(
5656
self,
5757
params: Params,
5858
*,
59-
array_format: NotGivenOr[ArrayFormat] = NOT_GIVEN,
60-
nested_format: NotGivenOr[NestedFormat] = NOT_GIVEN,
59+
array_format: ArrayFormat | NotGiven = not_given,
60+
nested_format: NestedFormat | NotGiven = not_given,
6161
) -> list[tuple[str, str]]:
6262
opts = Options(
6363
qs=self,
@@ -143,8 +143,8 @@ def __init__(
143143
self,
144144
qs: Querystring = _qs,
145145
*,
146-
array_format: NotGivenOr[ArrayFormat] = NOT_GIVEN,
147-
nested_format: NotGivenOr[NestedFormat] = NOT_GIVEN,
146+
array_format: ArrayFormat | NotGiven = not_given,
147+
nested_format: NestedFormat | NotGiven = not_given,
148148
) -> None:
149149
self.array_format = qs.array_format if isinstance(array_format, NotGiven) else array_format
150150
self.nested_format = qs.nested_format if isinstance(nested_format, NotGiven) else nested_format

src/miru_agent_sdk/_types.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,21 @@ class RequestOptions(TypedDict, total=False):
117117
# Sentinel class used until PEP 0661 is accepted
118118
class NotGiven:
119119
"""
120-
A sentinel singleton class used to distinguish omitted keyword arguments
121-
from those passed in with the value None (which may have different behavior).
120+
For parameters with a meaningful None value, we need to distinguish between
121+
the user explicitly passing None, and the user not passing the parameter at
122+
all.
123+
124+
User code shouldn't need to use not_given directly.
122125
123126
For example:
124127
125128
```py
126-
def get(timeout: Union[int, NotGiven, None] = NotGiven()) -> Response: ...
129+
def create(timeout: Timeout | None | NotGiven = not_given): ...
127130
128131
129-
get(timeout=1) # 1s timeout
130-
get(timeout=None) # No timeout
131-
get() # Default timeout behavior, which may not be statically known at the method definition.
132+
create(timeout=1) # 1s timeout
133+
create(timeout=None) # No timeout
134+
create() # Default timeout behavior
132135
```
133136
"""
134137

@@ -140,13 +143,14 @@ def __repr__(self) -> str:
140143
return "NOT_GIVEN"
141144

142145

143-
NotGivenOr = Union[_T, NotGiven]
146+
not_given = NotGiven()
147+
# for backwards compatibility:
144148
NOT_GIVEN = NotGiven()
145149

146150

147151
class Omit:
148-
"""In certain situations you need to be able to represent a case where a default value has
149-
to be explicitly removed and `None` is not an appropriate substitute, for example:
152+
"""
153+
To explicitly omit something from being sent in a request, use `omit`.
150154
151155
```py
152156
# as the default `Content-Type` header is `application/json` that will be sent
@@ -156,15 +160,18 @@ class Omit:
156160
# to look something like: 'multipart/form-data; boundary=0d8382fcf5f8c3be01ca2e11002d2983'
157161
client.post(..., headers={"Content-Type": "multipart/form-data"})
158162
159-
# instead you can remove the default `application/json` header by passing Omit
160-
client.post(..., headers={"Content-Type": Omit()})
163+
# instead you can remove the default `application/json` header by passing omit
164+
client.post(..., headers={"Content-Type": omit})
161165
```
162166
"""
163167

164168
def __bool__(self) -> Literal[False]:
165169
return False
166170

167171

172+
omit = Omit()
173+
174+
168175
@runtime_checkable
169176
class ModelBuilderProtocol(Protocol):
170177
@classmethod

src/miru_agent_sdk/_utils/_transform.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ def _transform_typeddict(
268268
annotations = get_type_hints(expected_type, include_extras=True)
269269
for key, value in data.items():
270270
if not is_given(value):
271-
# we don't need to include `NotGiven` values here as they'll
271+
# we don't need to include omitted values here as they'll
272272
# be stripped out before the request is sent anyway
273273
continue
274274

@@ -434,7 +434,7 @@ async def _async_transform_typeddict(
434434
annotations = get_type_hints(expected_type, include_extras=True)
435435
for key, value in data.items():
436436
if not is_given(value):
437-
# we don't need to include `NotGiven` values here as they'll
437+
# we don't need to include omitted values here as they'll
438438
# be stripped out before the request is sent anyway
439439
continue
440440

src/miru_agent_sdk/_utils/_utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import sniffio
2323

24-
from .._types import NotGiven, FileTypes, NotGivenOr, HeadersLike
24+
from .._types import Omit, NotGiven, FileTypes, HeadersLike
2525

2626
_T = TypeVar("_T")
2727
_TupleT = TypeVar("_TupleT", bound=Tuple[object, ...])
@@ -63,7 +63,7 @@ def _extract_items(
6363
try:
6464
key = path[index]
6565
except IndexError:
66-
if isinstance(obj, NotGiven):
66+
if not is_given(obj):
6767
# no value was provided - we can safely ignore
6868
return []
6969

@@ -126,8 +126,8 @@ def _extract_items(
126126
return []
127127

128128

129-
def is_given(obj: NotGivenOr[_T]) -> TypeGuard[_T]:
130-
return not isinstance(obj, NotGiven)
129+
def is_given(obj: _T | NotGiven | Omit) -> TypeGuard[_T]:
130+
return not isinstance(obj, NotGiven) and not isinstance(obj, Omit)
131131

132132

133133
# Type safe methods for narrowing types with TypeVars.

src/miru_agent_sdk/resources/agent.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import httpx
66

7-
from .._types import NOT_GIVEN, Body, Query, Headers, NotGiven
7+
from .._types import Body, Query, Headers, NotGiven, not_given
88
from .._compat import cached_property
99
from .._resource import SyncAPIResource, AsyncAPIResource
1010
from .._response import (
@@ -48,7 +48,7 @@ def health(
4848
extra_headers: Headers | None = None,
4949
extra_query: Query | None = None,
5050
extra_body: Body | None = None,
51-
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
51+
timeout: float | httpx.Timeout | None | NotGiven = not_given,
5252
) -> AgentHealthResponse:
5353
"""Health"""
5454
return self._get(
@@ -67,7 +67,7 @@ def version(
6767
extra_headers: Headers | None = None,
6868
extra_query: Query | None = None,
6969
extra_body: Body | None = None,
70-
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
70+
timeout: float | httpx.Timeout | None | NotGiven = not_given,
7171
) -> AgentVersionResponse:
7272
"""Version"""
7373
return self._get(
@@ -107,7 +107,7 @@ async def health(
107107
extra_headers: Headers | None = None,
108108
extra_query: Query | None = None,
109109
extra_body: Body | None = None,
110-
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
110+
timeout: float | httpx.Timeout | None | NotGiven = not_given,
111111
) -> AgentHealthResponse:
112112
"""Health"""
113113
return await self._get(
@@ -126,7 +126,7 @@ async def version(
126126
extra_headers: Headers | None = None,
127127
extra_query: Query | None = None,
128128
extra_body: Body | None = None,
129-
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
129+
timeout: float | httpx.Timeout | None | NotGiven = not_given,
130130
) -> AgentVersionResponse:
131131
"""Version"""
132132
return await self._get(

src/miru_agent_sdk/resources/device.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import httpx
66

7-
from .._types import NOT_GIVEN, Body, Query, Headers, NotGiven
7+
from .._types import Body, Query, Headers, NotGiven, not_given
88
from .._compat import cached_property
99
from .._resource import SyncAPIResource, AsyncAPIResource
1010
from .._response import (
@@ -48,7 +48,7 @@ def retrieve(
4848
extra_headers: Headers | None = None,
4949
extra_query: Query | None = None,
5050
extra_body: Body | None = None,
51-
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
51+
timeout: float | httpx.Timeout | None | NotGiven = not_given,
5252
) -> DeviceRetrieveResponse:
5353
"""Get"""
5454
return self._get(
@@ -67,7 +67,7 @@ def sync(
6767
extra_headers: Headers | None = None,
6868
extra_query: Query | None = None,
6969
extra_body: Body | None = None,
70-
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
70+
timeout: float | httpx.Timeout | None | NotGiven = not_given,
7171
) -> DeviceSyncResponse:
7272
"""Sync"""
7373
return self._post(
@@ -107,7 +107,7 @@ async def retrieve(
107107
extra_headers: Headers | None = None,
108108
extra_query: Query | None = None,
109109
extra_body: Body | None = None,
110-
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
110+
timeout: float | httpx.Timeout | None | NotGiven = not_given,
111111
) -> DeviceRetrieveResponse:
112112
"""Get"""
113113
return await self._get(
@@ -126,7 +126,7 @@ async def sync(
126126
extra_headers: Headers | None = None,
127127
extra_query: Query | None = None,
128128
extra_body: Body | None = None,
129-
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
129+
timeout: float | httpx.Timeout | None | NotGiven = not_given,
130130
) -> DeviceSyncResponse:
131131
"""Sync"""
132132
return await self._post(

0 commit comments

Comments
 (0)