Skip to content

get_size() resolves the leaf key when wrapped with a dol key codec - #3

Merged
thorwhalen merged 2 commits into
mainfrom
fix/get-size-key-resolution
Sep 22, 2026
Merged

thorwhalen merged 2 commits into
mainfrom
fix/get-size-key-resolution

Conversation

@thorwhalen

Copy link
Copy Markdown
Owner

Summary

HfMapping.get_size() forwarded its key argument straight to the module-level get_size(), with no leaf-key resolution. __getitem__/__contains__/etc. correctly see the leaf key when the instance is wrapped with a dol key codec (dunders are delegated to the wrapped store by construction); get_size, a non-dunder method, received the outer, unmapped key instead.

This was latent, not live — hfdol ships no key-transform wrapping itself — but it becomes a real bug the moment a caller wraps one of these Mappings, which is a natural thing to do for a package whose classes are collections.abc.Mappings.

Fix

Adds _leaf_key(self, key), which:

  • lazily imports dol.wrapped_self / dol.inner_most_key (dol stays undeclared/optional — nothing else in hfdol needs it, matching current pyproject.toml);
  • resolves the key through the wrapping chain when one is present;
  • falls back to returning key unchanged on any import/resolution failure — including the unwrapped case, which is the common path and must not regress.

Uses default=None on inner_most_key because i2mint/dol#84 changed unresolvable lookups from returning None to raising AttributeError — this repo doesn't pin a dol version, so the code has to be correct against both behaviors.

Verification

Ran the exact 4-shape repro from #2 (network calls stubbed) against this fix:

  • unwrapped s['music_genre'] (dunder, was already correct)
  • class-wrap (KeyCodecs.prefixed) get_size — now sends the correct leaf key
  • instance-wrap (wrap_kvs) get_size — now sends the correct leaf key
  • plain unwrapped get_size — unaffected (regression check)

All four pass. Also ran pytest --doctest-modules — 1 passed.

Breaking-change check

Checked _tmp/fleet_dependents.json per the AGENT_BRIEF: hfdol has no dependents on this box.

Closes #2

🤖 Generated with Claude Code

https://claude.ai/code/session_011HSBVhDjRU4apSLcRkavv9

thorwhalen and others added 2 commits September 22, 2026 14:28
HfMapping.get_size() forwarded its key argument straight to the
Hugging Face API. __getitem__ and the other dunders correctly see
the leaf key when a dol key codec (wrap_kvs, KeyCodecs.prefixed,
...) wraps the instance -- dol delegates dunders to the wrapped
store -- but get_size, a non-dunder method, received the outer,
unmapped key. Latent today (hfdol has no built-in wrapping), but a
correctness trap the moment a caller wraps one of these Mappings,
which for a package of dict-like classes is a natural thing to do.

Adds `_leaf_key`, which resolves through dol.inner_most_key /
wrapped_self when dol is installed and the instance is wrapped, and
is a no-op otherwise (import is lazy; dol stays an optional,
undeclared dependency, matching current pyproject.toml). Verified
against all four shapes from the issue's own repro: unwrapped,
class-wrap (KeyCodecs.prefixed), instance-wrap (wrap_kvs), and the
plain unwrapped case (regression check) -- all route the correct
repo id to the (stubbed) Hugging Face API.

Closes #2

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Covers the 3 scenarios from #2's own repro (class-wrap via
KeyCodecs.prefixed, instance-wrap via wrap_kvs, and the unwrapped
case as a regression guard), with the HF API call stubbed so it
runs offline. Adds dol to the `testing` extra since the test needs
it (dol itself stays a lazy, undeclared runtime dependency of the
package -- see _leaf_key's docstring).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@thorwhalen
thorwhalen merged commit d634c6b into main Sep 22, 2026
12 checks passed
@thorwhalen
thorwhalen deleted the fix/get-size-key-resolution branch September 22, 2026 14:34
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.

get_size() receives the outer, unmapped key when an HfMapping is wrapped with a dol key codec (latent)

1 participant