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

Disallowed typing in linalg.vector_norm #817

Open
lucascolley opened this issue Jul 1, 2024 · 4 comments
Open

Disallowed typing in linalg.vector_norm #817

lucascolley opened this issue Jul 1, 2024 · 4 comments
Labels
topic: Static Typing Static typing.

Comments

@lucascolley
Copy link
Member

Spec:

ord: int | float | ~typing.Literal[inf, -inf] = 2

Stubs:

ord: Union[int, float, Literal[inf, -inf]] = 2

Literals docs

The following are provisionally disallowed for simplicity. We can consider allowing them in the future.

  • Floats: e.g. Literal[3.14]. Representing Literals of infinity or NaN in a clean way is tricky; real-world APIs are unlikely to vary their behavior based on a float parameter.

A relevant issue: python/typing#1160


I don't know if anything can be done, and this was probably done deliberately, but useful to have an issue nonetheless.

@asmeurer
Copy link
Member

asmeurer commented Jul 1, 2024

Yes, this is a known issue. There are some instances where the Python typing rules disallow certain things and we've opted to keep the annotations accurate in those cases. Implementations may need to adjust them until the upstream typing rules can be fixed.

@jorenham
Copy link

jorenham commented Aug 19, 2024

float('inf') and float('-inf') are both assignable to the float type, so why not just use ord: int | float?

To illustrate

VectorOrder: typing.TypeAlias = int | float
orf: VectorOrder = float('inf')  # no problem

pyright playground link

@asmeurer
Copy link
Member

asmeurer commented Sep 3, 2024

That probably make sense for vector_norm, which actually accepts any float value (other than nan) https://data-apis.org/array-api/latest/extensions/generated/array_api.linalg.vector_norm.html#vector-norm

For matrix_norm, it says float, but it actually only supports integers and inf and -inf. A more accurate type description would be int | Literal[inf, -inf, 'fro', 'nuc'] https://data-apis.org/array-api/latest/extensions/generated/array_api.linalg.matrix_norm.html#matrix-norm

In any case, it probably is better to just loosen the typing here so that it can work with typing machinery. The actual set of allowed inputs is not representable by Python's typing rules, but it is spelled out in the standard text.

@jorenham
Copy link

jorenham commented Sep 4, 2024

FYI, enum values are allowed in typing.Literal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: Static Typing Static typing.
Projects
None yet
Development

No branches or pull requests

4 participants