Skip to content

Implement __hash__ for ASN.1 wrapper types#14963

Merged
reaperhulk merged 2 commits into
mainfrom
claude/laughing-lamport-Uef8G
Jun 7, 2026
Merged

Implement __hash__ for ASN.1 wrapper types#14963
reaperhulk merged 2 commits into
mainfrom
claude/laughing-lamport-Uef8G

Conversation

@alex
Copy link
Copy Markdown
Member

@alex alex commented Jun 7, 2026

Adds __hash__ to the declarative ASN.1 wrapper types: PrintableString, IA5String, UTCTime, GeneralizedTime, and BitString.

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.pyi stubs and adds tests covering equal/unequal hashes for each type.

https://claude.ai/code/session_01XYJLrH5WSNkJbgNd19jPtf


Generated by Claude Code

claude added 2 commits June 7, 2026 00:53
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 reaperhulk merged commit 2dc9281 into main Jun 7, 2026
64 checks passed
@reaperhulk reaperhulk deleted the claude/laughing-lamport-Uef8G branch June 7, 2026 01:04
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants