Skip to content

Commit 4fb808b

Browse files
committed
Fix typing issues with Pydantic
1 parent ba0c7cc commit 4fb808b

File tree

4 files changed

+58
-34
lines changed

4 files changed

+58
-34
lines changed

tests/test_fga.py

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,8 @@ def setup(self, sync_http_client_for_test):
107107
self.http_client = sync_http_client_for_test
108108
self.fga = FGA(http_client=self.http_client)
109109

110-
@pytest.fixture
111-
def mock_check_warning_response(self):
112-
return {
110+
def test_check_with_warning(self, mock_http_client_with_response):
111+
mock_response = {
113112
"result": "authorized",
114113
"is_implicit": True,
115114
"warnings": [
@@ -120,10 +119,23 @@ def mock_check_warning_response(self):
120119
}
121120
],
122121
}
122+
mock_http_client_with_response(self.http_client, mock_response, 200)
123123

124-
@pytest.fixture
125-
def mock_query_warning_response(self):
126-
return {
124+
response = self.fga.check(
125+
op="any_of",
126+
checks=[
127+
WarrantCheckInput(
128+
resource_type="schedule",
129+
resource_id="schedule-A1",
130+
relation="viewer",
131+
subject=SubjectInput(resource_type="user", resource_id="user-A"),
132+
)
133+
],
134+
)
135+
assert response.dict(exclude_none=True) == mock_response
136+
137+
def test_query_with_warning(self, mock_http_client_with_response):
138+
mock_response = {
127139
"object": "list",
128140
"data": [
129141
{
@@ -149,12 +161,28 @@ def mock_query_warning_response(self):
149161
],
150162
}
151163

152-
def test_check_with_warning(
153-
self, mock_check_warning_response, mock_http_client_with_response
154-
):
155-
mock_http_client_with_response(
156-
self.http_client, mock_check_warning_response, 200
164+
mock_http_client_with_response(self.http_client, mock_response, 200)
165+
166+
response = self.fga.query(
167+
q="select member of type user for permission:view-docs",
168+
order="asc",
169+
warrant_token="warrant_token",
157170
)
171+
assert response.dict(exclude_none=True) == mock_response
172+
173+
def test_check_with_generic_warning(self, mock_http_client_with_response):
174+
mock_response = {
175+
"result": "authorized",
176+
"is_implicit": True,
177+
"warnings": [
178+
{
179+
"code": "generic",
180+
"message": "Generic warning",
181+
}
182+
],
183+
}
184+
185+
mock_http_client_with_response(self.http_client, mock_response, 200)
158186

159187
response = self.fga.check(
160188
op="any_of",
@@ -167,21 +195,7 @@ def test_check_with_warning(
167195
)
168196
],
169197
)
170-
assert response.dict(exclude_none=True) == mock_check_warning_response
171-
172-
def test_query_with_warning(
173-
self, mock_query_warning_response, mock_http_client_with_response
174-
):
175-
mock_http_client_with_response(
176-
self.http_client, mock_query_warning_response, 200
177-
)
178-
179-
response = self.fga.query(
180-
q="select member of type user for permission:view-docs",
181-
order="asc",
182-
warrant_token="warrant_token",
183-
)
184-
assert response.dict(exclude_none=True) == mock_query_warning_response
198+
assert response.dict(exclude_none=True) == mock_response
185199

186200

187201
class TestFGA:

workos/fga.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
WarrantWrite,
1111
WarrantWriteOperation,
1212
WriteWarrantResponse,
13-
WarrantQueryResult, FGAWarning,
13+
WarrantQueryResult,
14+
FGAWarning,
1415
)
1516
from workos.types.fga.list_filters import (
1617
AuthorizationResourceListFilters,
@@ -46,9 +47,9 @@
4647
WarrantListResource = WorkOSListResource[Warrant, WarrantListFilters, ListMetadata]
4748

4849

49-
class WarrantQueryListResource(WorkOSListResource[
50-
WarrantQueryResult, WarrantQueryListFilters, ListMetadata
51-
]):
50+
class WarrantQueryListResource(
51+
WorkOSListResource[WarrantQueryResult, WarrantQueryListFilters, ListMetadata]
52+
):
5253
warnings: Optional[Sequence[FGAWarning]] = None
5354

5455

workos/types/fga/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
from .authorization_resource_types import *
33
from .authorization_resources import *
44
from .warrant import *
5+
from .warnings import *

workos/types/fga/warnings.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from typing import Sequence, Union, Literal, Annotated
1+
from typing import Sequence, Annotated, Union, Any, Dict
22

3-
from pydantic import Field
3+
from pydantic import BeforeValidator
4+
from pydantic_core.core_schema import ValidationInfo
45

56
from workos.types.workos_model import WorkOSModel
67

@@ -11,11 +12,18 @@ class FGABaseWarning(WorkOSModel):
1112

1213

1314
class MissingContextKeysWarning(FGABaseWarning):
14-
code: Literal["missing_context_keys"]
1515
keys: Sequence[str]
1616

1717

18+
def fga_warning_dispatch_validator(
19+
value: Dict[str, Any], info: ValidationInfo
20+
) -> FGABaseWarning:
21+
if value.get("code") == "missing_context_keys":
22+
return MissingContextKeysWarning.model_validate(value)
23+
return FGABaseWarning.model_validate(value)
24+
25+
1826
FGAWarning = Annotated[
1927
Union[MissingContextKeysWarning, FGABaseWarning],
20-
Field(discriminator='type')
28+
BeforeValidator(fga_warning_dispatch_validator),
2129
]

0 commit comments

Comments
 (0)