From ca591361a7126666adb95fe0cd50a39e841b63de Mon Sep 17 00:00:00 2001 From: Lamg222 Date: Thu, 11 Jun 2026 16:18:53 -0600 Subject: [PATCH 1/2] 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. --- src/msgraph_core/tasks/page_iterator.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/msgraph_core/tasks/page_iterator.py b/src/msgraph_core/tasks/page_iterator.py index dd56ca4c..12792de7 100644 --- a/src/msgraph_core/tasks/page_iterator.py +++ b/src/msgraph_core/tasks/page_iterator.py @@ -11,7 +11,7 @@ server, and the PageResult class to represent the pages. This module also imports the necessary types and exceptions from the -typing, requests.exceptions, kiota_http.httpx_request_adapter, +typing, kiota_http.httpx_request_adapter, kiota_abstractions.method, kiota_abstractions.headers_collection, kiota_abstractions.request_information, kiota_abstractions.serialization.parsable, and models modules. @@ -25,7 +25,6 @@ from kiota_abstractions.request_adapter import RequestAdapter from kiota_abstractions.request_information import RequestInformation from kiota_abstractions.serialization import Parsable, ParsableFactory -from requests.exceptions import InvalidURL from msgraph_core.models.page_result import ( PageResult, # pylint: disable=no-name-in-module, import-error @@ -201,6 +200,9 @@ async def fetch_next_page(self) -> Optional[Union[T, PageResult]]: if not next_link: raise ValueError('The response does not contain a nextLink.') if not next_link.startswith('http'): + # Imported lazily: importing requests at module level adds ~350 ms + # to every `from msgraph import GraphServiceClient` cold start. + from requests.exceptions import InvalidURL raise InvalidURL('Could not parse nextLink URL.') request_info = RequestInformation() request_info.http_method = Method.GET From 27b5879f49f47a8323d1a22a3d60f270fcde3549 Mon Sep 17 00:00:00 2001 From: ramsessanchez <63934382+ramsessanchez@users.noreply.github.com> Date: Mon, 29 Jun 2026 13:08:03 -0700 Subject: [PATCH 2/2] fix: disable pylint C0415 for intentional lazy import Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/msgraph_core/tasks/page_iterator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/msgraph_core/tasks/page_iterator.py b/src/msgraph_core/tasks/page_iterator.py index 12792de7..c7efd1c9 100644 --- a/src/msgraph_core/tasks/page_iterator.py +++ b/src/msgraph_core/tasks/page_iterator.py @@ -202,7 +202,7 @@ async def fetch_next_page(self) -> Optional[Union[T, PageResult]]: if not next_link.startswith('http'): # Imported lazily: importing requests at module level adds ~350 ms # to every `from msgraph import GraphServiceClient` cold start. - from requests.exceptions import InvalidURL + from requests.exceptions import InvalidURL # pylint: disable=import-outside-toplevel raise InvalidURL('Could not parse nextLink URL.') request_info = RequestInformation() request_info.http_method = Method.GET