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

Incorrect type/operator error for numpy ArrayLike #1198

Closed
theelderbeever opened this issue Apr 28, 2021 · 4 comments
Closed

Incorrect type/operator error for numpy ArrayLike #1198

theelderbeever opened this issue Apr 28, 2021 · 4 comments
Labels
needs investigation Could be an issue - needs investigation

Comments

@theelderbeever
Copy link

PyLance doesn't handle numpy types appropriately and shows them as type errors when attempting to perform operations with them that do exist. Possibly related to numpy/numpy#16515 or #150.

Environment data

  • Language Server version: 2021.4.2
  • OS and version: XXX
  • Python version: 3.8.8 (installed with pyenv)

Expected behaviour

Numpy arrays can be added, subtracted, multiplied, etc and shouldn't show up as an error.

Actual behaviour

image

image

Code Snippet / Additional information

from numpy.typing import ArrayLike

def test_func(x: ArrayLike, y: ArrayLike):
    return x + y
@jakebailey
Copy link
Member

I think this is a duplicate of #1182, and will likely be fixed in the next release.

@jakebailey
Copy link
Member

jakebailey commented Apr 28, 2021

Hm, maybe not. ArrayLike is a union (not a constrained TypeVar, though numpy.typing does have those available), so I don't think there's a guarantee that the two parameters will be compatible with each other when added (or have +, as far as I can tell).

@judej judej added the needs investigation Could be an issue - needs investigation label Apr 29, 2021
@github-actions github-actions bot removed the triage label Apr 29, 2021
@jakebailey
Copy link
Member

I'm inclined to believe that this is working as intended. Take this call for example:

test_func(1234, [1, 2, 3])

This fails at runtime, but the definition of ArrayLike is:

ArrayLike = Union[
    _ScalarLike,
    Sequence[_ScalarLike],
    Sequence[Sequence[Any]],  # TODO: Wait for support for recursive types
    _SupportsArray,
]

Two things being in this union doesn't imply that they are actually compatible. Under the hood, I believe numpy converts its array-like parameters as needed depending on the operation, but a function that tries to just do a + b isn't the same.

@jakebailey
Copy link
Member

If you wanted to be able to handle numpy arrays, you'd probably want:

from numpy import ndarray


def test_func(x: ndarray, y: ndarray) -> ndarray:
    return x + y

This does type check. Or, something like:

import numpy as np
from numpy.typing import ArrayLike

def test_func(x: ArrayLike, y: ArrayLike) -> np.ndarray:
    return np.array(x) + y

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs investigation Could be an issue - needs investigation
Projects
None yet
Development

No branches or pull requests

3 participants