Skip to content

Commit ca59136

Browse files
committed
Defer requests import in PageIterator to reduce cold import time
Importing requests at module level pulls the entire requests package (plus charset_normalizer) into every `from msgraph import GraphServiceClient`, adding ~350 ms (~20-25%) to cold import in an SDK whose transport is httpx. requests is also not a declared dependency of this package. Import InvalidURL lazily at the single raise site instead; exception type and behavior are unchanged.
1 parent 73c69e3 commit ca59136

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

src/msgraph_core/tasks/page_iterator.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
server, and the PageResult class to represent the pages.
1212
1313
This module also imports the necessary types and exceptions from the
14-
typing, requests.exceptions, kiota_http.httpx_request_adapter,
14+
typing, kiota_http.httpx_request_adapter,
1515
kiota_abstractions.method, kiota_abstractions.headers_collection,
1616
kiota_abstractions.request_information, kiota_abstractions.serialization.parsable,
1717
and models modules.
@@ -25,7 +25,6 @@
2525
from kiota_abstractions.request_adapter import RequestAdapter
2626
from kiota_abstractions.request_information import RequestInformation
2727
from kiota_abstractions.serialization import Parsable, ParsableFactory
28-
from requests.exceptions import InvalidURL
2928

3029
from msgraph_core.models.page_result import (
3130
PageResult, # pylint: disable=no-name-in-module, import-error
@@ -201,6 +200,9 @@ async def fetch_next_page(self) -> Optional[Union[T, PageResult]]:
201200
if not next_link:
202201
raise ValueError('The response does not contain a nextLink.')
203202
if not next_link.startswith('http'):
203+
# Imported lazily: importing requests at module level adds ~350 ms
204+
# to every `from msgraph import GraphServiceClient` cold start.
205+
from requests.exceptions import InvalidURL
204206
raise InvalidURL('Could not parse nextLink URL.')
205207
request_info = RequestInformation()
206208
request_info.http_method = Method.GET

0 commit comments

Comments
 (0)