@@ -164,7 +164,10 @@ def _normalize_field_type(
164164
165165 if hasattr (field_type , "__asn1_root__" ):
166166 root_type = field_type .__asn1_root__
167- if not isinstance (root_type , declarative_asn1 .Type .Sequence ):
167+ if not isinstance (
168+ root_type ,
169+ (declarative_asn1 .Type .Sequence , declarative_asn1 .Type .Set ),
170+ ):
168171 raise TypeError (f"unsupported root type: { root_type } " )
169172 return declarative_asn1 .AnnotatedType (
170173 typing .cast (declarative_asn1 .Type , root_type ), annotation
@@ -325,6 +328,13 @@ def _register_asn1_sequence(cls: type[U]) -> None:
325328 setattr (cls , "__asn1_root__" , root )
326329
327330
331+ def _register_asn1_set (cls : type [U ]) -> None :
332+ raw_fields = get_type_hints (cls , include_extras = True )
333+ root = declarative_asn1 .Type .Set (cls , _annotate_fields (raw_fields ))
334+
335+ setattr (cls , "__asn1_root__" , root )
336+
337+
328338# Due to https://github.com/python/mypy/issues/19731, we can't define an alias
329339# for `dataclass_transform` that conditionally points to `typing` or
330340# `typing_extensions` depending on the Python version (like we do for
@@ -356,6 +366,29 @@ def sequence(cls: type[U]) -> type[U]:
356366 _register_asn1_sequence (dataclass_cls )
357367 return dataclass_cls
358368
369+ @typing_extensions .dataclass_transform (kw_only_default = True )
370+ def set (cls : type [U ]) -> type [U ]:
371+ # We use `dataclasses.dataclass` to add an __init__ method
372+ # to the class with keyword-only parameters.
373+ if sys .version_info >= (3 , 10 ):
374+ dataclass_cls = dataclasses .dataclass (
375+ repr = False ,
376+ eq = False ,
377+ # `match_args` was added in Python 3.10 and defaults
378+ # to True
379+ match_args = False ,
380+ # `kw_only` was added in Python 3.10 and defaults to
381+ # False
382+ kw_only = True ,
383+ )(cls )
384+ else :
385+ dataclass_cls = dataclasses .dataclass (
386+ repr = False ,
387+ eq = False ,
388+ )(cls )
389+ _register_asn1_set (dataclass_cls )
390+ return dataclass_cls
391+
359392else :
360393
361394 @typing .dataclass_transform (kw_only_default = True )
@@ -371,6 +404,19 @@ def sequence(cls: type[U]) -> type[U]:
371404 _register_asn1_sequence (dataclass_cls )
372405 return dataclass_cls
373406
407+ @typing .dataclass_transform (kw_only_default = True )
408+ def set (cls : type [U ]) -> type [U ]:
409+ # Only add an __init__ method, with keyword-only
410+ # parameters.
411+ dataclass_cls = dataclasses .dataclass (
412+ repr = False ,
413+ eq = False ,
414+ match_args = False ,
415+ kw_only = True ,
416+ )(cls )
417+ _register_asn1_set (dataclass_cls )
418+ return dataclass_cls
419+
374420
375421# TODO: replace with `Default[U]` once the min Python version is >= 3.12
376422@dataclasses .dataclass (frozen = True )
0 commit comments