Skip to content

Improve module import#805

Merged
vgvoleg merged 1 commit intomainfrom
simplify_package_import
Apr 10, 2026
Merged

Improve module import#805
vgvoleg merged 1 commit intomainfrom
simplify_package_import

Conversation

@vgvoleg
Copy link
Copy Markdown
Collaborator

@vgvoleg vgvoleg commented Apr 10, 2026

Reduce import ydb time by ~36% (158ms → 101ms) by deferring heavy
optional dependencies that were loaded unconditionally at import time.

Changes:

  • driver.py: lazy-import ydb.iam inside credentials_from_env_variables()
    instead of at module level
  • iam/auth.py: move import requests and import jwt from module level
    into the functions that actually use them (MetadataUrlCredentials.__init__,
    _make_token_request, get_jwt)

Neither requests nor jwt is needed unless the user explicitly creates
MetadataUrlCredentials or calls get_jwt. Previously they were imported
on every import ydb, pulling in the full requests/charset-normalizer/
urllib3/PyJWT/cryptography stack (~60ms) even for users who never use
IAM auth.

Pull request type

Please check the type of change your PR introduces:

  • Bugfix
  • Feature
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • Documentation content changes
  • Other (please describe):

What is the current behavior?

Issue Number: N/A

What is the new behavior?

Other information

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR reduces import ydb time by deferring optional/heavy IAM-related dependencies until they’re actually needed, avoiding unconditional imports of the requests/PyJWT stacks.

Changes:

  • Lazy-import ydb.iam in credentials_from_env_variables() instead of importing it at ydb.driver module import time.
  • Move requests and jwt imports in ydb.iam.auth from module scope into the specific functions/methods that require them.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
ydb/iam/auth.py Lazily imports jwt/requests in IAM credential helpers to avoid loading optional deps at import time.
ydb/driver.py Defers importing ydb.iam until metadata credentials are selected, reducing baseline module import overhead.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@vgvoleg vgvoleg merged commit 803d111 into main Apr 10, 2026
37 of 38 checks passed
@vgvoleg vgvoleg deleted the simplify_package_import branch April 10, 2026 14:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants