dgasmith / opt_einsum

⚡️Optimizing einsum functions in NumPy, Tensorflow, Dask, and more with contraction order optimization.
https://dgasmith.github.io/opt_einsum/
MIT License
822 stars 67 forks source link

Return type of opt_einsum.contract -- linter errors #212

Closed danielkelshaw closed 1 year ago

danielkelshaw commented 1 year ago

Hi, first off I just want to thank you for developing and maintaining a great package!

I notice that the return type of oe.contract is Any: https://github.com/dgasmith/opt_einsum/blob/1a984b7b75f3e532e7129f6aa13f7ddc3da66e10/opt_einsum/typing.py#L8

This is causing some small annoyances with my lsp when I try to multiply the result of a contraction with a float. For instance, I get the error: Pyright: Operator "*" not supported for types "float" and "Unknown | Any | ContractExpression | tuple[Unknown, Unknown]"

Are you aware of any way to get around this or am I stuck with this error if I continue to use opt_einsum?

To clarify: the code runs fine -- just getting highlighted as an error in my IDE.

danielkelshaw commented 1 year ago

Update: I was using the most recently available version from PyPI, i.e: opt_einsum=3.3.0. When I install from source using the latest version on the repository this issue is resolved.

dgasmith commented 1 year ago

While this is not an issue yet, it will become one upon the next release. It's difficult to type contract well since each Array-type will have unique properties. Typing the interface is difficult as well since there are multiple input pathways and we generally coerce types.

I don't have a great solution here at the moment with Python's current typing system. An idea is that we could try to build a Union on the fly based off imported libraries.