Implement __hash__ for ASN.1 wrapper types#14963
Merged
Merged
Conversation
Adds __hash__ to PrintableString, IA5String, UTCTime, GeneralizedTime, and BitString, delegating to the hash of the wrapped Python value (and for BitString, the (data, padding_bits) tuple) so that equal values hash equally. https://claude.ai/code/session_01XYJLrH5WSNkJbgNd19jPtf
reaperhulk
approved these changes
Jun 7, 2026
reaperhulk
pushed a commit
that referenced
this pull request
Jun 7, 2026
* Resolve value set decode performance TODO with a value -> member map Replaces the linear scan over enum members in value set decoding with an O(1) lookup in a value -> member map, built once when the class is registered and stored in Type::ValueSet. This is now possible because the ASN.1 wrapper types implement __hash__ (#14963). Member values that implement __eq__ but not __hash__ (e.g. Null) can't be used as dict keys; for those the map is None and decoding falls back to the previous linear scan, preserving behavior. https://claude.ai/code/session_01FrUdGjs6fKGSQ6E6WQPGB1 * Remove the linear scan fallback for unhashable value set members Per review: value set member values must be hashable. The value -> member map is now built unconditionally at registration time, so an enum with unhashable member values (e.g. Null) raises TypeError when decorated, and decoding is always an O(1) map lookup. https://claude.ai/code/session_01FrUdGjs6fKGSQ6E6WQPGB1 * Address review feedback - Use a {} literal for the value map in test_fields_of_variant_type (the annotation stays: mypy can't infer the type of an empty literal) - Drop test_fail_unhashable_member_values https://claude.ai/code/session_01FrUdGjs6fKGSQ6E6WQPGB1 --------- Co-authored-by: Claude <noreply@anthropic.com>
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.
Adds
__hash__to the declarative ASN.1 wrapper types:PrintableString,IA5String,UTCTime,GeneralizedTime, andBitString.Each implementation delegates to the hash of the wrapped Python value (for
BitString, the(data, padding_bits)tuple), so objects that compare equal via__eq__hash equally.Also updates the
declarative_asn1.pyistubs and adds tests covering equal/unequal hashes for each type.https://claude.ai/code/session_01XYJLrH5WSNkJbgNd19jPtf
Generated by Claude Code