Skip to content

Subtype (numpy.float64) is reported as incompatible with parent type (float) #4332

Closed

Description

See also numpy/numpy#23663

Environment data

  • Language Server version: v2023.5.10
  • OS and version: Ubuntu 22.04.2 LTS
  • Python version (& distribution if applicable, e.g. Anaconda): 3.11.3 Miniconda
  • numpy version: 1.24.3
  • python.analysis.indexing: true
  • python.analysis.typeCheckingMode: basic

environment.yml

name: consolidation
channels:
  - conda-forge
  - defaults
dependencies:
  - black=23.3
  - bottleneck=1.3
  - ipywidgets=8.0
  - jupyterlab=3.6
  - matplotlib=3.7
  - netcdf4=1.6
  - numpy=1.24
  - pandas=2.0
  - python=3.11
  - scipy=1.10
  - seaborn=0.12
  - tqdm=4.65
  - xarray=2023.4
  - pip=23.1
  - pip:
      - bctpy==0.6

Code Snippet

import numpy as np

def idx(x: float) -> float:
    return x

arr = np.arange(5.0, dtype=np.float64)
sum = np.sum(arr)
idx(sum)

Repro Steps

  1. Create an environment using the provided environment.yml file
  2. Open VSCode
  3. Create a new Python file
  4. Select the newly created environment as interpreter
  5. Enter the provided code snippet

Expected behavior

I would expect Pylance to not give any error messages for the above code snippet, as np.float64 is a subtype of python's native float type.

Actual behavior

Pylance reports the following error message:

Argument of type "float64" cannot be assigned to parameter "x" of type "float" in function "idx"
  "float64" is incompatible with "float" Pylance (reportGeneralTypeIssues)

Logs

Python Language Server.log

Activity

  1. erictraut commented on May 9, 2023

    @erictraut
    Contributor

    At runtime, numpy.float64 has the following inheritance hierarchy:

    numpy.float64
    numpy.inexact
    numpy.number
    numpy.generic
    float
    

    However, the type stubs that are bundled with numpy don't define numpy.generic as a subtype of float. Rather, they define numpy.generic as derived from an internal class called numpy._ArrayOrScalarCommon which has no relationship to float. For this reason, a static type checker will not see float as being a subtype of numpy.float64.

    Not surprisingly, mypy (a different static type checker) emits the same error as pylance in this case.

    I don't know why the numpy authors have chosen to define numpy.generic in a way that deviates from the runtime. You may want to post a question in the numpy discussion forum.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions