Replace frozendict (LGPL-3.0) with a stdlib hashable dict in lru_cache#35
Merged
Conversation
Fixes the LGPL-3.0 license propagation reported in issue fferflo#34. frozendict was used solely to make dict args hashable as lru_cache keys. A tuple of (key, frozen_value) pairs is equally hashable and correct, requires no third-party dep, and preserves cache-hit semantics since Python 3.7+ guarantees dict insertion order.
The previous tuple replacement broke downstream code that reads frozen kwargs back via key access (e.g. kwargs["backend"] in _construct_graph), causing 'TypeError: tuple indices must be integers or slices, not str'. Use a minimal stdlib dict subclass with an order-independent __hash__ so the frozen value stays a real mapping while remaining hashable for functools.lru_cache. Still no frozendict dependency. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Owner
|
Looks good, thanks for the contribution! |
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.
Closes #34.
What
frozendictfromdependenciesinpyproject.tomlimport frozendictfromeinx/_src/util/lru_cache.pyfrozendict.frozendict(...)branch in_freeze_valuewith a tiny stdlibdictsubclass (_FrozenDict) that is hashableWhy this works
_freeze_valueproduces hashable keys forfunctools.lru_cache. The frozen value must stay a real mapping, because downstream code reads it back via key access (e.g.kwargs["backend"]in_construct_graph) — so a plain tuple is not enough._FrozenDictsubclassesdict, so it keeps full mapping behaviour, and defines an order-independent__hash__(hash(frozenset(self.items()))) so it can be used as anlru_cachekey. Since equal dicts hash equally and compare equal, cache-hit behaviour is preserved.Why this matters
frozendictis released under LGPL-3.0.einxis MIT-licensed and widely used as a transitive dependency (e.g. viax-transformers), so thefrozendictdep silently propagates LGPL-3.0 into downstream projects that otherwise use only permissive licenses. This change removes that last LGPL-3.0 dep with a stdlib-only replacement.Notes
tuple. That broke the test suite (TypeError: tuple indices must be integers or slices, not str) because the frozen value is read back as a mapping downstream. The_FrozenDictapproach fixes this while keeping the no-frozendictgoal.