Skip to content

BUG: Usage of implicit reexports breaks using mypy with --no-implicit-reexport #21214

@ThunderKey

Description

@ThunderKey

Describe the issue:

The code example described below, when checked with mypy, reveals the type of the array and generates an error. If mypy is used with the option --no-implicit-reexport the code example described below does not reveal the type and does not generate any error. Adding the option --warn-unreachable shows that everything after numpy.cross is detected as unreachable.

As detected in python/mypy#12350 the issue is that an overload of numpy.cross uses _ArrayLikeBool_co but it is an implicit reexport. Due to that it resolves to Any and the following overload matches all calls to numpy.cross and the calls resolve to NoReturn.

@overload
def cross(
a: _ArrayLikeBool_co,
b: _ArrayLikeBool_co,
axisa: int = ...,
axisb: int = ...,
axisc: int = ...,
axis: None | int = ...,
) -> NoReturn: ...

I see two options to resolve this issue:

  1. Replacing imports from numpy.typing to the package where they are actually defined, like from numpy.typing._array_like import _ArrayLikeBool_co
  2. Explicitly reexport private types from numpy.typing as well.

Reproduce the code example:

import numpy
from numpy.typing import ArrayLike


def test_method() -> None:
    value: ArrayLike = numpy.cross(numpy.array([1, 2, 3]), numpy.array([4, 5, 6]))
    reveal_type(value)
    broken: int = 'test'

Error message:

No response

NumPy/Python version information:

1.22.3 3.10.2 (main, Jan 15 2022, 19:56:27) [GCC 11.1.0]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions