Skip to content

Commit 3b9a2cb

Browse files
feat: Add NOT_GIVEN sentinel for nullable optional params (#694)
Co-authored-by: Devin <devin@workos.com>
1 parent b1d3352 commit 3b9a2cb

3 files changed

Lines changed: 46 additions & 2 deletions

File tree

src/workos/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
)
1717
from ._pagination import AsyncPage, ListMetadata, SyncPage
1818
from .public_client import create_public_client
19-
from ._types import RequestOptions
19+
from ._types import NOT_GIVEN, NotGiven, RequestOptions
2020

2121
__all__ = [
2222
"WorkOSClient",
@@ -34,5 +34,7 @@
3434
"AsyncPage",
3535
"ListMetadata",
3636
"RequestOptions",
37+
"NOT_GIVEN",
38+
"NotGiven",
3739
"create_public_client",
3840
]

src/workos/_types.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import sys
66
from datetime import datetime
77
from enum import Enum
8-
from typing import Any, Dict, NoReturn, Protocol, TypedDict, TypeVar
8+
from typing import Any, Dict, Literal, NoReturn, Protocol, TypedDict, TypeVar
99

1010
if sys.version_info >= (3, 11):
1111
from typing import Self
@@ -35,6 +35,24 @@ def enum_value(value: Any) -> Any:
3535
return value.value if isinstance(value, Enum) else value
3636

3737

38+
class NotGiven:
39+
"""Sentinel used as the default for nullable optional parameters.
40+
41+
Distinguishes an omitted argument ("leave unchanged", not sent) from an
42+
explicit ``None``, which clears the field by sending JSON ``null``.
43+
Falsy so ``if not param`` reads naturally.
44+
"""
45+
46+
def __bool__(self) -> Literal[False]:
47+
return False
48+
49+
def __repr__(self) -> str:
50+
return "NOT_GIVEN"
51+
52+
53+
NOT_GIVEN = NotGiven()
54+
55+
3856
D = TypeVar("D", bound=Deserializable)
3957

4058

tests/test_types.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from workos._types import NOT_GIVEN, NotGiven
2+
3+
4+
class TestNotGiven:
5+
"""The NOT_GIVEN sentinel is the default for nullable optional params,
6+
letting generated methods tell an omitted argument apart from an explicit
7+
None (which clears the field via JSON null)."""
8+
9+
def test_is_falsy(self):
10+
assert not NOT_GIVEN
11+
assert bool(NOT_GIVEN) is False
12+
13+
def test_repr(self):
14+
assert repr(NOT_GIVEN) == "NOT_GIVEN"
15+
16+
def test_is_distinct_from_none(self):
17+
assert NOT_GIVEN is not None
18+
assert isinstance(NOT_GIVEN, NotGiven)
19+
20+
def test_exported_from_public_namespace(self):
21+
import workos
22+
23+
assert workos.NOT_GIVEN is NOT_GIVEN
24+
assert workos.NotGiven is NotGiven

0 commit comments

Comments
 (0)