Skip to content

Make @validate_quantities accept Quantity[Unit] style annotations - #2346

Merged
namurphy merged 12 commits into
PlasmaPy:mainfrom
namurphy:validate-quantities-annotations
Sep 28, 2023
Merged

namurphy merged 12 commits into
PlasmaPy:mainfrom
namurphy:validate-quantities-annotations

Conversation

@namurphy

@namurphy namurphy commented Sep 26, 2023 •

Copy link
Copy Markdown
Member

Description

This PR makes it so that @validate_quantities accepts annotations of the style:

@validate_quantities
def convert_to_meters(x: u.Quantity[u.m]) -> u.Quantity[u.m]:
    return x

I still need to update the documentation and make more thorough tests.

This might not work for Python 3.9, but it should work for Python 3.10+. We can add the capability now, but we will not be able to apply it in our repository until we drop Python 3.9 support for our 2024.5.0 release.

I was pleasantly surprised that this is working for Python 3.9 as well as 3.10 & 3.11. Not sure why...but I'll take it!

Motivation and context

Before this pull request, annotations used by @validate_quantities had to be unit-like objects, like f(x: u.m):. However, this is incompatible with mypy, which is expecting Quantity as an annotation. This PR will make it so that annotations can actually be type hint annotations.

Related issues

Closes #1413. Related to #268.

@github-actions

Copy link
Copy Markdown

Thank you for submitting a pull request (PR) to PlasmaPy! ✨ The future of the project depends on contributors like you, so we deeply appreciate it! 🌱

Our contributor guide has information on:

The bottom of this page shows several checks that are run for every PR. Don't worry if something broke! We break stuff all the time. 😺 Click on "Details" to learn why a check didn't pass. Please also feel free to ask for help. We do that all the time as well. 🌸 You can find us in our chat room or weekly community meeting & office hours. Here are some tips:

  • Try fixing CI / Python 3.11 test failures first.
  • Most pre-commit.ci - pr failures can be automagically fixed by commenting pre-commit.ci autofix below, followed by a git pull to bring the changes back to your computer.
  • If pre-commit.ci - pr says that a function is too long or complex, try breaking up that function into multiple short functions that each do one thing. See also these tips on writing clean scientific software.
  • If the CI / Documentation check ends with a cryptic error message, check out our documentation troubleshooting guide.
  • For a documentation preview, click on Details next to docs/readthedocs.org:plasmapy.

If this PR is marked as ready for review, someone should stop by to provide a code review and offer suggestions soon. ✅ If you don't get a review within a few days, please feel free to send us a reminder.

Please also use SI units within PlasmaPy, except when there is strong justification otherwise or in some examples.

We thank you once again!

@github-actions github-actions Bot added testing plasmapy.utils Related to the plasmapy.utils subpackage labels Sep 26, 2023
@codecov

codecov Bot commented Sep 26, 2023 •

Copy link
Copy Markdown

Codecov Report

All modified lines are covered by tests ✅

Comparison is base (8007636) 98.35% compared to head (1483f2a) 98.35%.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2346   +/-   ##
=======================================
  Coverage   98.35%   98.35%           
=======================================
  Files         100      100           
  Lines        8567     8571    +4     
=======================================
+ Hits         8426     8430    +4     
  Misses        141      141           
Files Coverage Δ
plasmapy/utils/decorators/checks.py 100.00% <100.00%> (ø)
plasmapy/utils/decorators/validators.py 100.00% <ø> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@namurphy
namurphy marked this pull request as ready for review September 28, 2023 19:08
@namurphy
namurphy requested a review from a team as a code owner September 28, 2023 19:08

@ejohnson-96 ejohnson-96 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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

Labels

plasmapy.utils Related to the plasmapy.utils subpackage static type checking testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Adopt Quantity type annotations that preserve units

2 participants