Skip to content

Add Dimensions.__contains__#69

Open
mruffalo wants to merge 2 commits into
bioio-devs:mainfrom
hubmapconsortium:mruffalo/dimension-membership-testing
Open

Add Dimensions.__contains__#69
mruffalo wants to merge 2 commits into
bioio-devs:mainfrom
hubmapconsortium:mruffalo/dimension-membership-testing

Conversation

@mruffalo
Copy link
Copy Markdown

This allows straightforward checking of whether dimension S is 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 a str or Sequence[str] and return True if all dimensions are present.

The isort, autoflake, and black lint steps passed, but flake8 and mypy fail both on current main and with my changes.

Comment thread bioio_base/dimensions.py Outdated
else:
raise TypeError(
f"Key must be a string or list of strings but got type {type(key)}"
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If it is possible to supply the wrong type then this seems fine with me - python specific function like this tend to be lenient

@BrianWhitneyAI
Copy link
Copy Markdown
Contributor

looks like linting is failing for these changes, maybe some config differences between your local setup and the repo defined setup?

- hook id: mypy
- exit code: 1

bioio_base/dimensions.py:160: error: Function is missing a return type annotation  [no-untyped-def]
bioio_base/dimensions.py:164: error: "type[Sequence[Any]]" has no attribute "__iter__" (not iterable)  [attr-defined]
Found 2 errors in 1 file (checked 22 source files)

@mruffalo
Copy link
Copy Markdown
Author

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.
@mruffalo mruffalo force-pushed the mruffalo/dimension-membership-testing branch from 497c4e1 to 964ef05 Compare May 28, 2026 03:50
@mruffalo
Copy link
Copy Markdown
Author

Squashed that follow-up commit as a fixup to avoid having something obviously broken in the commit history.

@mruffalo mruffalo force-pushed the mruffalo/dimension-membership-testing branch from 4b23f36 to 964ef05 Compare May 28, 2026 15:56
@mruffalo
Copy link
Copy Markdown
Author

@BrianWhitneyAI @SeanDuHare I was just about to say that I pushed another commit implementing the following behavior:

  • if checking membership of a sequence (but not a str), return True if all elements are present
  • otherwise return membership of the passed item

with no TypeErrors thrown.

While typing this, I saw the PR approval -- thanks!

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.

Inconvenient to check if a dimension (like S) is present

4 participants