Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Defer requests import in PageIterator to reduce cold import time#1089

Open
Lamg222 wants to merge 1 commit into
microsoftgraph:main from
Lamg222:fix/defer-requests-import
Open

Defer requests import in PageIterator to reduce cold import time #1089
Lamg222 wants to merge 1 commit into
microsoftgraph:main from
Lamg222:fix/defer-requests-import

Conversation

@Lamg222

@Lamg222 Lamg222 commented Jun 11, 2026

Copy link
Copy Markdown

Problem

msgraph_core/tasks/page_iterator.py imports InvalidURL from requests.exceptions at module level. This is the only use of requests in msgraph-core, and it drags the whole requests package (+ its charset_normalizer dependency) into every from msgraph import GraphServiceClient — ~350 ms cumulative, roughly 20–25 % of cold import time (profiled with python -X importtime, msgraph-sdk 1.58.0, Python 3.11, Linux). The SDK transport is httpx, so requests is loaded purely for one exception class.

Additionally, requests is not a declared dependency of this package (not in pyproject.toml) — it currently works only because it arrives transitively.

Context: profiling details in microsoftgraph/msgraph-sdk-python#904.

Change

Import InvalidURL lazily at the single raise site in fetch_next_page. Exception type and behavior are unchanged, so existing code catching requests.exceptions.InvalidURL keeps working.

Verification

  • python -c "from msgraph_core.tasks.page_iterator import PageIterator" no longer loads requests (verified via sys.modules)
  • Full test suite: 74 passed

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.
@Lamg222 Lamg222 requested a review from a team as a code owner June 11, 2026 22:20

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

1 participant

AltStyle によって変換されたページ (->オリジナル) /