Skip to content
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

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

Open
namurphy opened this issue Mar 21, 2024 · 0 comments

Comments

@namurphy
Copy link
Member

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.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant