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 particlewise
keyword to particle_collections.is_category
#2648
Conversation
The ruff error failing is FBT003, from this line There's some relevant discussion in this thread. Any suggestions? |
This caused a lot of other tests to fail because Any thoughts? |
Perhaps we could add a |
In the long term, it would probably be best for Thank you for doing this! |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2648 +/- ##
==========================================
- Coverage 95.22% 95.20% -0.02%
==========================================
Files 103 103
Lines 9401 9414 +13
Branches 2151 2156 +5
==========================================
+ Hits 8952 8963 +11
Misses 274 274
- Partials 175 177 +2 ☔ View full report in Codecov by Sentry. |
Thanks @namurphy! I've I'm not sure that I understand the outstanding issue with the Documentation, though. Any insight into the issue there? |
Sorry for the commits going back and forth. I'm trying to understand the issue with the static type checking. I've changed Edit: still haven't figured this one out. It's raising an error in |
@namurphy, please review when you can. I finally managed to get the issues sorted. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! 🚀 Thank you for doing this! 🙏🏻
I only have a few quick suggestions, and I think we can get this merged in time for our v2024.5.0
release on Tuesday or Wednesday.
Thank you again!
As with an individual |Particle| and |CustomParticle|, we can check whether | ||
all the particles in a list fall within a category using |is_category|. | ||
|
||
>>> helium_ions.is_category("ion") | ||
False | ||
|
||
We may also check each particle in the list individually by setting | ||
the keyword ``particlewise`` to `True`. | ||
|
||
>>> helium_ions.is_category("ion", particlewise=True) | ||
[False, True, True] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏻 Thank you for updating this here! I often forget to update the narrative docs until I realize it about six months later... 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I only caught this while trying to track down why the docs were failing to build
if not all(ions.is_category("ion")): | ||
if not ions.is_category("ion"): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏻 Always nice to be able to shorten/simplify code!
@overload | ||
def is_category( | ||
self, | ||
*category_tuple, | ||
require: str | Iterable[str] | None = None, | ||
any_of: str | Iterable[str] | None = None, | ||
exclude: str | Iterable[str] | None = None, | ||
) -> list[bool]: | ||
particlewise: Literal[True] = ..., | ||
) -> bool: ... | ||
|
||
@overload | ||
def is_category( | ||
self, | ||
*category_tuple, | ||
require: str | Iterable[str] | None = None, | ||
any_of: str | Iterable[str] | None = None, | ||
exclude: str | Iterable[str] | None = None, | ||
particlewise: Literal[False], | ||
) -> list[bool]: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏻 Nice use of @overload
!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, I did this when I was trying to solve my issues with the static typing. It didn't solve that issue, but from what I gather reading comments on StackExchange and Github, it seems this is a clear way to specify the output when it changes based on a bool
input.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is helpful to know!
If you run into any mypy errors that feel unduly strict, please feel free to bring it up in #2589. We're still in the process of figuring out how strict we want the mypy settings to be, since there are tradeoffs between the benefits of type hints compared to the effort needed to implement them. I suspect that we'll probably need to make mypy somewhat less strict.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to use # type: ignore[arg-type]
to resolve the issue here:
if (
not self.allow_custom_particles
and isinstance(particle, ParticleList)
and any(particle.is_category("custom", particlewise=True)) # type: ignore[arg-type]
):
The third condition can only be called if it is indeed a ParticleList
, but the mypy check insisted that there would be an issue with possibly return something that is not iterable, which means that the call to any()
would fail. It might be too strict for this case? I'll mention it in the other thread.
Thank you for addressing the changes! Everything looks ready, so I'll enable auto-merge. |
Thanks for the help! |
Description
Addresses #2595 by adding the requested
particlewise
keyword to the functionparticle_collections.is_category
.This changes the default behavior to return a
bool
rather than alist
ofbool
. If the original functionality is preferred to be the default, the default value ofparticlewise
could be switched toTrue
.I've added tests that should (hopefully) address all the new cases, by using
particlewise
as bothTrue
andFalse
and adding one new test that should evaluate toTrue
.I looked at the docs to see if any changes were needed, but the
is_category
function doesn't appear to have any further description there. Please let me know if I missed something!Motivation and context
See the description by @namurphy in #2595. It was marked as "needs discussion", which I don't know if this has been resolved.
Related issues
#2595