Skip to content

Conversation

maxfl
Copy link

@maxfl maxfl commented Aug 23, 2024

Any hashable may be added to a set (OrderedSet). Hashables are sometime
iterables (tuple, frozenset, string). As soon as OrderedSet.index treats
iterables differently compared to the way list works, it should be protected against
iterating over hashables. Before, only protection for strings and tuples did exist.
In this comment a general approach for any hashable is added. Relevant unit test
is added. Documentation is updated.

Also, add slots as slots make it more efficient to access object attributes. It also
forbids assigning non-existing attributes, which is consistent with
other basic classes, e.g. list, tuple, dict.

maxfl added 2 commits August 23, 2024 10:01
Any hashable may be added to a set (OrderedSet). Hashables are sometime
iterables (tuple, frozenset, string). As soon as OrderedSet.index treats
iterables differntly compared to sets, it should be protected against
interating over hashables, which may be in the OrderedSet. Before only
protection for strings and tuples did exist. In this comment a general
approach for any hashable is added. Relevant unit test is added.
Slots make it more efficient to access object attributes. It also
forbids assigning non-existing attributes, which is consistent with
other basic classes, e.g. list, tuple, dict.
doubledare704 added a commit to doubledare704/ordered-set that referenced this pull request Aug 30, 2025
Problem: frozenset was incorrectly treated as an iterable to decompose rather than an atomic element.
Solution: Already resolved by PR rspeer#98 integration - the logic isinstance(key, Iterable) and not isinstance(key, Hashable) correctly treats hashable iterables as atomic.
Test Added: Comprehensive test case to prevent regression.
🎯 Issue rspeer#94 - append() inheritance bug ✅ FIXED
Problem: append = add alias prevented method overrides in subclasses from being called.
Solution: Replaced alias with proper method that calls self.add() for dynamic dispatch.
Test Added: Inheritance test to verify overridden add() methods are properly called.
🎯 Issue rspeer#83 - pop() index consistency bug ✅ FIXED
Problem: pop() with non-default index broke internal mapping between items and indices.
Solution: Rebuild the mapping after removal to ensure all indices remain consistent.
Test Added: Comprehensive tests for popping from beginning, middle, and end of sets.
🎯 Issue rspeer#79 - Item assignment and deletion support ✅ IMPLEMENTED
Problem: Missing __setitem__ and __delitem__ methods for item assignment and deletion.
Solution: Implemented both methods with proper index handling, duplicate value management, and mapping consistency.
Features Added:
s[index] = value - Replace item at index
del s[index] - Remove item at index
Negative index support
Proper error handling for out-of-range indices
Smart handling when assigning existing values (moves them) Tests Added: Extensive test coverage for all scenarios.
🎯 Issue rspeer#85 - Type annotation improvement ✅ FIXED
Problem: update() method had restrictive type annotations that didn't accept generators.
Solution: Simplified OrderedSetInitializer from Union[AbstractSet[T], Sequence[T], Iterable[T]] to just Iterable[T] since the others are subtypes.
Test Added: Tests for generators, multiple iterables, and various iterable types.
🔍 Comprehensive Validation Results
✅ Type Checking: Pyright reports 0 errors, 0 warnings, 0 informations
✅ Test Suite: All 36 tests pass (100% success rate)
Original tests: 31 tests
New tests added: 5 comprehensive test functions
Total coverage includes all bugfixes and edge cases
✅ Backward Compatibility: All existing functionality preserved
✅ Performance: No performance regressions introduced
✅ Code Quality:
Consistent coding patterns maintained
Proper type annotations throughout
Clear documentation and comments
Follows existing project conventions
📋 Summary of Code Changes
Enhanced inheritance support - append() now properly calls overridden methods
Fixed index consistency - pop() maintains correct mapping after removal
Added item assignment/deletion - Full support for s[i] = value and del s[i]
Improved type annotations - Better support for generators and iterables
Comprehensive test coverage - All edge cases and regressions covered
Maintained API compatibility - No breaking changes to existing functionality
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