Add Dimensions.__contains__#69
Conversation
| else: | ||
| raise TypeError( | ||
| f"Key must be a string or list of strings but got type {type(key)}" | ||
| ) |
There was a problem hiding this comment.
I dont think we need the error handling since the function is typed.
maybe something like
def __contains__(self, key: Union[str, Sequence[str]]) -> bool:
if isinstance(key, str):
return key in self._dims_shape
return all(k in self._dims_shape for k in key)
There was a problem hiding this comment.
That's fine with me -- I mimicked the same input type handling in __getitem__, and in this case it would just be a difference in the friendliness of the TypeError message. Without that error checking, one would see something like TypeError: 'int' object is not iterable if checking whether 3 in img.dims.
Despite the type hint, I would probably just expect object_of_incompatible_type in img.dims to return False, but I don't have a big stake in this.
There was a problem hiding this comment.
If it is possible to supply the wrong type then this seems fine with me - python specific function like this tend to be lenient
|
looks like linting is failing for these changes, maybe some config differences between your local setup and the repo defined setup? |
|
Thanks @BrianWhitneyAI; I missed a return type declaration and fixed an actual typo in my original commit. I have not yet made any changes in reply to #69 (comment). |
This allows straightforward checking of whether dimension `S` is present, since the interface of this class previously made this a bit clumsy.
497c4e1 to
964ef05
Compare
|
Squashed that follow-up commit as a |
4b23f36 to
964ef05
Compare
|
@BrianWhitneyAI @SeanDuHare I was just about to say that I pushed another commit implementing the following behavior:
with no While typing this, I saw the PR approval -- thanks! |
This allows straightforward checking of whether dimension
Sis present, since the interface of this class previously made this a bit clumsy.Link to Relevant Issue
This pull request resolves #68.
Description of Changes
Implement
Dimensions.__contains__, with similar semantics as__getitem__: accept astrorSequence[str]and returnTrueif all dimensions are present.The
isort,autoflake, andblacklint steps passed, butflake8andmypyfail both on currentmainand with my changes.