|
| 1 | +# This file is dual licensed under the terms of the Apache License, Version |
| 2 | +# 2.0, and the BSD License. See the LICENSE file in the root of this repository |
| 3 | +# for complete details. |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +import dataclasses |
| 8 | +import sys |
| 9 | +import typing |
| 10 | + |
| 11 | +if sys.version_info < (3, 11): |
| 12 | + import typing_extensions |
| 13 | + |
| 14 | + # We use the `include_extras` parameter of `get_type_hints`, which was |
| 15 | + # added in Python 3.9. This can be replaced by the `typing` version |
| 16 | + # once the min version is >= 3.9 |
| 17 | + if sys.version_info < (3, 9): |
| 18 | + get_type_hints = typing_extensions.get_type_hints |
| 19 | + else: |
| 20 | + get_type_hints = typing.get_type_hints |
| 21 | +else: |
| 22 | + get_type_hints = typing.get_type_hints |
| 23 | + |
| 24 | +from cryptography.hazmat.bindings._rust import declarative_asn1 |
| 25 | + |
| 26 | +T = typing.TypeVar("T", covariant=True) |
| 27 | +U = typing.TypeVar("U") |
| 28 | + |
| 29 | + |
| 30 | +encode_der = declarative_asn1.encode_der |
| 31 | + |
| 32 | + |
| 33 | +def _normalize_field_type( |
| 34 | + field_type: typing.Any, field_name: str |
| 35 | +) -> declarative_asn1.AnnotatedType: |
| 36 | + annotation = declarative_asn1.Annotation() |
| 37 | + |
| 38 | + if hasattr(field_type, "__asn1_root__"): |
| 39 | + annotated_root = field_type.__asn1_root__ |
| 40 | + if not isinstance(annotated_root, declarative_asn1.AnnotatedType): |
| 41 | + raise TypeError(f"unsupported root type: {annotated_root}") |
| 42 | + return annotated_root |
| 43 | + else: |
| 44 | + rust_field_type = declarative_asn1.non_root_python_to_rust(field_type) |
| 45 | + |
| 46 | + return declarative_asn1.AnnotatedType(rust_field_type, annotation) |
| 47 | + |
| 48 | + |
| 49 | +def _annotate_fields( |
| 50 | + raw_fields: dict[str, type], |
| 51 | +) -> dict[str, declarative_asn1.AnnotatedType]: |
| 52 | + fields = {} |
| 53 | + for field_name, field_type in raw_fields.items(): |
| 54 | + # Recursively normalize the field type into something that the |
| 55 | + # Rust code can understand. |
| 56 | + annotated_field_type = _normalize_field_type(field_type, field_name) |
| 57 | + fields[field_name] = annotated_field_type |
| 58 | + |
| 59 | + return fields |
| 60 | + |
| 61 | + |
| 62 | +def _register_asn1_sequence(cls: type[U]) -> None: |
| 63 | + raw_fields = get_type_hints(cls, include_extras=True) |
| 64 | + root = declarative_asn1.AnnotatedType( |
| 65 | + declarative_asn1.Type.Sequence(cls, _annotate_fields(raw_fields)), |
| 66 | + declarative_asn1.Annotation(), |
| 67 | + ) |
| 68 | + |
| 69 | + setattr(cls, "__asn1_root__", root) |
| 70 | + |
| 71 | + |
| 72 | +# Due to https://github.com/python/mypy/issues/19731, we can't define an alias |
| 73 | +# for `dataclass_transform` that conditionally points to `typing` or |
| 74 | +# `typing_extensions` depending on the Python version (like we do for |
| 75 | +# `get_type_hints`). |
| 76 | +# We work around it by making the whole decorated class conditional on the |
| 77 | +# Python version. |
| 78 | +if sys.version_info < (3, 11): |
| 79 | + |
| 80 | + @typing_extensions.dataclass_transform(kw_only_default=True) |
| 81 | + def sequence(cls: type[U]) -> type[U]: |
| 82 | + # We use `dataclasses.dataclass` to add an __init__ method |
| 83 | + # to the class with keyword-only parameters. |
| 84 | + if sys.version_info >= (3, 10): |
| 85 | + dataclass_cls = dataclasses.dataclass( |
| 86 | + repr=False, |
| 87 | + eq=False, |
| 88 | + # `match_args` was added in Python 3.10 and defaults |
| 89 | + # to True |
| 90 | + match_args=False, |
| 91 | + # `kw_only` was added in Python 3.10 and defaults to |
| 92 | + # False |
| 93 | + kw_only=True, |
| 94 | + )(cls) |
| 95 | + else: |
| 96 | + dataclass_cls = dataclasses.dataclass( |
| 97 | + repr=False, |
| 98 | + eq=False, |
| 99 | + )(cls) |
| 100 | + _register_asn1_sequence(dataclass_cls) |
| 101 | + return dataclass_cls |
| 102 | + |
| 103 | +else: |
| 104 | + |
| 105 | + @typing.dataclass_transform(kw_only_default=True) |
| 106 | + def sequence(cls: type[U]) -> type[U]: |
| 107 | + # Only add an __init__ method, with keyword-only |
| 108 | + # parameters. |
| 109 | + dataclass_cls = dataclasses.dataclass( |
| 110 | + repr=False, |
| 111 | + eq=False, |
| 112 | + match_args=False, |
| 113 | + kw_only=True, |
| 114 | + )(cls) |
| 115 | + _register_asn1_sequence(dataclass_cls) |
| 116 | + return dataclass_cls |
0 commit comments