Skip to content

Commit 2874cc4

Browse files
Use Optional param annotations for manager create methods
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 2da21c9 commit 2874cc4

File tree

5 files changed

+46
-4
lines changed

5 files changed

+46
-4
lines changed

hyperbrowser/client/managers/async_manager/profile.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ class ProfileManager:
1414
def __init__(self, client):
1515
self._client = client
1616

17-
async def create(self, params: CreateProfileParams = None) -> CreateProfileResponse:
17+
async def create(
18+
self, params: Optional[CreateProfileParams] = None
19+
) -> CreateProfileResponse:
1820
response = await self._client.transport.post(
1921
self._client._build_url("/profile"),
2022
data=(

hyperbrowser/client/managers/async_manager/session.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ def __init__(self, client):
4444
self._client = client
4545
self.event_logs = SessionEventLogsManager(client)
4646

47-
async def create(self, params: CreateSessionParams = None) -> SessionDetail:
47+
async def create(
48+
self, params: Optional[CreateSessionParams] = None
49+
) -> SessionDetail:
4850
response = await self._client.transport.post(
4951
self._client._build_url("/session"),
5052
data=(

hyperbrowser/client/managers/sync_manager/profile.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ class ProfileManager:
1414
def __init__(self, client):
1515
self._client = client
1616

17-
def create(self, params: CreateProfileParams = None) -> CreateProfileResponse:
17+
def create(
18+
self, params: Optional[CreateProfileParams] = None
19+
) -> CreateProfileResponse:
1820
response = self._client.transport.post(
1921
self._client._build_url("/profile"),
2022
data=(

hyperbrowser/client/managers/sync_manager/session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def __init__(self, client):
4444
self._client = client
4545
self.event_logs = SessionEventLogsManager(client)
4646

47-
def create(self, params: CreateSessionParams = None) -> SessionDetail:
47+
def create(self, params: Optional[CreateSessionParams] = None) -> SessionDetail:
4848
response = self._client.transport.post(
4949
self._client._build_url("/session"),
5050
data=(
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from typing import Any, Type, Union, get_args, get_origin, get_type_hints
2+
3+
from hyperbrowser.client.managers.async_manager.profile import (
4+
ProfileManager as AsyncProfileManager,
5+
)
6+
from hyperbrowser.client.managers.async_manager.session import (
7+
SessionManager as AsyncSessionManager,
8+
)
9+
from hyperbrowser.client.managers.sync_manager.profile import (
10+
ProfileManager as SyncProfileManager,
11+
)
12+
from hyperbrowser.client.managers.sync_manager.session import (
13+
SessionManager as SyncSessionManager,
14+
)
15+
from hyperbrowser.models.profile import CreateProfileParams
16+
from hyperbrowser.models.session import CreateSessionParams
17+
18+
19+
def _is_optional_annotation(annotation: Any, expected_type: Type[Any]) -> bool:
20+
if get_origin(annotation) is not Union:
21+
return False
22+
args = set(get_args(annotation))
23+
return expected_type in args and type(None) in args
24+
25+
26+
def test_create_manager_param_annotations_are_optional():
27+
cases = [
28+
(SyncSessionManager.create, "params", CreateSessionParams),
29+
(AsyncSessionManager.create, "params", CreateSessionParams),
30+
(SyncProfileManager.create, "params", CreateProfileParams),
31+
(AsyncProfileManager.create, "params", CreateProfileParams),
32+
]
33+
34+
for method, param_name, expected_type in cases:
35+
type_hints = get_type_hints(method)
36+
assert _is_optional_annotation(type_hints[param_name], expected_type)

0 commit comments

Comments
 (0)