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

Implement a Protocol for Quantity-like #3

Open
mhvk opened this issue Nov 27, 2024 · 0 comments
Open

Implement a Protocol for Quantity-like #3

mhvk opened this issue Nov 27, 2024 · 0 comments

Comments

@mhvk
Copy link
Contributor

mhvk commented Nov 27, 2024

Currently, we use

def get_value_and_unit(arg, default_unit=None):
    try:
        unit = arg.unit
    except AttributeError:
        return arg, default_unit
    else:
        return arg.value, unit

which, if a protocol existed, would be as simple as

def get_value_and_unit(arg, default_unit=None):
    return (arg.value, arg.unit) if isinstance(arg, QuantityAPI) else (arg, default_unit)

The question is what exactly the protocol should be. E.g., from https://github.com/astropy/quantity-2.0/pull/2/files#r1858839304

@runtime_checkable
class QuantityAPI(Protocol):
    value: Array
    unit: Unit

But perhaps this needs more discussion with other units package maintainers?

@mhvk mhvk mentioned this issue Nov 27, 2024
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

No branches or pull requests

1 participant