Skip to content

Commit ae486ab

Browse files
chore(types): change optional parameter type from NotGiven to Omit
1 parent 1e72324 commit ae486ab

36 files changed

+370
-356
lines changed

src/finch/__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 Finch, Client, Stream, Timeout, Transport, AsyncFinch, 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
"FinchError",
4345
"APIError",
4446
"APIStatusError",

src/finch/_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
@@ -146,9 +146,9 @@ def __init__(
146146
def __init__(
147147
self,
148148
*,
149-
url: URL | NotGiven = NOT_GIVEN,
150-
json: Body | NotGiven = NOT_GIVEN,
151-
params: Query | NotGiven = NOT_GIVEN,
149+
url: URL | NotGiven = not_given,
150+
json: Body | NotGiven = not_given,
151+
params: Query | NotGiven = not_given,
152152
) -> None:
153153
self.url = url
154154
self.json = json
@@ -596,7 +596,7 @@ def _maybe_override_cast_to(self, cast_to: type[ResponseT], options: FinalReques
596596
# we internally support defining a temporary header to override the
597597
# default `cast_to` type for use with `.with_raw_response` and `.with_streaming_response`
598598
# see _response.py for implementation details
599-
override_cast_to = headers.pop(OVERRIDE_CAST_TO_HEADER, NOT_GIVEN)
599+
override_cast_to = headers.pop(OVERRIDE_CAST_TO_HEADER, not_given)
600600
if is_given(override_cast_to):
601601
options.headers = headers
602602
return cast(Type[ResponseT], override_cast_to)
@@ -826,7 +826,7 @@ def __init__(
826826
version: 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,
@@ -1371,7 +1371,7 @@ def __init__(
13711371
base_url: str | URL,
13721372
_strict_response_validation: bool,
13731373
max_retries: int = DEFAULT_MAX_RETRIES,
1374-
timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
1374+
timeout: float | Timeout | None | NotGiven = not_given,
13751375
http_client: httpx.AsyncClient | None = None,
13761376
custom_headers: Mapping[str, str] | None = None,
13771377
custom_query: Mapping[str, object] | None = None,
@@ -1847,8 +1847,8 @@ def make_request_options(
18471847
extra_query: Query | None = None,
18481848
extra_body: Body | None = None,
18491849
idempotency_key: str | None = None,
1850-
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
1851-
post_parser: PostParser | NotGiven = NOT_GIVEN,
1850+
timeout: float | httpx.Timeout | None | NotGiven = not_given,
1851+
post_parser: PostParser | NotGiven = not_given,
18521852
) -> RequestOptions:
18531853
"""Create a dict of type RequestOptions without keys of NotGiven values."""
18541854
options: RequestOptions = {}

src/finch/_client.py

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

55
import os
66
import base64
7-
from typing import TYPE_CHECKING, Any, Union, Mapping
7+
from typing import TYPE_CHECKING, Any, Mapping
88
from typing_extensions import Self, override
99

1010
import httpx
1111

1212
from . import _exceptions
1313
from ._qs import Querystring
1414
from ._types import (
15-
NOT_GIVEN,
1615
Omit,
1716
Headers,
1817
Timeout,
1918
NotGiven,
2019
Transport,
2120
ProxiesTypes,
2221
RequestOptions,
22+
not_given,
2323
)
2424
from ._utils import is_given, get_async_library
2525
from ._compat import cached_property
@@ -73,7 +73,7 @@ def __init__(
7373
client_secret: str | None = None,
7474
webhook_secret: str | None = None,
7575
base_url: str | httpx.URL | None = None,
76-
timeout: Union[float, Timeout, None, NotGiven] = NOT_GIVEN,
76+
timeout: float | Timeout | None | NotGiven = not_given,
7777
max_retries: int = DEFAULT_MAX_RETRIES,
7878
default_headers: Mapping[str, str] | None = None,
7979
default_query: Mapping[str, object] | None = None,
@@ -261,9 +261,9 @@ def copy(
261261
client_secret: str | None = None,
262262
webhook_secret: str | None = None,
263263
base_url: str | httpx.URL | None = None,
264-
timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
264+
timeout: float | Timeout | None | NotGiven = not_given,
265265
http_client: httpx.Client | None = None,
266-
max_retries: int | NotGiven = NOT_GIVEN,
266+
max_retries: int | NotGiven = not_given,
267267
default_headers: Mapping[str, str] | None = None,
268268
set_default_headers: Mapping[str, str] | None = None,
269269
default_query: Mapping[str, object] | None = None,
@@ -423,7 +423,7 @@ def __init__(
423423
client_secret: str | None = None,
424424
webhook_secret: str | None = None,
425425
base_url: str | httpx.URL | None = None,
426-
timeout: Union[float, Timeout, None, NotGiven] = NOT_GIVEN,
426+
timeout: float | Timeout | None | NotGiven = not_given,
427427
max_retries: int = DEFAULT_MAX_RETRIES,
428428
default_headers: Mapping[str, str] | None = None,
429429
default_query: Mapping[str, object] | None = None,
@@ -611,9 +611,9 @@ def copy(
611611
client_secret: str | None = None,
612612
webhook_secret: str | None = None,
613613
base_url: str | httpx.URL | None = None,
614-
timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
614+
timeout: float | Timeout | None | NotGiven = not_given,
615615
http_client: httpx.AsyncClient | None = None,
616-
max_retries: int | NotGiven = NOT_GIVEN,
616+
max_retries: int | NotGiven = not_given,
617617
default_headers: Mapping[str, str] | None = None,
618618
set_default_headers: Mapping[str, str] | None = None,
619619
default_query: Mapping[str, object] | None = None,

src/finch/_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/finch/_types.py

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

@@ -141,13 +144,14 @@ def __repr__(self) -> str:
141144
return "NOT_GIVEN"
142145

143146

144-
NotGivenOr = Union[_T, NotGiven]
147+
not_given = NotGiven()
148+
# for backwards compatibility:
145149
NOT_GIVEN = NotGiven()
146150

147151

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

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

168172

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

src/finch/_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/finch/_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/finch/resources/access_tokens.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from .. import _legacy_response
88
from ..types import access_token_create_params
9-
from .._types import NOT_GIVEN, Body, Query, Headers, NotGiven
9+
from .._types import Body, Omit, Query, Headers, NotGiven, omit, not_given
1010
from .._utils import is_given, maybe_transform
1111
from .._compat import cached_property
1212
from .._resource import SyncAPIResource, AsyncAPIResource
@@ -41,15 +41,15 @@ def create(
4141
self,
4242
*,
4343
code: str,
44-
client_id: str | NotGiven = NOT_GIVEN,
45-
client_secret: str | NotGiven = NOT_GIVEN,
46-
redirect_uri: str | NotGiven = NOT_GIVEN,
44+
client_id: str | Omit = omit,
45+
client_secret: str | Omit = omit,
46+
redirect_uri: str | Omit = omit,
4747
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
4848
# The extra values given here take precedence over values defined on the client or passed to this method.
4949
extra_headers: Headers | None = None,
5050
extra_query: Query | None = None,
5151
extra_body: Body | None = None,
52-
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
52+
timeout: float | httpx.Timeout | None | NotGiven = not_given,
5353
) -> CreateAccessTokenResponse:
5454
"""
5555
Exchange the authorization code for an access token
@@ -127,15 +127,15 @@ async def create(
127127
self,
128128
*,
129129
code: str,
130-
client_id: str | NotGiven = NOT_GIVEN,
131-
client_secret: str | NotGiven = NOT_GIVEN,
132-
redirect_uri: str | NotGiven = NOT_GIVEN,
130+
client_id: str | Omit = omit,
131+
client_secret: str | Omit = omit,
132+
redirect_uri: str | Omit = omit,
133133
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
134134
# The extra values given here take precedence over values defined on the client or passed to this method.
135135
extra_headers: Headers | None = None,
136136
extra_query: Query | None = None,
137137
extra_body: Body | None = None,
138-
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
138+
timeout: float | httpx.Timeout | None | NotGiven = not_given,
139139
) -> CreateAccessTokenResponse:
140140
"""
141141
Exchange the authorization code for an access token

0 commit comments

Comments
 (0)