Skip to content

Invalidate cached returns when function bodies change - #959

Open
yangfan-yf-yf wants to merge 2 commits into
python-rope:masterfrom
yangfan-yf-yf:fix/function-return-body-cache
Open

yangfan-yf-yf wants to merge 2 commits into
python-rope:masterfrom
yangfan-yf-yf:fix/function-return-body-cache

Conversation

@yangfan-yf-yf

@yangfan-yf-yf yangfan-yf-yf commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Problem

After a function body changes, Rope can reuse a return type observed for the previous body. For example, when a callback changes from returning its first argument to its second argument, a refreshed or reopened project may still infer the first argument's type.

Change

I associate each recorded return with the function's own body and require a matching version when retrieving exact or fallback returns. Existing parameter and per-name observations remain available. Older persisted records without a body version retain their observations, but their returns are ignored for return inference until fresh information is available.

For dynamic analysis, a return is associated with a body only when the running frame matches the current source snapshot. This avoids attributing an old frame's return to a newly written body. If source matching reaches the recursion limit in either the analyzed program or the analysis process, the return remains unverified. An unverified observation cannot replace an existing verified return for the same arguments. The version tracks the function's own body; it does not track parameter signature changes, arbitrary global values, or transitive dependencies. For example, reordering parameters while leaving return x unchanged can still leave an older inferred return in place; that existing case is outside this change.

Validation

  • Python 3.12: full suite, 2192 passed, 12 skipped, 5 xfailed; Python 3.14: full suite, 2196 passed, 8 skipped, 5 xfailed. Both runs used the same final source and test snapshot.
  • Regression tests cover recursion limits in the analyzed program and the analysis process, unverified but decodable returns, and preserving an existing verified return when a later observation has no provenance.
  • Forward reading of legacy pickle and JSON records: four cases passed on each Python version against the same cache implementation.
  • Cold dynamic analysis with a larger import fixture: two cases passed on Python 3.14. Python 3.12 exceeded the original 20-second per-process bound twice; both cases passed with a 60-second bound. The earlier candidate also timed out under the original bound during recheck.
  • The source-rewrite regression isolates timestamp-based bytecode caching; the other dynamic-analysis cases retain normal bytecode behavior.
  • The project pre-commit checks passed on an isolated copy of all eleven changed files.

Checklist

  • I added tests that prove the fix.
  • I updated CHANGELOG.md.
  • I updated the library documentation.

@codecov

codecov Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.10390% with 21 lines in your changes missing coverage. Please review.
✅ Project coverage is 95.49%. Comparing base (ff8a306) to head (6ef1526).

Files with missing lines Patch % Lines
ropetest/refactor/ordinary_return_doa_test.py 95.93% 5 Missing ⚠️
...opetest/refactor/ordinary_return_recursion_test.py 90.56% 5 Missing ⚠️
ropetest/refactor/ordinary_return_unproved_test.py 89.58% 5 Missing ⚠️
rope/base/oi/objectinfo.py 93.18% 3 Missing ⚠️
ropetest/refactor/ordinary_return_cache_test.py 98.05% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #959      +/-   ##
==========================================
+ Coverage   95.44%   95.49%   +0.04%     
==========================================
  Files         134      139       +5     
  Lines       26910    27421     +511     
==========================================
+ Hits        25685    26186     +501     
- Misses       1225     1235      +10     

☔ View full report in Codecov by Harness.
📢 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.

This branch has not been deployed

No deployments
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