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

Enable Quantity typing with syntax of Quantity[unit] #95

Merged
merged 2 commits into from
Jan 15, 2025
Merged

Conversation

chaoming0625
Copy link
Collaborator

@chaoming0625 chaoming0625 commented Jan 15, 2025

This pull request introduces type variables and generics to the Quantity class in the brainunit module and includes corresponding tests. The changes enhance type safety and flexibility when working with quantities.

Code example:

        def f1(a: u.Quantity[u.ms]) -> u.Quantity[u.ms]:
            return a

This enhance the readability of the Quantity typing.

Type Enhancements:

  • brainunit/_base.py: Added TypeVar and Generic imports to enable the use of generics in the Quantity class.
  • brainunit/_base.py: Introduced a new type variable A to be used with the Quantity class.
  • brainunit/_base.py: Modified the Quantity class to inherit from Generic[A], making it a generic class.

Testing Enhancements:

  • brainunit/_base_test.py: Added a new test test_quantity_type to verify the type safety and flexibility of the Quantity class with different type variables.
  • brainunit/_base_test.py: Removed unnecessary blank lines in the test_pickle function for better code readability.

Copy link
Contributor

sourcery-ai bot commented Jan 15, 2025

Reviewer's Guide by Sourcery

This pull request introduces generics to the Quantity class in brainunit to enhance type safety. It modifies the class definition and adds corresponding tests.

Sequence diagram showing type checking with generic Quantity

sequenceDiagram
    participant Dev as Developer
    participant TC as Type Checker
    participant Q as Quantity[ms]
    Dev->>TC: Define function with Quantity[ms]
    TC->>Q: Verify parameter type
    Q-->>TC: Type matches
    TC-->>Dev: Type check passes
Loading

Class diagram showing updated Quantity class with generics

classDiagram
    class Generic~A~ {
    }
    class Quantity~A~ {
        +mantissa
        +unit
    }
    Generic <|-- Quantity
    note for Quantity "Now supports generic type parameter A
for type-safe unit specifications"
Loading

File-Level Changes

Change Details Files
Introduced generics to the Quantity class.
  • Added TypeVar and Generic imports.
  • Introduced type variable A.
  • Modified the Quantity class to inherit from Generic[A].
brainunit/_base.py
Added tests for the generic Quantity class.
  • Added test_quantity_type test to verify type safety with different type variables.
brainunit/_base_test.py
Removed unnecessary blank lines.
  • Removed blank lines in the test_pickle function for better readability.
brainunit/_base_test.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @chaoming0625 - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟡 Testing: 1 issue found
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +905 to +906
def f1(a: u.Quantity[u.ms]) -> u.Quantity[u.mV]:
return a
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (testing): Missing assertion in test_quantity_type

The test test_quantity_type defines functions f1, f2, and f3 but doesn't include any assertions to verify their behavior. It should assert that these functions either raise expected type errors or return values of the correct types. For instance, f1 should probably raise a type error because it claims to return u.Quantity[u.mV] but actually returns u.Quantity[u.ms].

@chaoming0625 chaoming0625 merged commit 862210b into main Jan 15, 2025
30 checks passed
@chaoming0625 chaoming0625 deleted the update branch January 15, 2025 09:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant