Skip to content

Commit 8e20a51

Browse files
authored
Merge pull request #773 from microsoftgraph/fix/failing-static-checks
Fix failing static type checks
2 parents 80cb43b + eb27bdd commit 8e20a51

17 files changed

+249
-170
lines changed

pyproject.toml

-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ documentation = "https://github.com/microsoftgraph/msgraph-sdk-python-core/docs"
4141
[tool.mypy]
4242
warn_unused_configs = true
4343
files = "src"
44-
ignore_missing_imports = true
4544

4645
[tool.yapf]
4746
based_on_style = "pep8"

requirements-dev.txt

+9-3
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ python-dotenv==1.0.1
8888

8989
pytest-trio==0.8.0
9090

91+
pytest-asyncio==0.24.0
92+
9193
pywin32==308 ; platform_system == 'Windows'
9294

9395
requests==2.32.3 ; python_version >= '3.7'
@@ -143,14 +145,18 @@ httpx[http2]==0.28.1
143145

144146
hyperframe==6.0.1 ; python_full_version >= '3.6.1'
145147

146-
microsoft-kiota-abstractions==1.3.3
148+
microsoft-kiota-abstractions==1.6.6
147149

148-
microsoft-kiota-authentication-azure==1.1.0
150+
microsoft-kiota-authentication-azure==1.6.6
149151

150-
microsoft-kiota-http==1.3.3
152+
microsoft-kiota-http==1.6.6
151153

152154
multidict==6.1.0 ; python_version >= '3.7'
153155

154156
uritemplate==4.1.1 ; python_version >= '3.6'
155157

156158
yarl==1.15.2 ; python_version >= '3.7'
159+
160+
deprecated==1.2.15
161+
162+
types-Deprecated==1.2.15.20241117

src/msgraph_core/authentication/azure_identity_authentication_provider.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def __init__(
1919
credentials: Union["TokenCredential", "AsyncTokenCredential"],
2020
options: Optional[Dict] = {},
2121
scopes: List[str] = [],
22-
allowed_hosts: Optional[List[str]] = [nc.value for nc in NationalClouds]
22+
allowed_hosts: List[str] = [nc.value for nc in NationalClouds]
2323
) -> None:
2424
"""[summary]
2525

src/msgraph_core/graph_client_factory.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ class GraphClientFactory(KiotaClientFactory):
2222
"""
2323

