Skip to content

Add is_binned property - #3775

Merged
jl-wynen merged 6 commits into
mainfrom
is_binned
Oct 17, 2025
Merged

jl-wynen merged 6 commits into
mainfrom
is_binned

Conversation

@jl-wynen

@jl-wynen jl-wynen commented Oct 15, 2025 •

Copy link
Copy Markdown
Member

First step towards #3687

Unfortunately, this leads to a bunch of type errors because Mypy does not understand the relationship between .is_binned and .bins. But once we make bins raise, we can just revert the 'Suppress temporary type error' commit.

Comment thread src/scipp/coords/transform_coords.py Outdated
if da.bins is not None:
da.bins.coords.pop(name, None)
if da.is_binned:
da.bins.coords.pop(name, None) # type: ignore[union-attr]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How about changing the type hint of bins right now, even if it may still return None. Then you can avoid suppressing everything (and having to remove it again later)?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Could do. No downstream package actually passes type checks at the moment anyway.

Comment thread src/scipp/core/bins.py Outdated
Comment on lines 598 to 601
"""
Returns helper :py:class:`scipp.Bins` allowing bin-wise operations
to be performed or `None` if not binned data.
"""

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do you want to add a deprecation note for the None return behavior in the docstring?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I was thinking of releasing this change, then adding a deprecation warning in the release after. Should I still add something in the docstring now?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think you can add a deprecation warning anyway, right? So why not put it into the docstring now?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Why not? We can raise a warning when .bins is called on an unbinned object.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

But won't that give tons of false positives from uses such as

if da.bins is None:
  # do a
else:
  # do b

?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Or maybe the order of releases, refactoring, and deprecation we had in mind was mismatching, so it may just be down to confusion!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

My idea was just to insert an extra step by raising a warning and later change that to an error. This would allow people to still use the old approach before it gets properly disabled. But it would trip up our tests and force us to migrate.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ok, makes sense!

Comment thread src/scipp/core/bins.py Outdated
def _bins(obj: _O) -> Bins[_O]:
"""Returns helper :class:`scipp.Bins` for bin-wise operations.

.. attention::

@SimonHeybrock SimonHeybrock Oct 16, 2025 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Didn't Sphinx have a specific tag for deprecation?

@jl-wynen
jl-wynen enabled auto-merge October 17, 2025 06:29
@jl-wynen
jl-wynen merged commit aea93a1 into main Oct 17, 2025
4 checks passed
@jl-wynen
jl-wynen deleted the is_binned branch October 17, 2025 06:38
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