Skip to content

False positive: in overloads, mypy is confused by torch.Tensor & numpy.ndarray #13032

@Yura52

Description

@Yura52

Bug Report

(A clear and concise description of what the bug is.)

To Reproduce

# a.py
from typing import overload
import numpy
import torch

@overload
def f(a: numpy.ndarray) -> numpy.ndarray: ...

@overload
def f(a: torch.Tensor) -> torch.Tensor: ...

def f(a):
    return a + 1

mypy a.py

Expected Behavior

No errors reported by mypy, since this is a correct usage of overloading.

Actual Behavior

Overloaded function signature 2 will never be matched: signature 1's parameter type(s) are the same or broader

In fact, if torch overload is declared first, no errors are reported.

Environment

  • macOS: 11.6
  • VSCode: 1.68.1
  • Pylance: v2022.6.20

I am working on this repository. So, my environment can be reproduced as follows:

# you can use conda instead of micromamba, the result is the same
micromamba create -n rtdl python=3.7
micromamba activate rtdl
pip install numpy=1.18.5 torch=1.7.1 scikit-learn=1.0.2
pip install -r requirements_dev.txt

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions