Make it so that mypy thinks that `ParticleLike` has the same attributes as `Particle`
Nobody has claimed this yet.
- 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
.pyitype stub file that overridesParticleLike - Add an
if typing.TYPE_CHECKINGblock that adds the attributes toParticleLike - 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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