Skip to content

Conversation

@facutuesca
Copy link
Contributor

This PR adds support for SIZE annotations to the ASN.1 API, starting with fields of type SEQUENCE OF.

@asn1.sequence
class Example
  a: Annotated[typing.List[int], asn1.Size(min=1, max=5)]  # SIZE(1..5)

@asn1.sequence
class Example
  a: Annotated[typing.List[int], asn1.Size(min=1, max=None)]  # SIZE(1..MAX)


@asn1.sequence
class Example
  a: Annotated[typing.List[int], asn1.Size.exact(6)]  # SIZE(6)

Part of #12283

Copy link
Member

@alex alex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should block size annotations with types we don't do anything with them

min: int
max: int | None

def __new__(cls, min: int, max: int | None) -> Size: ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess if you want no min you just do min=0?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup

let list_len = list.len();
let min = size.get().min;
let max = size.get().max.unwrap_or(usize::MAX);
if list_len < min || list_len > max {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is like, 2% easier to read as if !(min..=max).contains(&list_len) since thats it unavoidable that it's an inclusive range

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

if let Some(size) = &annotated_type.annotation.get().size {
let min = size.get().min;
let max = size.get().max.unwrap_or(usize::MAX);
if values.len() < min || values.len() > max {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Signed-off-by: Facundo Tuesca <[email protected]>
@facutuesca
Copy link
Contributor Author

we should block size annotations with types we don't do anything with them

done!

@alex alex enabled auto-merge (squash) November 20, 2025 23:57
@alex alex merged commit 626db88 into pyca:main Nov 21, 2025
68 checks passed
@facutuesca facutuesca deleted the ft/asn1-size branch November 21, 2025 00:06
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.

2 participants