Skip to content

Commit 43968e2

Browse files
chore(types): change optional parameter type from NotGiven to Omit
1 parent afc8269 commit 43968e2

642 files changed

Lines changed: 19352 additions & 19396 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/cloudflare/__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 (
99
Client,
@@ -48,7 +48,9 @@
4848
"ProxiesTypes",
4949
"NotGiven",
5050
"NOT_GIVEN",
51+
"not_given",
5152
"Omit",
53+
"omit",
5254
"CloudflareError",
5355
"APIError",
5456
"APIStatusError",

src/cloudflare/_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,
@@ -59,6 +58,7 @@
5958
MultipartSyntax,
6059
HttpxRequestFiles,
6160
ModelBuilderProtocol,
61+
not_given,
6262
)
6363
from ._utils import is_dict, is_list, asyncify, is_given, is_tuple, lru_cache, is_mapping, is_mapping_t, is_sequence_t
6464
from ._compat import PYDANTIC_V1, model_copy, model_dump
@@ -147,9 +147,9 @@ def __init__(
147147
def __init__(
148148
self,
149149
*,
150-
url: URL | NotGiven = NOT_GIVEN,
151-
json: Body | NotGiven = NOT_GIVEN,
152-
params: Query | NotGiven = NOT_GIVEN,
150+
url: URL | NotGiven = not_given,
151+
json: Body | NotGiven = not_given,
152+
params: Query | NotGiven = not_given,
153153
) -> None:
154154
self.url = url
155155
self.json = json
@@ -635,7 +635,7 @@ def _maybe_override_cast_to(self, cast_to: type[ResponseT], options: FinalReques
635635
# we internally support defining a temporary header to override the
636636
# default `cast_to` type for use with `.with_raw_response` and `.with_streaming_response`
637637
# see _response.py for implementation details
638-
override_cast_to = headers.pop(OVERRIDE_CAST_TO_HEADER, NOT_GIVEN)
638+
override_cast_to = headers.pop(OVERRIDE_CAST_TO_HEADER, not_given)
639639
if is_given(override_cast_to):
640640
options.headers = headers
641641
return cast(Type[ResponseT], override_cast_to)
@@ -866,7 +866,7 @@ def __init__(
866866
base_url: str | URL,
867867
api_version: str,
868868
max_retries: int = DEFAULT_MAX_RETRIES,
869-
timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
869+
timeout: float | Timeout | None | NotGiven = not_given,
870870
http_client: httpx.Client | None = None,
871871
custom_headers: Mapping[str, str] | None = None,
872872
custom_query: Mapping[str, object] | None = None,
@@ -1399,7 +1399,7 @@ def __init__(
13991399
api_version: str,
14001400
_strict_response_validation: bool,
14011401
max_retries: int = DEFAULT_MAX_RETRIES,
1402-
timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
1402+
timeout: float | Timeout | None | NotGiven = not_given,
14031403
http_client: httpx.AsyncClient | None = None,
14041404
custom_headers: Mapping[str, str] | None = None,
14051405
custom_query: Mapping[str, object] | None = None,
@@ -1862,8 +1862,8 @@ def make_request_options(
18621862
extra_query: Query | None = None,
18631863
extra_body: Body | None = None,
18641864
idempotency_key: str | None = None,
1865-
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
1866-
post_parser: PostParser | NotGiven = NOT_GIVEN,
1865+
timeout: float | httpx.Timeout | None | NotGiven = not_given,
1866+
post_parser: PostParser | NotGiven = not_given,
18671867
multipart_syntax: MultipartSyntax | None = None,
18681868
) -> RequestOptions:
18691869
"""Create a dict of type RequestOptions without keys of NotGiven values."""

src/cloudflare/_client.py

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

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

@@ -12,14 +12,14 @@
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
@@ -276,7 +276,7 @@ def __init__(
276276
user_service_key: str | None = None,
277277
base_url: str | httpx.URL | None = None,
278278
api_version: str | None = None,
279-
timeout: Union[float, Timeout, None, NotGiven] = NOT_GIVEN,
279+
timeout: float | Timeout | None | NotGiven = not_given,
280280
max_retries: int = DEFAULT_MAX_RETRIES,
281281
default_headers: Mapping[str, str] | None = None,
282282
default_query: Mapping[str, object] | None = None,
@@ -1002,9 +1002,9 @@ def copy(
10021002
user_service_key: str | None = None,
10031003
base_url: str | httpx.URL | None = None,
10041004
api_version: str | None = None,
1005-
timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
1005+
timeout: float | Timeout | None | NotGiven = not_given,
10061006
http_client: httpx.Client | None = None,
1007-
max_retries: int | NotGiven = NOT_GIVEN,
1007+
max_retries: int | NotGiven = not_given,
10081008
default_headers: Mapping[str, str] | None = None,
10091009
set_default_headers: Mapping[str, str] | None = None,
10101010
default_query: Mapping[str, object] | None = None,
@@ -1102,7 +1102,7 @@ def __init__(
11021102
user_service_key: str | None = None,
11031103
base_url: str | httpx.URL | None = None,
11041104
api_version: str | None = None,
1105-
timeout: Union[float, Timeout, None, NotGiven] = NOT_GIVEN,
1105+
timeout: float | Timeout | None | NotGiven = not_given,
11061106
max_retries: int = DEFAULT_MAX_RETRIES,
11071107
default_headers: Mapping[str, str] | None = None,
11081108
default_query: Mapping[str, object] | None = None,
@@ -1828,9 +1828,9 @@ def copy(
18281828
user_service_key: str | None = None,
18291829
base_url: str | httpx.URL | None = None,
18301830
api_version: str | None = None,
1831-
timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
1831+
timeout: float | Timeout | None | NotGiven = not_given,
18321832
http_client: httpx.AsyncClient | None = None,
1833-
max_retries: int | NotGiven = NOT_GIVEN,
1833+
max_retries: int | NotGiven = not_given,
18341834
default_headers: Mapping[str, str] | None = None,
18351835
set_default_headers: Mapping[str, str] | None = None,
18361836
default_query: Mapping[str, object] | None = None,

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

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

@@ -143,13 +146,14 @@ def __repr__(self) -> str:
143146
return "NOT_GIVEN"
144147

145148

146-
NotGivenOr = Union[_T, NotGiven]
149+
not_given = NotGiven()
150+
# for backwards compatibility:
147151
NOT_GIVEN = NotGiven()
148152

149153

150154
class Omit:
151-
"""In certain situations you need to be able to represent a case where a default value has
152-
to be explicitly removed and `None` is not an appropriate substitute, for example:
155+
"""
156+
To explicitly omit something from being sent in a request, use `omit`.
153157
154158
```py
155159
# as the default `Content-Type` header is `application/json` that will be sent
@@ -159,15 +163,18 @@ class Omit:
159163
# to look something like: 'multipart/form-data; boundary=0d8382fcf5f8c3be01ca2e11002d2983'
160164
client.post(..., headers={"Content-Type": "multipart/form-data"})
161165
162-
# instead you can remove the default `application/json` header by passing Omit
163-
client.post(..., headers={"Content-Type": Omit()})
166+
# instead you can remove the default `application/json` header by passing omit
167+
client.post(..., headers={"Content-Type": omit})
164168
```
165169
"""
166170

167171
def __bool__(self) -> Literal[False]:
168172
return False
169173

170174

175+
omit = Omit()
176+
177+
171178
@runtime_checkable
172179
class ModelBuilderProtocol(Protocol):
173180
@classmethod

src/cloudflare/_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/cloudflare/_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.

0 commit comments

Comments
 (0)