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

Numpy 1.21 NDArray Type Error #1565

Closed
Antyos opened this issue Jul 17, 2021 · 3 comments
Closed

Numpy 1.21 NDArray Type Error #1565

Antyos opened this issue Jul 17, 2021 · 3 comments
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@Antyos
Copy link

Antyos commented Jul 17, 2021

Environment data

  • Language Server version: 2021.7.4 (pyright 209f9621)
  • OS and version: Windows 10 20H2
  • Python version: 3.8.10 and 3.9.4
  • Numpy version: 1.21.0

Expected behaviour

Numpy v1.21.0 introduced runtime subscriptable aliases for ndarrays.

When defining variables using the generic type hint numpy.typing.NDArray, Pylance should not raise an error when attempting to index into it.

Actual behaviour

Pylance raises the error:

Could not specialize type "NDArray[ScalarType@NDArray]"
  Type "int" is incompatible with bound type "generic" for type variable "ScalarType@NDArray"
    "int" is incompatible with "generic" Pylance

If using slice notation (a[:]), the error message is basically the same, but refers to "slice" instead of "int"`

Logs

[Info  - 10:15:30 PM] Pylance language server 2021.7.4 (pyright 209f9621) starting
[Info  - 10:15:30 PM] Server root directory: c:\Users\Andrew\.vscode\extensions\ms-python.vscode-pylance-2021.7.4\dist
[Info  - 10:15:31 PM] No pyproject.toml file found.
[Info  - 10:15:31 PM] Setting pythonPath for service "<default>": "C:\Users\Andrew\AppData\Local\Programs\Python\Python39\python.exe"
[Error - 10:15:31 PM] venvPath ~\.virtualenvs is not a valid directory.
[Warn  - 10:15:31 PM] stubPath typings is not a valid directory.
[Info  - 10:15:31 PM] Assuming Python version 3.9
[Info  - 10:15:31 PM] Assuming Python platform Windows
Search paths for 
  c:\Users\Andrew\.vscode\extensions\ms-python.vscode-pylance-2021.7.4\dist\typeshed-fallback\stdlib
  
  typings
  c:\Users\Andrew\.vscode\extensions\ms-python.vscode-pylance-2021.7.4\dist\typeshed-fallback\stubs\...
  c:\Users\Andrew\.vscode\extensions\ms-python.vscode-pylance-2021.7.4\dist\bundled\stubs
  c:\Users\Andrew\AppData\Local\Programs\Python\Python39\DLLs
  c:\Users\Andrew\AppData\Local\Programs\Python\Python39\Lib
  c:\Users\Andrew\AppData\Local\Programs\Python\Python39
  c:\Users\Andrew\AppData\Roaming\Python\Python39\site-packages
  c:\Users\Andrew\AppData\Local\Programs\Python\Python39\Lib\site-packages
  c:\Users\Andrew\AppData\Local\Programs\Python\Python39\Lib\site-packages\Equation-1.3.dev20210526-py3.9.egg
  c:\Users\Andrew\AppData\Local\Programs\Python\Python39\Lib\site-packages\win32
  c:\Users\Andrew\AppData\Local\Programs\Python\Python39\Lib\site-packages\win32\lib
  c:\Users\Andrew\AppData\Local\Programs\Python\Python39\Lib\site-packages\pythonwin
[Info  - 10:15:33 PM] Searching for source files
[Info  - 10:15:33 PM] No source files found.
[FG] parsing: c:\Users\Andrew\Code\FSABM-v2\foo.py (95ms)
[FG] parsing: c:\Users\Andrew\.vscode\extensions\ms-python.vscode-pylance-2021.7.4\dist\typeshed-fallback\stdlib\builtins.pyi [fs read 6ms] (185ms)
[FG] binding: c:\Users\Andrew\.vscode\extensions\ms-python.vscode-pylance-2021.7.4\dist\typeshed-fallback\stdlib\builtins.pyi (49ms)
[FG] binding: c:\Users\Andrew\Code\FSABM-v2\foo.py (0ms)
[Info  - 10:15:33 PM] Background analysis(1) root directory: c:\Users\Andrew\.vscode\extensions\ms-python.vscode-pylance-2021.7.4\dist
[Info  - 10:15:33 PM] Background analysis(1) started
Background analysis message: setConfigOptions
Background analysis message: ensurePartialStubPackages
Background analysis message: setTrackedFiles
Background analysis message: markAllFilesDirty
Background analysis message: setFileOpened
Background analysis message: getDiagnosticsForRange
Background analysis message: analyze
[BG(1)] analyzing: c:\Users\Andrew\Code\FSABM-v2\foo.py ...
[BG(1)]   parsing: c:\Users\Andrew\Code\FSABM-v2\foo.py (65ms)
[BG(1)]   parsing: c:\Users\Andrew\.vscode\extensions\ms-python.vscode-pylance-2021.7.4\dist\typeshed-fallback\stdlib\builtins.pyi [fs read 3ms] (128ms)
[BG(1)]   binding: c:\Users\Andrew\.vscode\extensions\ms-python.vscode-pylance-2021.7.4\dist\typeshed-fallback\stdlib\builtins.pyi (38ms)
[BG(1)]   binding: c:\Users\Andrew\Code\FSABM-v2\foo.py (1ms)
[BG(1)]   checking: c:\Users\Andrew\Code\FSABM-v2\foo.py ...
[BG(1)]     parsing: c:\Users\Andrew\.vscode\extensions\ms-python.vscode-pylance-2021.7.4\dist\typeshed-fallback\stdlib\typing.pyi [fs read 3ms] (55ms)
[BG(1)]     binding: c:\Users\Andrew\.vscode\extensions\ms-python.vscode-pylance-2021.7.4\dist\typeshed-fallback\stdlib\typing.pyi (12ms)
[BG(1)]     parsing: c:\Users\Andrew\.vscode\extensions\ms-python.vscode-pylance-2021.7.4\dist\typeshed-fallback\stdlib\_typeshed\__init__.pyi [fs read 1ms] (23ms)
[BG(1)]     binding: c:\Users\Andrew\.vscode\extensions\ms-python.vscode-pylance-2021.7.4\dist\typeshed-fallback\stdlib\_typeshed\__init__.pyi (2ms)
[BG(1)]     parsing: c:\Users\Andrew\.vscode\extensions\ms-python.vscode-pylance-2021.7.4\dist\typeshed-fallback\stdlib\typing_extensions.pyi [fs read 0ms] (2ms)
[BG(1)]     binding: c:\Users\Andrew\.vscode\extensions\ms-python.vscode-pylance-2021.7.4\dist\typeshed-fallback\stdlib\typing_extensions.pyi (1ms)
[BG(1)]     parsing: c:\Users\Andrew\AppData\Local\Programs\Python\Python39\Lib\site-packages\numpy\__init__.pyi [fs read 1ms] (489ms)
[BG(1)]     binding: c:\Users\Andrew\AppData\Local\Programs\Python\Python39\Lib\site-packages\numpy\__init__.pyi (57ms)
[BG(1)]     parsing: c:\Users\Andrew\AppData\Local\Programs\Python\Python39\Lib\site-packages\numpy\typing\__init__.py [fs read 1ms] (33ms)
[BG(1)]     binding: c:\Users\Andrew\AppData\Local\Programs\Python\Python39\Lib\site-packages\numpy\typing\__init__.py (2ms)
[BG(1)]     parsing: c:\Users\Andrew\AppData\Local\Programs\Python\Python39\Lib\site-packages\numpy\typing\_generic_alias.py [fs read 1ms] (21ms)
[BG(1)]     binding: c:\Users\Andrew\AppData\Local\Programs\Python\Python39\Lib\site-packages\numpy\typing\_generic_alias.py (3ms)
[BG(1)]   checking: c:\Users\Andrew\Code\FSABM-v2\foo.py (752ms)
[BG(1)] analyzing: c:\Users\Andrew\Code\FSABM-v2\foo.py (987ms)
Background analysis message: getDiagnosticsForRange
Background analysis message: resumeAnalysi

Code Snippet / Additional information

import numpy as np
import numpy.typing as npt


# NOT ok
def func1(a: npt.NDArray):
    print(a[0])  # See error message above
	print(a[0:])

# Ok
def func2(a: npt.NDArray[np.float64]):
    print(a[0])

# Ok
def func3(a: np.ndarray):
    print(a[0])
@erictraut
Copy link
Contributor

Thanks for the bug report. This will be fixed in the next release.

@erictraut erictraut added bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version and removed triage labels Jul 17, 2021
@erictraut
Copy link
Contributor

In the meantime, you can work around the problem by explicitly specifying Any as the type argument to the generic type alias NDArray.

@jakebailey
Copy link
Member

This issue has been fixed in version 2021.7.5, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#202175-22-july-2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version
Projects
None yet
Development

No branches or pull requests

3 participants