Repository navigation
Change unit annotations to Quantity annotations - #2421
Conversation
|
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #2421 +/- ##
=======================================
Coverage 96.91% 96.91%
=======================================
Files 104 104
Lines 9121 9121
=======================================
Hits 8840 8840
Misses 281 281 ☔ View full report in Codecov by Sentry. |
Quantity annotations
pheuer
left a comment
There was a problem hiding this comment.
Lots of changes, but looks pretty straightforward. I had to open a console to realize that u.Quantity[u.m] returns a typing annotation. The coding guide could I guess mention that, but the example is pretty clear.
Does this PR make this change on every instance in the package?
| "RUF013", # implicit-optional | ||
| "RUF015", # unnecessary-iterable-allocation-for-first-element | ||
| "RUF016", # invalid-index-type | ||
| "RUF018", # assignment-in-assert |
There was a problem hiding this comment.
This is an experimental rule recently added by ruff, and I was locally getting warning messages about not running ruff with --preview enabled. It's unrelated to this PR.
I used Thank you so much for the review! |
|
Correction: this PR doesn't change all of the unit annotations across the package. In |
Description
This PR changes annotations like
u.kgtou.Quantity[u.kg]so that they are true type hint annotations:Motivation and context
At present, we use annotations like
u.kgto specify the required units for@validate_quantities, which then verifies that the quantities are of the appropriate physical type and converts them to the correct units. However, this prevents us from using static type checking tools like mypy. While these are annotations, they are not type hint annotations because things likeu.kgare not a type.Enabling mypy (#2424) will let us catch errors in type hint annotations, which we currently have no way of doing.
Here's an example docstring before (with unit annotations) and after (with
Quantityannotations).Related issues
Related to: #268, #2424