Skip to content

Adopt Quantity type annotations that preserve units #1413

Closed

Description

@namurphy

For many of our functions, we use annotations for units:

def func(x: u.m, t: u.s) -> u.m / u.s:
    return (x / t).to(u.m / u.s)

These are not type hint annotations, since the expected value is a Quantity in most situations. Instead the annotations are used by decorators like our validators and astropy.units.quantity_input. Unfortunately, this means that we can't use static type checkers like mypy to check PlasmaPy code. 😿 Using mypy has the potential to help us find bugs, as well as errors in existing type hint annotations (#268).

Astropy now allows for Quantity type annotations that preserve units. Both typing.Union and typing.Optional are supported.

def func(x: Quantity[u.kpc]) -> Quantity[u.m]:
    return x << u.m

We should migrate towards using Quantity annotations that preserve units in PlasmaPy. However, before we do this, we would need to change our validators to make them work with this style of annotation.

Activity

  1. added
    effort: highRequiring perhaps ∼1–2 weeks. Can this be split up into multiple smaller/focused issues?
    on Jan 31, 2022
  2. added
    StaleDormant issues & PRs which will be automatically closed if the label is not removed.
    on Apr 1, 2022
  3. removed
    StaleDormant issues & PRs which will be automatically closed if the label is not removed.
    on May 12, 2023
  4. namurphy commented on Oct 17, 2023

    @namurphy
    MemberAuthor

    I closed the wrong issue here with #2346. I meant to close #1414.

  5. namurphy commented on Jan 17, 2024

    @namurphy
    MemberAuthor

    Closed by #2421!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Plasma Lv1 | BeginnerIssues appropriate for someone who has some knowledge of physicsPython Lv3 | ProficientIssues that require proficiency in Pythoneffort: highRequiring perhaps ∼1–2 weeks. Can this be split up into multiple smaller/focused issues?static type checking

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions