Skip to content

Commit 5b19de5

Browse files
committed
Fix mypy issues and bring extra type hints
1 parent dd15658 commit 5b19de5

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

gql/client.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,9 @@ def _execute(self, document: DocumentNode, *args, **kwargs) -> ExecutionResult:
211211

212212
return self.transport.execute(document, *args, **kwargs)
213213

214-
def execute(self, document: DocumentNode, *args, **kwargs) -> Dict:
214+
def execute(
215+
self, document: DocumentNode, *args, **kwargs
216+
) -> Optional[Dict[str, Any]]:
215217

216218
# Validate and execute on the transport
217219
result = self._execute(document, *args, **kwargs)
@@ -225,7 +227,9 @@ def execute(self, document: DocumentNode, *args, **kwargs) -> Dict:
225227
), "Transport returned an ExecutionResult without data or errors"
226228

227229
if self.client.type_adapter:
228-
result.data = self.client.type_adapter.convert_scalars(result.data)
230+
result = result._replace(
231+
data=self.client.type_adapter.convert_scalars(result.data)
232+
)
229233

230234
return result.data
231235

@@ -289,7 +293,7 @@ async def _subscribe(
289293

290294
async def subscribe(
291295
self, document: DocumentNode, *args, **kwargs
292-
) -> AsyncGenerator[Dict, None]:
296+
) -> AsyncGenerator[Optional[Dict[str, Any]], None]:
293297

294298
# Validate and subscribe on the transport
295299
async for result in self._subscribe(document, *args, **kwargs):
@@ -300,7 +304,9 @@ async def subscribe(
300304

301305
elif result.data is not None:
302306
if self.client.type_adapter:
303-
result.data = self.client.type_adapter.convert_scalars(result.data)
307+
result = result._replace(
308+
data=self.client.type_adapter.convert_scalars(result.data)
309+
)
304310
yield result.data
305311

306312
async def _execute(
@@ -316,7 +322,9 @@ async def _execute(
316322
self.client.execute_timeout,
317323
)
318324

319-
async def execute(self, document: DocumentNode, *args, **kwargs) -> Dict:
325+
async def execute(
326+
self, document: DocumentNode, *args, **kwargs
327+
) -> Optional[Dict[str, Any]]:
320328

321329
# Validate and execute on the transport
322330
result = await self._execute(document, *args, **kwargs)
@@ -330,7 +338,9 @@ async def execute(self, document: DocumentNode, *args, **kwargs) -> Dict:
330338
), "Transport returned an ExecutionResult without data or errors"
331339

332340
if self.client.type_adapter:
333-
result.data = self.client.type_adapter.convert_scalars(result.data)
341+
result = result._replace(
342+
data=self.client.type_adapter.convert_scalars(result.data)
343+
)
334344

335345
return result.data
336346

gql/type_adapter.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Dict, List, Optional, Union
1+
from typing import Any, Dict, List, Optional, Union, cast
22

33
from graphql import GraphQLSchema
44
from graphql.type.definition import GraphQLField, GraphQLObjectType, GraphQLScalarType
@@ -88,7 +88,7 @@ def traverse_schema(
8888
except (KeyError, AttributeError):
8989
return None
9090

91-
def _get_decoded_scalar_type(self, keys: List[str], value: Any):
91+
def _get_decoded_scalar_type(self, keys: List[str], value: str) -> str:
9292
"""Get the decoded value of the type identified by `keys`.
9393
9494
If the type is not a custom scalar, then return the original value.
@@ -100,7 +100,7 @@ def _get_decoded_scalar_type(self, keys: List[str], value: Any):
100100
return self.custom_types[scalar_type].parse_value(value)
101101
return value
102102

103-
def convert_scalars(self, response: Dict[str, Any]):
103+
def convert_scalars(self, response: Dict[str, Any]) -> Dict[str, Any]:
104104
"""Recursively traverse the GQL response
105105
106106
Recursively traverses the GQL response and calls _get_decoded_scalar_type()
@@ -114,7 +114,9 @@ def convert_scalars(self, response: Dict[str, Any]):
114114
Builds a new tree with the substituted values so old `response` is not
115115
modified."""
116116

117-
def iterate(node: Union[List, Dict, str], keys: List[str] = None):
117+
def iterate(
118+
node: Union[List, Dict, str], keys: List[str] = None
119+
) -> Union[Dict[str, Any], List, str]:
118120
if keys is None:
119121
keys = []
120122
if isinstance(node, dict):
@@ -126,4 +128,4 @@ def iterate(node: Union[List, Dict, str], keys: List[str] = None):
126128
else:
127129
return self._get_decoded_scalar_type(keys, node)
128130

129-
return iterate(response)
131+
return cast(Dict, iterate(response))

0 commit comments

Comments
 (0)