Add asn1.value_set for enum-based ASN.1 value sets#14962
Merged
Conversation
Adds a new @asn1.value_set class decorator that registers an enum.Enum subclass whose member values all share a single ASN.1 type. Members are encoded as their underlying value, and decoding maps the value back to the corresponding member, failing if it does not match any member. fixes #14891 https://claude.ai/code/session_01AbKLBGrHu13j6gKNpKGD2e
The decorator is now used as @value_set(ObjectIdentifier), making the governing type explicit (matching ASN.1 value set syntax) and allowing member values to be validated against the declared type. https://claude.ai/code/session_01AbKLBGrHu13j6gKNpKGD2e
The combined-coverage CI gate flagged the ValueSet variant declaration: the pyo3-generated _0/_1 accessors were never exercised. Extend test_fields_of_variant_type to cover them, like the other variants. https://claude.ai/code/session_01AbKLBGrHu13j6gKNpKGD2e
alex
commented
Jun 7, 2026
| """ | ||
| try: | ||
| rust_type = declarative_asn1.non_root_python_to_rust(value_type) | ||
| except TypeError: |
Member
Author
There was a problem hiding this comment.
I don't think we need to re-raise here, the original exception is fine
| f"value set '{cls.__name__}' must have at least one member" | ||
| ) | ||
| for member in members: | ||
| if type(member.value) is not value_type: |
Member
Author
There was a problem hiding this comment.
Should this be an isinstance? I guess theoretically subclasses are fine?
- Don't wrap the non_root_python_to_rust exception in value_set - Allow subclasses of the declared value type (isinstance check) - Document why value set decoding uses a linear member scan https://claude.ai/code/session_01AbKLBGrHu13j6gKNpKGD2e
reaperhulk
approved these changes
Jun 7, 2026
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 a new
@asn1.value_set(value_type)class decorator that registers anenum.Enumsubclass as an ASN.1 value set: a named set of values of a single governing type (X.680 §16.7).Members are encoded exactly as their underlying value; decoding maps the value back to the corresponding enum member and fails with
ValueErrorif it doesn't match any member. All member values are validated against the declaredvalue_typeat class-definition time.Works with any supported ASN.1 value type (OIDs, ints, strings, ...) and composes with
Implicit/Explicit/Defaultannotations,OPTIONAL(X | None),CHOICEunions, and top-levelencode_der/decode_der.Implementation notes:
Type::ValueSet(class, inner)variant on the Rust side; the field's encoding annotation is propagated to the underlying value, whileDEFAULTis handled at the member level.PrintableStringthat define__eq__without__hash__work as values.fixes #14891
https://claude.ai/code/session_01AbKLBGrHu13j6gKNpKGD2e
Generated by Claude Code