Skip to content

Normalize cachedmethod keys for unbound calls - #541

Closed
theman6660 wants to merge 1 commit into
mahmoud:masterfrom
theman6660:codex/fix-cachedmethod-unbound-keys
Closed

theman6660 wants to merge 1 commit into
mahmoud:masterfrom
theman6660:codex/fix-cachedmethod-unbound-keys

Conversation

@theman6660

Copy link
Copy Markdown

The change

obj.method(3) and Class.method(obj, 3) generate different cache keys for the same cached method. Calling both currently runs the method twice and stores two entries. With scoped=False, an unbound call also includes the instance in the key, so unhashable instances fail even though the equivalent bound call succeeds.

Separate the receiver from the arguments before building either form's key. Use (obj, func) only when scoped, and always pass the receiver when invoking the underlying function. Bound-call keys stay unchanged, and unbound calls reuse them. Existing custom key functions receive the same normalized arguments for both invocation forms.

Regression coverage checks both call orders, attribute/callable/mapping caches, scoped and typed configurations, keyword arguments, unhashable unscoped receivers, custom key callbacks, instance isolation/sharing, and numeric type distinctions.

Validation on Windows / Python 3.12.10:

  • The initial 27 regression cases all failed before the fix.
  • Complete source tests and module doctests: 709 passed, 12 subtests passed.
  • Built-wheel tests and doctests outside the checkout, with the import path verified: 709 passed, 12 subtests passed.
  • git diff --check: passed.

Other interpreter versions and operating systems were not tested locally.

The backstory

Previous contribution: #519, which includes my contribution background. This was found while comparing bound and unbound calls during a source inspection for small, verifiable contributions, rather than from a production incident.

AI assistance

  • Harness/tooling: OpenAI Codex desktop; source inspection, implementation, tests, validation and this description were prepared with Codex assistance under the contributor's authorization.
  • Model(s): GPT-6-based Codex; exact serving-model identifier unavailable.
  • Original prompt, verbatim: “去多做几个” (“Go make a few more”), following the contributor's request to continue GitHub PR work.

@mahmoud

mahmoud commented Oct 25, 2026

Copy link
Copy Markdown
Owner

Folded into #609 (see the PR description for credit). Thanks for the report and the repro.

@mahmoud mahmoud closed this Oct 25, 2026
blebo pushed a commit to blebo/boltons that referenced this pull request Nov 1, 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.

2 participants