Skip to content

Extend @particle_input to ParticleListLike annotations#2790

Merged
namurphy merged 12 commits intoPlasmaPy:mainfrom
JaydenR2305:particle-list-input
May 28, 2025
Merged

Extend @particle_input to ParticleListLike annotations#2790
namurphy merged 12 commits intoPlasmaPy:mainfrom
JaydenR2305:particle-list-input

Conversation

@JaydenR2305
Copy link
Member

Currently, the @particle_input decorator supports ParticleList annotations but does not provide robust support for casting for ParticleListLike objects. This PR will implement support for casting of iterables and strings to ParticleList objects for parameters annotated with ParticleListLike.

@JaydenR2305 JaydenR2305 requested a review from a team as a code owner July 26, 2024 14:41
@JaydenR2305 JaydenR2305 requested review from namurphy and removed request for a team July 26, 2024 14:41
@github-actions
Copy link

Thank you for submitting this pull request (PR)! ✨ PlasmaPy depends on contributors like you, so we deeply appreciate it!

The bottom of this page shows several checks that are run for every PR. ✅ Don't worry if something broke! We break stuff all the time. 😅 Click on "Details" to learn why a check didn't pass. Please also feel free to ask for help. We do that all the time as well! 🌸 We invite you to our weekly community meeting (2 pm ET on most Tuesdays) and office hours (3 pm ET on most Thursdays).

After this PR is marked as ready for review, someone should stop by to provide a code review and offer suggestions soon. 👥 If we don't provide a review within a few days, please feel free to send us a reminder. 🎗

PlasmaPy's contributor guide has information on:

Tip

📚 For a documentation preview, click on Details next to docs/readthedocs.org:plasmapy. For cryptic documentation errors, see the documentation troubleshooting guide.

[!HINT]
🧹 Automatically fix most pre-commit.ci failures by commenting pre-commit.ci autofix below. For other failures, please see the pre-commit troubleshooting guide.

We thank you once again! 🌌

@github-actions github-actions bot added plasmapy.particles Related to the plasmapy.particles subpackage python Pull requests that update Python code labels Jul 26, 2024
@github-actions github-actions bot added the feature For new functionality, excluding breaking changes label Jul 26, 2024
@codecov
Copy link

codecov bot commented Jul 26, 2024

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.62%. Comparing base (e98f696) to head (0051443).
⚠️ Report is 120 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2790   +/-   ##
=======================================
  Coverage   94.61%   94.62%           
=======================================
  Files         107      107           
  Lines        9664     9667    +3     
  Branches     1464     1466    +2     
=======================================
+ Hits         9144     9147    +3     
  Misses        327      327           
  Partials      193      193           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@JaydenR2305
Copy link
Member Author

JaydenR2305 commented Jul 26, 2024

# Apply casting to arguments annotated with ParticleList and ParticleListLike
# If the argument is already an iterable, it will be cast to a ParticleList
# otherwise, cast it to a list
if (
not isinstance(argument, ParticleList)
and (not isinstance(argument, Iterable) or isinstance(argument, str))
): # TODO: figure out the python hierarchy for arrays like this so we can find the difference Iterable \ str
argument = [argument]

I'm trying to figure out if there's some type in Python that includes the usual Iterable types like tuples, lists, and sets, but doesn't include strings (referring to the logic on line 595)

@namurphy
Copy link
Member

I'm trying to figure out if there's some type in Python that includes the usual Iterable types like tuples, lists, and sets, but doesn't include strings

Hm...I don't know of any type or typing construct like that, unfortunately.

This reminds me that we used to have a problem where ParticleList("He") was the same as ParticleList(["H", "e-"])... 👀

@JaydenR2305
Copy link
Member Author

This reminds me that we used to have a problem where ParticleList("He") was the same as ParticleList(["H", "e-"])... 👀

Yeah that should be fixed now with the above logic. I'll double check just to be 100% :)

@namurphy namurphy added the no changelog entry needed For PRs that should not have a changelog entry label Jul 31, 2024
Copy link
Member

@namurphy namurphy left a comment

Choose a reason for hiding this comment

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

Thank you for adding this, and my apologies for taking so long to merge it! I expect that this will be useful. I added a changelog entry, and will merge it shortly.

Comment on lines +676 to +679
@pytest.mark.parametrize("particle", ["p+", ("p+", "D+"), ["He-4", "Al", "Si"]])
@particle_input
def test_particle_list_input(particle: ParticleListLike) -> None:
assert isinstance(particle, ParticleList)
Copy link
Member

Choose a reason for hiding this comment

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

🪩 That's a cool idea to decorate the test itself in order to test the decorator. I like this pattern!

@namurphy namurphy removed the no changelog entry needed For PRs that should not have a changelog entry label May 28, 2025
@namurphy namurphy merged commit 8b617b8 into PlasmaPy:main May 28, 2025
21 of 22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature For new functionality, excluding breaking changes plasmapy.particles Related to the plasmapy.particles subpackage python Pull requests that update Python code testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants