-
Notifications
You must be signed in to change notification settings - Fork 11
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
#346
Comments
|
TIL from hashlib docs:
So with the way We could also extend >>> import hashlib
>>> hashlib.algorithms_guaranteed
{'shake_256', 'sha224', 'sha3_512', 'md5', 'sha3_256', 'sha3_224', 'shake_128', 'sha3_384', 'sha512', 'sha384', 'blake2s', 'blake2b', 'sha1', 'sha256'}
>>> hashlib.algorithms_available - hashlib.algorithms_guaranteed # set difference
{'sm3', 'md4', 'ripemd160', 'sha512_256', 'whirlpool', 'md5-sha1', 'sha512_224'} And example usage: >>> hasattr(hashlib, "ripemd160")
False
>>> h = hashlib.new("ripemd160") # must go through new
>>> h.update(b"Nobody inspects the spammish repetition")
>>> h.hexdigest()
'cc4a5ce1b3df48aec5d22d1f16b894a0b894eccc' I will probably use |
Trying to build
I just wanted to confirm whether this is an intended behavior, and ask about the correct way to implement the constraint - should it handle a list, or handle a single item, but be somehow wrapped inside |
I was doubly wrong in my comment above:
I think I've got it now - parameter constraint should expect a string, but the command should get |
Maybe an Sorry for spamming the issue, I am working on it in small chunks and keeping notes here. |
The approach is good. That the messaging is suboptimal exposes two issues:
The following diff sketches a solution to both issues. I have not checked what would break when applied in general, but in your PR this changes the error message to
diff --git a/datalad_next/constraints/basic.py b/datalad_next/constraints/basic.py
index e60cbae..4a47a11 100644
--- a/datalad_next/constraints/basic.py
+++ b/datalad_next/constraints/basic.py
@@ -275,6 +275,9 @@ class EnsureChoice(Constraint):
def short_description(self):
return '{%s}' % ', '.join([repr(c) for c in self._allowed])
+ def __str__(self):
+ return f"one of {self.short_description()}"
+
class EnsureKeyChoice(EnsureChoice):
"""Ensure value under a key in an input is in a set of possible values"""
diff --git a/datalad_next/constraints/compound.py b/datalad_next/constraints/compound.py
index 99fe8d6..bb0d87f 100644
--- a/datalad_next/constraints/compound.py
+++ b/datalad_next/constraints/compound.py
@@ -77,10 +77,12 @@ class EnsureIterableOf(Constraint):
iter = self._iter_type(
self._item_constraint(i) for i in value
)
- except TypeError as e:
+ except (ConstraintError, TypeError) as e:
self.raise_for(
value,
- "cannot coerce to target (item) type",
+ "{itertype} item is not {itype}",
+ itertype=self._iter_type.__name__,
+ itype=self._item_constraint,
__caused_by__=e,
)
if self._min_len is not None or self._max_len is not None: |
This was added in #492 |
There is detection logic in
MultiHash
that could/should become exposed in this more standard fashion. For commands that support it (download
,ls-file-collection
) it would also make sense to implementEnsureMultiHash
The text was updated successfully, but these errors were encountered: