Repository navigation
Improve type hint annotations for @particle_input - #2443
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #2443 +/- ##
=======================================
Coverage 96.92% 96.93%
=======================================
Files 104 104
Lines 9153 9163 +10
=======================================
+ Hits 8872 8882 +10
Misses 281 281 ☔ View full report in Codecov by Sentry. |
| if isinstance(uncharged, Iterable): | ||
| uncharged = any(uncharged) | ||
| lacks_charge_info = any(lacks_charge_info) | ||
| lacks_charge_info = any(lacks_charge_info) # type: ignore[arg-type] |
There was a problem hiding this comment.
For some reason, the built-in any and all annotations aren't matching the types that we're using here.
| def f(particle: ParticleLike) -> Union[Particle, CustomParticle, ParticleList]: | ||
| return particle # type: ignore[return-value] |
There was a problem hiding this comment.
The # type: ignore comment here reflects that static type checkers like mypy are unable to tell that there is an implicit object conversion happening here, which would require actually running the code in @particle_input instead of statically analyzing it. This is going to show up in a bunch of places throughout the code. In these cases, it's probably fine to explicitly ignore the mypy error since it is something that mypy is unable to check.
| `dict` of `str` to `object` | ||
| """ | ||
| return self._data.get("annotations") | ||
| return self._data.get("annotations") # type: ignore[return-value] |
There was a problem hiding this comment.
I didn't want to bother with fixing this since there's a new way to get annotations in Python 3.10, and we're dropping Python 3.9 support soon anyway.
| [mypy-plasmapy.particles.decorators] | ||
| disable_error_code = arg-type,assignment,call-arg,index,misc,no-any-return,no-untyped-call,no-untyped-def,operator,return-value,type-arg,union-attr,var-annotated |
There was a problem hiding this comment.
These are the mypy errors that existed in plasmapy.particles.decorators before this pull request. My process was essentially to:
- Remove one error code and find the errors that mypy was reporting
- Either fix those errors, or add a
#type: ignorecomment if the reported error wasn't actually a problem with the code GOTO 1
|
Thank you for the reviews, @ejohnson-96! |
Description
This PR adds & improves type hint annotations in
plasmapy.particles.decoratorswhich is where@particle_inputis defined, along with the tests.There are some
# type: ignore[...]statements for errors due to mypy's limitation as a static (rather than dynamic) type checker. We'll probably need to add that to the contributor guide.Motivation and context
See #2434.
Related issues
This PR is a spinoff of #2429, and the motivation for #2442. Closes #2434.