2424
@staticmethod
25-
def create_with_default_middleware(
25+
def create_with_default_middleware( # type: ignore
26+
# Breaking change to remove KiotaClientFactory as base class
2627
api_version: APIVersion = APIVersion.v1,
2728
client: Optional[httpx.AsyncClient] = None,
2829
host: NationalClouds = NationalClouds.Global,
@@ -53,7 +54,8 @@ def create_with_default_middleware(
5354
return GraphClientFactory._load_middleware_to_client(client, middleware)
5455

5556
@staticmethod
56-
def create_with_custom_middleware(
57+
def create_with_custom_middleware( # type: ignore
58+
# Breaking change to remove Kiota client factory as base class
5759
middleware: Optional[List[BaseMiddleware]],
5860
api_version: APIVersion = APIVersion.v1,
5961
client: Optional[httpx.AsyncClient] = None,
@@ -91,7 +93,9 @@ def _get_telemetry_handler(
9193
options"""
9294

9395
if options:
94-
graph_telemetry_options = options.get(GraphTelemetryHandlerOption().get_key())
96+
graph_telemetry_options: GraphTelemetryHandlerOption = options.get(
97+
GraphTelemetryHandlerOption().get_key()
98+
) # type: ignore # Unable to down cast type
9599
if graph_telemetry_options:
96100
return GraphTelemetryHandler(options=graph_telemetry_options)
97101
return GraphTelemetryHandler()

src/msgraph_core/models/large_file_upload_session.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from __future__ import annotations
2-
from typing import Any, Callable, Dict, List, Optional, TYPE_CHECKING, Union
2+
from typing import Any, Callable, Dict, List, Optional
33
import datetime
44
from dataclasses import dataclass, field
55

@@ -25,7 +25,7 @@ def create_from_discriminator_value(
2525
) -> LargeFileUploadSession:
2626
"""
2727
Creates a new instance of the appropriate class based
28-
on discriminator value param parse_node: The parse node
28+
on discriminator value param parse_node: The parse node
2929
to use to read the discriminator value and create the object
3030
Returns: UploadSession
3131
"""

src/msgraph_core/models/page_result.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,17 @@ def get_field_deserializers(self) -> Dict[str, Callable[[ParseNode], None]]:
4646
object where each entry is a property key with its deserialization callback.
4747
"""
4848
return {
49-
"@odata.nextLink": lambda x: setattr(self, "odata_next_link", x.get_str_value()),
50-
"value": lambda x: setattr(self, "value", x.get_collection_of_object_values(Parsable))
49+
"@odata.nextLink":
50+
lambda x: setattr(self, "odata_next_link", x.get_str_value()),
51+
"value":
52+
lambda x: setattr(
53+
self,
54+
"value",
55+
x.get_collection_of_object_values(
56+
Parsable # type: ignore
57+
# Bug. Should get a collection of primitive dictionary objects
58+
)
59+
)
5160
}
5261

5362
def serialize(self, writer: SerializationWriter) -> None:

src/msgraph_core/requests/batch_request_builder.py

+16-22
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from kiota_abstractions.request_adapter import RequestAdapter
55
from kiota_abstractions.request_information import RequestInformation
66
from kiota_abstractions.method import Method
7-
from kiota_abstractions.serialization import Parsable
7+
from kiota_abstractions.serialization import Parsable, ParsableFactory
88
from kiota_abstractions.headers_collection import HeadersCollection
99
from kiota_abstractions.api_error import APIError
1010

@@ -26,7 +26,7 @@ class BatchRequestBuilder:
2626
def __init__(
2727
self,
2828
request_adapter: RequestAdapter,
29-
error_map: Optional[Dict[str, Type[Parsable]]] = None
29+
error_map: Optional[Dict[str, Type[ParsableFactory]]] = None
3030
):
3131
if request_adapter is None:
3232
raise ValueError("request_adapter cannot be Null.")
@@ -37,20 +37,19 @@ def __init__(
3737
async def post(
3838
self,
3939
batch_request_content: Union[BatchRequestContent, BatchRequestContentCollection],
40-
error_map: Optional[Dict[str, Type[Parsable]]] = None,
41-
) -> Union[T, BatchResponseContentCollection]:
40+
error_map: Optional[Dict[str, Type[ParsableFactory]]] = None,
41+
) -> Union[BatchResponseContent, BatchResponseContentCollection]:
4242
"""
4343
Sends a batch request and returns the batch response content.
44-
44+
4545
Args:
46-
batch_request_content (Union[BatchRequestContent,
46+
batch_request_content (Union[BatchRequestContent,
4747
BatchRequestContentCollection]): The batch request content.
48-
response_type: Optional[Type[T]] : The type to deserialize the response into.
49-
Optional[Dict[str, Type[Parsable]]] = None:
48+
Optional[Dict[str, Type[ParsableFactory]]] = None:
5049
Error mappings for response handling.
5150
5251
Returns:
53-
Union[T, BatchResponseContentCollection]: The batch response content
52+
Union[BatchResponseContent, BatchResponseContentCollection]: The batch response content
5453
or the specified response type.
5554
5655
"""
@@ -60,11 +59,6 @@ async def post(
6059

6160
if isinstance(batch_request_content, BatchRequestContent):
6261
request_info = await self.to_post_request_information(batch_request_content)
63-
bytes_content = request_info.content
64-
json_content = bytes_content.decode("utf-8")
65-
updated_str = '{"requests":' + json_content + '}'
66-
updated_bytes = updated_str.encode("utf-8")
67-
request_info.content = updated_bytes
6862
error_map = error_map or self.error_map
6963
response = None
7064
try:
@@ -87,15 +81,15 @@ async def post(
8781
async def _post_batch_collection(
8882
self,
8983
batch_request_content_collection: BatchRequestContentCollection,
90-
error_map: Optional[Dict[str, Type[Parsable]]] = None,
84+
error_map: Optional[Dict[str, Type[ParsableFactory]]] = None,
9185
) -> BatchResponseContentCollection:
9286
"""
9387
Sends a collection of batch requests and returns a collection of batch response contents.
94-
88+
9589
Args:
96-
batch_request_content_collection (BatchRequestContentCollection): The
90+
batch_request_content_collection (BatchRequestContentCollection): The
9791
collection of batch request contents.
98-
Optional[Dict[str, Type[Parsable]]] = None:
92+
Optional[Dict[str, Type[ParsableFactory]]] = None:
9993
Error mappings for response handling.
10094
10195
Returns:
@@ -108,7 +102,8 @@ async def _post_batch_collection(
108102
batch_requests = batch_request_content_collection.get_batch_requests_for_execution()
109103
for batch_request_content in batch_requests:
110104
response = await self.post(batch_request_content, error_map)
111-
batch_responses.add_response(response)
105+
if isinstance(response, BatchResponseContent):
106+
batch_responses.add_response(response)
112107

113108
return batch_responses
114109

@@ -117,7 +112,7 @@ async def to_post_request_information(
117112
) -> RequestInformation:
118113
"""
119114
Creates request information for a batch POST request.
120-
115+
121116
Args:
122117
batch_request_content (BatchRequestContent): The batch request content.
123118
@@ -127,15 +122,14 @@ async def to_post_request_information(
127122

128123
if batch_request_content is None:
129124
raise ValueError("batch_request_content cannot be Null.")
130-
batch_request_items = list(batch_request_content.requests.values())
131125

132126
request_info = RequestInformation()
133127
request_info.http_method = Method.POST
134128
request_info.url_template = self.url_template
135129
request_info.headers = HeadersCollection()
136130
request_info.headers.try_add("Content-Type", APPLICATION_JSON)
137131
request_info.set_content_from_parsable(
138-
self._request_adapter, APPLICATION_JSON, batch_request_items
132+
self._request_adapter, APPLICATION_JSON, batch_request_content
139133
)
140134

141135
return request_info

src/msgraph_core/requests/batch_request_content.py

+41-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import uuid
22
from typing import List, Dict, Union, Optional
3+
from urllib.request import Request
34

45
from kiota_abstractions.request_information import RequestInformation
56
from kiota_abstractions.serialization import Parsable, ParseNode
@@ -15,27 +16,36 @@ class BatchRequestContent(Parsable):
1516

1617
MAX_REQUESTS = 20
1718

18-
def __init__(self, requests: Dict[str, Union['BatchRequestItem', 'RequestInformation']] = {}):
19+
def __init__(self, requests: Dict[str, Union[BatchRequestItem, RequestInformation]] = {}):
1920
"""
2021
Initializes a new instance of the BatchRequestContent class.
22+
Args:
23+
Requests (Dict[str, Union[BatchRequestItem, RequestInformation]]): The requests to add.
2124
"""
22-
self._requests: Dict[str, Union[BatchRequestItem, 'RequestInformation']] = requests or {}
25+
self._requests: Dict[str, BatchRequestItem] = {}
2326

2427
self.is_finalized = False
2528
for request_id, request in requests.items():
29+
if isinstance(request, RequestInformation):
30+
self.add_request_information(request, request_id)
31+
continue
2632
self.add_request(request_id, request)
2733

2834
@property
29-
def requests(self) -> Dict:
35+
def requests(self) -> Dict[str, BatchRequestItem]:
3036
"""
3137
Gets the requests.
38+
Returns:
39+
Dict[str, BatchRequestItem]: requests in the batch request content.
3240
"""
3341
return self._requests
3442

3543
@requests.setter
3644
def requests(self, requests: List[BatchRequestItem]) -> None:
3745
"""
3846
Sets the requests.
47+
Args:
48+
requests (List[BatchRequestItem]): The requests to set.
3949
"""
4050
if len(requests) >= BatchRequestContent.MAX_REQUESTS:
4151
raise ValueError(f"Maximum number of requests is {BatchRequestContent.MAX_REQUESTS}")
@@ -45,43 +55,56 @@ def requests(self, requests: List[BatchRequestItem]) -> None:
4555
def add_request(self, request_id: Optional[str], request: BatchRequestItem) -> None:
4656
"""
4757
Adds a request to the batch request content.
58+
Args:
59+
request_id (Optional[str]): The request id to add.
60+
request (BatchRequestItem): The request to add.
4861
"""
4962
if len(self.requests) >= BatchRequestContent.MAX_REQUESTS:
5063
raise RuntimeError(f"Maximum number of requests is {BatchRequestContent.MAX_REQUESTS}")
5164
if not request.id:
52-
request.id = str(uuid.uuid4())
65+
request.id = request_id if request_id else str(uuid.uuid4())
5366
if hasattr(request, 'depends_on') and request.depends_on:
5467
for dependent_id in request.depends_on:
55-
if dependent_id not in self.requests:
56-
dependent_request = self._request_by_id(dependent_id)
57-
if dependent_request:
58-
self._requests[dependent_id] = dependent_request
68+
if not self._request_by_id(dependent_id):
69+
raise ValueError(
70+
f"""
71+
Request depends on request id: {dependent_id}
72+
which was not found in requests. Add request id: {dependent_id} first"""
73+
)
5974
self._requests[request.id] = request
6075

61-
def add_request_information(self, request_information: RequestInformation) -> None:
62-
"""
76+
def add_request_information(
77+
self, request_information: RequestInformation, request_id: Optional[str] = None
78+
) -> None:
79+
"""
6380
Adds a request to the batch request content.
6481
Args:
6582
request_information (RequestInformation): The request information to add.
83+
request_id: Optional[str]: The request id to add.
6684
"""
67-
request_id = str(uuid.uuid4())
85+
request_id = request_id if request_id else str(uuid.uuid4())
6886
self.add_request(request_id, BatchRequestItem(request_information))
6987

70-
def add_urllib_request(self, request) -> None:
88+
def add_urllib_request(self, request: Request, request_id: Optional[str] = None) -> None:
7189
"""
7290
Adds a request to the batch request content.
91+
Args:
92+
request (Request): The request to add.
93+
request_id: Optional[str]: The request id to add.
7394
"""
74-
request_id = str(uuid.uuid4())
95+
request_id = request_id if request_id else str(uuid.uuid4())
7596
self.add_request(request_id, BatchRequestItem.create_with_urllib_request(request))
7697

7798
def remove(self, request_id: str) -> None:
7899
"""
79100
Removes a request from the batch request content.
80-
Also removes the request from the depends_on list of
101+
Also removes the request from the depends_on list of
81102
other requests.
103+
Args:
104+
request_id (str): The request id to remove.
82105
"""
83106
request_to_remove = None
84-
for request in self.requests:
107+
for request in self.requests.values():
85108
if request.id == request_id:
86109
request_to_remove = request
87110
if hasattr(request, 'depends_on') and request.depends_on:
@@ -108,12 +131,12 @@ def finalize(self):
108131
def _request_by_id(self, request_id: str) -> Optional[BatchRequestItem]:
109132
"""
110133
Finds a request by its ID.
111-
134+
112135
Args:
113136
request_id (str): The ID of the request to find.
114137
115138
Returns:
116-
The request with the given ID, or None if not found.
139+
Optional[BatchRequestItem]: The request with the given ID, or None if not found.
117140
"""
118141
return self._requests.get(request_id)
119142

@@ -137,4 +160,4 @@ def serialize(self, writer: SerializationWriter) -> None:
137160
Args:
138161
writer: Serialization writer to use to serialize this model
139162
"""
140-
writer.write_collection_of_object_values("requests", self.requests)
163+
writer.write_collection_of_object_values("requests", list(self.requests.values()))

0 commit comments

Comments
 (0)