Skip to content

fix: support functools.cached_property#335

Merged
mcous merged 1 commit into
mainfrom
fix/cached-property
Apr 28, 2026
Merged

fix: support functools.cached_property#335
mcous merged 1 commit into
mainfrom
fix/cached-property

Conversation

@mcous
Copy link
Copy Markdown
Owner

@mcous mcous commented Apr 27, 2026

Summary by CodeRabbit

  • Bug Fixes

    • Corrected mock handling for functools.cached_property so mocked cached properties resolve to the annotated return type.
    • Fixed behavior for non-callable descriptor attributes so accessing them on mocks no longer fails.
    • Improved handling of bound and routine-like attributes so mocked callables report signatures and binding consistently.
  • Tests

    • Added tests covering functools.cached_property mocking scenarios.
    • Added tests for non-callable descriptor behavior on mocks.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (8229d6b) to head (c5326fd).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #335   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           32        32           
  Lines         1543      1548    +5     
  Branches       191       194    +3     
=========================================
+ Hits          1543      1548    +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mcous mcous marked this pull request as ready for review April 28, 2026 01:06
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 28, 2026

Caution

Review failed

Pull request was closed or merged during review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 2ed8ec0f-2d67-4fdc-9b43-ae71290a53fe

📥 Commits

Reviewing files that changed from the base of the PR and between 18cf22f and c5326fd.

📒 Files selected for processing (4)
  • decoy/next/_internal/inspect.py
  • decoy/spy_core.py
  • tests/legacy/test_mock.py
  • tests/test_mock.py
✅ Files skipped from review due to trivial changes (1)
  • decoy/spy_core.py
🚧 Files skipped from review as they are similar to previous changes (2)
  • tests/legacy/test_mock.py
  • decoy/next/_internal/inspect.py

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of functools.cached_property and custom descriptors when generating mocks and spies for class attributes.
    • Enhanced method signature adjustment to correctly bind parameters for descriptor-based attributes.
  • Tests

    • Added test coverage for mocking classes with functools.cached_property attributes.
    • Added test coverage for mocking classes with non-callable custom descriptors.

Walkthrough

get_child_spec stops using inspect.unwrap and branches on raw descriptor types: handles property, functools.cached_property, staticmethod, and classmethod to extract/adjust annotated return types or bind away receivers; routines partially consume self. SpyCore.create_child_core similarly resolves cached_property and tightens partial-binding checks. Tests for descriptor cases were added.

Changes

Cohort / File(s) Summary
Spec resolution logic
decoy/next/_internal/inspect.py
Rewrote get_child_spec to avoid inspect.unwrap; branch on descriptor kinds — use getter return hints for property and functools.cached_property, return __func__ for staticmethod, partially apply __func__ for classmethod, and partially bind routines by consuming self; always apply _unwrap_type_alias to sources.
Spy core behavior
decoy/spy_core.py
SpyCore.create_child_core now resolves functools.cached_property by reading the underlying function's return annotation and only applies functools.partial to routines when inspect.isroutine AND callable are both true.
Tests
tests/legacy/test_mock.py, tests/test_mock.py
Added tests for functools.cached_property (conditionally skipped on older Python) and for non-callable descriptors to ensure mocked attribute access returns a Spy/Mock and respects annotated return types.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 I peeked at descriptors, both cached and shy,
I nibbled return hints till types hopped by.
Static, class, and quirky I now embrace,
Partials bound neatly — a tidy little place.
Hooray for hops and tests that verify!

🚥 Pre-merge checks | ✅ 3 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Description check ❓ Inconclusive No pull request description was provided by the author, making it impossible to assess whether the description relates to the changeset. Add a pull request description explaining the motivation, changes made, and how functools.cached_property support improves the codebase.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The PR title follows Conventional Commits format with 'fix:' prefix and clearly describes the main change: adding support for functools.cached_property handling.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@tests/legacy/test_mock.py`:
- Around line 344-358: Fix the docstring typo in the test_cached_property test:
replace "It can mock a cached property classes." with a correct phrase such as
"It can mock cached properties." Locate the test function named
test_cached_property (and the inner class _Spec) and update its triple-quoted
docstring accordingly to remove the grammatical error.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: b50738ed-96df-4856-91cd-1d1fe2373903

📥 Commits

Reviewing files that changed from the base of the PR and between 8b3dfaf and c233c9b.

📒 Files selected for processing (4)
  • decoy/next/_internal/inspect.py
  • decoy/spy_core.py
  • tests/legacy/test_mock.py
  • tests/test_mock.py

Comment thread tests/legacy/test_mock.py
@mcous mcous force-pushed the fix/cached-property branch from c233c9b to 18cf22f Compare April 28, 2026 01:19
@mcous mcous enabled auto-merge (squash) April 28, 2026 16:24
@mcous mcous force-pushed the fix/cached-property branch from 18cf22f to c5326fd Compare April 28, 2026 16:25
@mcous mcous merged commit dc712d2 into main Apr 28, 2026
17 of 18 checks passed
@mcous mcous deleted the fix/cached-property branch April 28, 2026 16:27
@mcous mcous mentioned this pull request Apr 30, 2026
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.

1 participant