Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FixedTuple member enforcing a given number of items #211

Merged
merged 18 commits into from
Mar 25, 2024

Conversation

MatthieuDartiailh
Copy link
Member

No description provided.

Copy link

codecov bot commented Mar 22, 2024

Codecov Report

Merging #211 (fb808da) into main (330b8fc) will increase coverage by 0.09%.
The diff coverage is 100.00%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #211      +/-   ##
==========================================
+ Coverage   97.57%   97.67%   +0.09%     
==========================================
  Files          24       24              
  Lines        1114     1075      -39     
  Branches      189      174      -15     
==========================================
- Hits         1087     1050      -37     
+ Misses         13       12       -1     
+ Partials       14       13       -1     

@MatthieuDartiailh MatthieuDartiailh marked this pull request as ready for review March 22, 2024 11:07
@MatthieuDartiailh MatthieuDartiailh changed the title [WIP] Add FixedTuple member enforcing a given number of items Add FixedTuple member enforcing a given number of items Mar 22, 2024
@MatthieuDartiailh
Copy link
Member Author

@frmdstryr do you have an interest in this feature and do you want to review ?


// Create a copy in which to store the validated values
Py_ssize_t size = PyTuple_GET_SIZE( newvalue );
cppy::ptr tuplecopy = PyTuple_New( size );
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be moved to after the size check

@frmdstryr
Copy link
Contributor

This is a really nice addition! A quick test and it seems to work fine.

Instead of adding a new member, what do you think about changing the existing Tuple to have a variable_length parameter that defaults to True if there is only one or no items? I think the original Tuple member will get practically no use if FixedTuple it is added as is.

It would be really nice if the type error would show the whole format eg tuple[int, bool] instead of %s-tuple, but I realize that's more complicated to do in cpp.

The unrelated formatting changes could probably be a separate commit.

@MatthieuDartiailh
Copy link
Member Author

That's a good point. I will look into it. The error message may indeed be more challenging but I will look into it.

@MatthieuDartiailh
Copy link
Member Author

So the issue with re-using the same member is that it is not backward compatible. Changes to how we interpret annotations makes the system stricter but it bothers me less since it should not break anything that was previously valid.

@frmdstryr
Copy link
Contributor

frmdstryr commented Mar 22, 2024

I am normally against breaking backwards compatibility but in this case I think it is worth it. I'm so used to using the tuple[x, y, z] with typing I actually found an invalid use of the current Tuple that passes what should be the second type as the default value. Maybe others can chime in but do whatever.

Edit: After thinking about it more, i'd say just leave it as a separate member.

@MatthieuDartiailh
Copy link
Member Author

Thanks @frmdstryr I will keep two separate members.

Please note that if you use annotations instead of the actual members tuple[x, y, z] will use FixedTuple which should fix your issue.

@MatthieuDartiailh MatthieuDartiailh merged commit d6a1b11 into main Mar 25, 2024
22 checks passed
@MatthieuDartiailh MatthieuDartiailh deleted the fixed-tuple branch March 25, 2024 16:31
@MatthieuDartiailh
Copy link
Member Author

I forgot to address your comments (forgot to re-read the whole discussion). I will come back to it once I am done with #210

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants