Lazy-import oauthlib/httpx and relax token type hint#6260
Open
FeodorFitsner wants to merge 1 commit intomainfrom
Open
Lazy-import oauthlib/httpx and relax token type hint#6260FeodorFitsner wants to merge 1 commit intomainfrom
FeodorFitsner wants to merge 1 commit intomainfrom
Conversation
Move imports of oauthlib.WebApplicationClient and httpx from module top-level into the functions that use them to enable lazy importing and avoid import-time dependency issues. Also remove the explicit OAuth2Token type annotation from __convert_token to avoid importing oauthlib types at module import. These changes reduce startup import overhead and make the module safer when oauthlib/httpx may not be available until runtime. Fix #6258
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces lazy imports for oauthlib and httpx within the authorization service to avoid import-time dependency issues and reduce startup overhead.
Changes:
- Moved
httpxandoauthlib.oauth2.WebApplicationClientimports from module scope into the methods that use them - Removed the
OAuth2Tokentype annotation from__convert_tokento avoid importing oauthlib token types at module import time
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| ) | ||
|
|
||
| def __convert_token(self, t: OAuth2Token): | ||
| def __convert_token(self, t): |
There was a problem hiding this comment.
Removing the parameter type entirely loses useful static guarantees for callers and maintainers. To keep lazy-import behavior without depending on oauthlib at import time, consider using a stdlib type such as Mapping[str, object] / dict[str, object] (or Mapping[str, Any]) for t, or a local TYPE_CHECKING import to preserve the precise oauthlib type only for type checkers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Move imports of oauthlib.WebApplicationClient and httpx from module top-level into the functions that use them to enable lazy importing and avoid import-time dependency issues. Also remove the explicit OAuth2Token type annotation from __convert_token to avoid importing oauthlib types at module import. These changes reduce startup import overhead and make the module safer when oauthlib/httpx may not be available until runtime.
Fix #6258
Summary by Sourcery
Make OAuth-related imports lazy and relax token typing to avoid import-time dependencies and reduce startup overhead.
Enhancements: