Skip to content

Lazy imports for pandas, numpy and sklearn - #79

Merged
simo-prior merged 4 commits into
mainfrom
slim-heavy-imports
Jul 21, 2026
Merged

Lazy imports for pandas, numpy and sklearn#79
simo-prior merged 4 commits into
mainfrom
slim-heavy-imports

Conversation

@simo-prior

@simo-prior simo-prior commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Why

utils.py imports pandas, numpy and scikit-learn at module level, but the telemetry package only needs its light helpers (singleton, ttl_cache, shape_of). This forces every consumer of tabpfn_common_utils.telemetry — including API services that never touch a DataFrame — to have the full data stack importable.

What

Move the pandas/numpy/sklearn imports inside the four functions that actually use them (serialize_to_csv_formatted_bytes, get_example_dataset, get_dataset_with_specific_size, assert_y_pred_proba_is_valid). Signatures are unchanged (from __future__ import annotations + TYPE_CHECKING imports).

Declared dependencies are deliberately NOT touched: every consumer keeps installing exactly what it does today. Slim consumers that install the package without its declared deps (e.g. gapi in tabpfn-server) can now import the telemetry modules safely; the heavy helpers raise ImportError only when called without the stack installed.

(Note: an intermediate commit moved numpy/pandas/scikit-learn to a datasets extra; it was reverted to keep install behavior unchanged for existing consumers.)

Testing

  • Verified import tabpfn_common_utils.telemetry and ...utils succeed with a meta-path hook that blocks pandas/numpy/sklearn/scipy.
  • pytest (169 passed), ruff check, ruff format --check, pyright (0 errors) all clean.

🤖 Generated with Claude Code

The telemetry package only needs the light helpers (singleton, ttl_cache,
shape_of) from utils.py, but the module-level pandas/numpy/sklearn imports
forced every consumer of telemetry to install the full data stack. Defer
those imports to the four dataset/CSV helper functions that actually use
them, so API services can import telemetry without pandas installed.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@simo-prior
simo-prior requested a review from a team as a code owner July 21, 2026 11:17
@simo-prior
simo-prior requested review from eliott-kalfon and removed request for a team July 21, 2026 11:17

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors src/tabpfn_common_utils/utils.py to lazily import heavy dependencies like pandas, numpy, and scikit-learn. By using from __future__ import annotations and moving these imports inside the functions that require them (or within a TYPE_CHECKING block), the module can be imported in environments without the heavy data stack installed. There are no review comments, and I have no feedback to provide.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

With utils.py importing them lazily, nothing in the default install path
needs the data stack anymore. Consumers that use the dataset/array helpers
(get_example_dataset, regression_pred_result, expense_estimation, ...)
install `tabpfn-common-utils[datasets]`; ML consumers (tabpfn,
tabpfn-client) already declare these packages directly.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 6a34615. Configure here.

Comment thread pyproject.toml Outdated
@simo-prior
simo-prior marked this pull request as draft July 21, 2026 11:24
@simo-prior simo-prior changed the title Import pandas/numpy/sklearn lazily in utils.py Lazy imports for pandas, numpy and sklearn Jul 21, 2026
@simo-prior
simo-prior marked this pull request as ready for review July 21, 2026 11:29
@eliott-kalfon
eliott-kalfon requested a review from oscarkey July 21, 2026 11:45

@oscarkey oscarkey left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lgtm!

Comment thread src/tabpfn_common_utils/utils.py Outdated
Co-authored-by: Oscar <oscar@priorlabs.ai>
@simo-prior
simo-prior merged commit 3e4c992 into main Jul 21, 2026
13 checks passed
@simo-prior
simo-prior deleted the slim-heavy-imports branch July 21, 2026 11:54
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.

3 participants