refactor: split data.py god-module and decompose B(10) blocks (#871) - #894
Merged
Conversation
data.py was the largest module in src/ (808 LOC) with the worst-ranking complexity blocks. This splits it and removes those hotspots: - Move the frame-construction/interpolation free functions into a new src/jquantstats/_utils/_construction.py (_to_polars, _apply_null_strategy, interpolate, _subtract_risk_free, _require_date_col, _align_returns_benchmark, plus a new _prices_to_returns). data.py re-imports them, so `jquantstats.data._subtract_risk_free` and `from jquantstats import interpolate` keep working. - Move the "derive a new Data" methods (resample, copy, head, tail, truncate and the _truncate_* helpers) into src/jquantstats/_data_reshape.py as _ReshapeMixin, which Data now inherits. The mixin carries empty __slots__ so the frozen slotted dataclass is unchanged; it rebuilds via a lazily-imported Data to avoid the accessor import cycle. - Decompose the B(10)/B(8) blocks: _apply_null_strategy -> B(5) (extracted _value_columns/_columns_with_nulls), Data.from_prices -> A(2) (extracted _prices_to_returns), _truncate_integer -> A (extracted _resolve_row_bound). Result: data.py drops 808 -> 453 LOC; no block in data.py exceeds B(6). make test (100% coverage), typecheck (ty + mypy strict) and docs-coverage (100%) all stay green. Closes #871 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors the Data entry point by splitting the former src/jquantstats/data.py “god-module” into smaller, focused modules while preserving the existing public API surface (Data methods and interpolate import path).
Changes:
- Moved frame construction / coercion / interpolation helpers into
src/jquantstats/_utils/_construction.pyand re-imported them fromdata.pyfor compatibility. - Moved “derive a new
Data” operations (resample/copy/head/tail/truncate) into a new_ReshapeMixininsrc/jquantstats/_data_reshape.py, then madeDatainherit the mixin. - Decomposed price→returns logic into
_prices_to_returnsand simplifiedData.from_prices.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/jquantstats/data.py |
Rewires imports to the new helper/mixin modules; Data now inherits _ReshapeMixin; from_prices delegates to _prices_to_returns. |
src/jquantstats/_utils/_construction.py |
New helper module containing Polars coercion, null strategy handling, interpolation, benchmark alignment, risk-free subtraction, and price→returns conversion. |
src/jquantstats/_data_reshape.py |
New _ReshapeMixin hosting Data reshaping/rebuilding operations and integer/temporal truncation logic. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+146
to
+153
| When the index is integer-based, row slicing is used instead, and | ||
| ``start`` and ``end`` must be non-negative integers. Passing | ||
| non-integer bounds to an integer-indexed Data raises `TypeError`. | ||
|
|
||
| Args: | ||
| start: Optional lower bound (inclusive). A date/datetime value | ||
| when the index is temporal; a non-negative `int` row | ||
| index when the data has no temporal index. |
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.
Summary
src/jquantstats/data.pywas the largest module insrc/(808 LOC) and held the worst-ranking complexity blocks. This splits it into focused modules and removes the complexity hotspots.Changes
src/jquantstats/_utils/_construction.py— the frame-construction/interpolation free functions move here:_to_polars,_apply_null_strategy,interpolate,_subtract_risk_free,_require_date_col,_align_returns_benchmark, plus a new_prices_to_returns.data.pyre-imports them, sojquantstats.data._subtract_risk_freeandfrom jquantstats import interpolatekeep working.src/jquantstats/_data_reshape.py— the "derive a newData" methods (resample,copy,head,tail,truncate,_truncate_*) move into_ReshapeMixin, whichDatainherits. The mixin carries empty__slots__(frozen slotted dataclass unchanged) and rebuilds via a lazily-importedDatato avoid the accessor import cycle._apply_null_strategyB(10) → B(5) (extracted_value_columns/_columns_with_nulls)Data.from_pricesB(10) → A(2) (extracted_prices_to_returns)_truncate_integerB(8) → A (extracted_resolve_row_bound, which also restores mypy int-narrowing)Done-when (issue #871)
data.py< 500 LOC — 808 → 453data.pyexceeds CC B(7) — worst is now B(6) (__post_init__,from_returns)make testgreen at 100% coverage (1215 passed)make typecheckgreen (ty + mypy strict)make docs-coveragegreen at 100%Compatibility
No public API change.
Data's methods and theinterpolateexport are unchanged;jquantstats.data._subtract_risk_free(used by a test) still resolves. Behaviour is preserved (all existing tests pass unmodified).Closes #871
🤖 Generated with Claude Code