PlasmaPy / PlasmaPy/PlasmaPy

Enable a way to enforce the length of a `ParticleList` upon creation

Open
#2,045 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature request Plasma Lv1 | Beginner plasmapy.particles priority: low Python Lv3 | Proficient
Dominant language
Python
Stars
711
Forks
375
Avg merge
3h 2m
Merged PRs (30d)
8

Description

Feature description

We should create a mechanism by which we can easily enforce the length of a ParticleList.

Motivation

When dealing with particle interactions, it's common to need to create a ParticleList of length two. Right now, it's necessary to add a couple of lines of code:

if len(particle_list) != 2:
    raise ValueError("Add an error message here.")

This isn't so long, but we'd need to recreate the error message each time, so we'd end up with repeating code. Given how frequently this comes up, it'd be helpful to have a more standard error message and way of handling this situation.

Implementation strategy

This could be in ParticleList itself, via @particle_input(), or through the creation of a separate function.

If we do this via @particle_input(), we could use the new annotated typing capabilities. These are the capabilities that let us have annotations like u.Quantity[u.kg], and use the __class_getitem__ dunder, so it could be ParticleListLike[2]. Alternately, we could have a separate keyword argument in the signature of @particle_input().

When the length is

Additional context

Many thanks to @ejohnson-96 for originally discussing this idea with me!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the ParticleList implementation and the @particle_input() entry point, since the issue proposes either location for enforcing length. Compare the possible approaches described, then define the behavior and standard error message for length-constrained ParticleList creation; the issue does not name a test file.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.