PlasmaPy / PlasmaPy/PlasmaPy

Make it so that mypy thinks that `ParticleLike` has the same attributes as `Particle`

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

Nobody has claimed this yet.

static type checking
Dominant language
Python
Stars
711
Forks
375
Avg merge
3h 2m
Merged PRs (30d)
8

Description

In functions decorated with @particle_input, arguments are converted from something ParticleLike into a Particle | CustomParticle | ParticleList. When an attribute of the processed argument is used, mypy detects a union-attr error, such as in plasmapy.particles.atomic. If we made mypy and static type checkers interpret that ParticleLike had appropriate attributes, then we could avoid these mypy errors. All this applies to ParticleListLike too.

The possibilities that I can think of that might work are:

  • Create a .pyi type stub file that overrides ParticleLike
  • Add an if typing.TYPE_CHECKING block that adds the attributes to ParticleLike
  • Actually add attributes to ParticleLike (though this doesn't seem like a clean way to go about it).

This probably requires some thought, and a better understanding of static type checking in Python. In the meantime, we should add type: ignore[union-attr] comments when this happens. (We probably don't want to disable union-attr checks entirely because they've helped me identify potential bugs.)

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 with the particle_input usage in plasmapy/particles/atomic.py around line 84 and trace the definitions of ParticleLike and ParticleListLike. Compare a stub, TYPE_CHECKING attributes, and runtime attributes while preserving useful union-attr checks. Done means mypy can access the appropriate Particle attributes without unnecessary ignores, with the affected checks passing.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.