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 EnsureHashAlgorithm #492

Merged
merged 8 commits into from
Oct 21, 2023
Merged

Add EnsureHashAlgorithm #492

merged 8 commits into from
Oct 21, 2023

Conversation

adswa
Copy link
Member

@adswa adswa commented Oct 20, 2023

This PR revives #357, which I couldn't push into directly. I didn't add much to it, I committed a patch @mih proposed a while ago already on top of it, and I added a basic set of tests for the constraint.

mslw and others added 4 commits May 15, 2023 11:58
This adds EnsureHashAlgorithm constraint which checks whether the
given algorith name is contained in hashlib's algorithms_guaranteed
set. It derives from EnsureChoice without further customization.

Using `algorithms_guaranteed` covers algorithms that have named
constructors in hashlib, i.e. accessible through getattr(hashlib,
name) - that's the current usage in MultiHash. Future expansion could
see us move to algorithms_available - these will be recognized when
passed to (slower) hashlib.new(), but have no named constructors.

For details, see: https://docs.python.org/3.11/library/hashlib.html
The EnsureHash algorithm now calls its super.__init__ correctly.

We use EnsureHashAlgorithm() | EnsureListOf(...) in ls_file_colelction
now. This correctly handles incoming values, which are always a list
in CLI (argparse behaviour with --append) and can be str or list in
Python API. At least when they are OK.

When input is not OK, two things are suboptimal:
- an incorrect hash (str) proceeds to EnsureListOf, which tries to go
  letter by letter, failing on first
- a hash (list) with an incorrect value would first fail on
  EnsureHashAlgorithm(list), proceed to EnsureListOf, and fail on
  EnsureHashAlgorithm(str)
As a conequence, in both cases the error message would say "does not
match any of 2 alternatives" and print EnsureChoice message ("is not
one of ...") twice.
@adswa adswa requested a review from mih as a code owner October 20, 2023 09:41
@codecov
Copy link

codecov bot commented Oct 20, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (f0b0fc7) 92.33% compared to head (666977d) 92.39%.
Report is 3 commits behind head on main.

❗ Current head 666977d differs from pull request most recent head 4e04ec8. Consider uploading reports for the commit 4e04ec8 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #492      +/-   ##
==========================================
+ Coverage   92.33%   92.39%   +0.05%     
==========================================
  Files         125      125              
  Lines        9530     9549      +19     
  Branches     1025     1028       +3     
==========================================
+ Hits         8800     8823      +23     
+ Misses        708      704       -4     
  Partials       22       22              
Files Coverage Δ
datalad_next/commands/ls_file_collection.py 100.00% <100.00%> (ø)
datalad_next/constraints/__init__.py 100.00% <ø> (ø)
datalad_next/constraints/basic.py 100.00% <100.00%> (ø)
datalad_next/constraints/compound.py 100.00% <100.00%> (ø)
datalad_next/constraints/tests/test_basic.py 100.00% <100.00%> (ø)

... and 2 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@mih mih added the semver-minor PR implies minor-level version increment (API addition) label Oct 21, 2023
@mih mih added this to the 1.1 milestone Oct 21, 2023
Copy link
Member

@mih mih left a comment

Choose a reason for hiding this comment

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

Thanks for reviving this feature! I added a changelog, minimal documentation, and a missing test to reach 100% coverage. It tested it, and it LGTM!

@mih mih enabled auto-merge October 21, 2023 13:10
@mih mih merged commit 6a2d65e into datalad:main Oct 21, 2023
6 checks passed
@mih mih mentioned this pull request Oct 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver-minor PR implies minor-level version increment (API addition)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants