Skip to content

TYP: typehints for __array__ #29203

@MarDiehl

Description

@MarDiehl

Describe the issue:

I have a class that has an numpy array as an attribute. Its __array__ method simply calls np.ndarray.__array__ (see below). Mypy complains about non-matching types, but I believe the typehints that I provide are reasonable.

Reproduce the code example:

import numpy as np
import numpy.typing as npt
from typing import Optional

class Test:

    def __init__(self, ar: np.ndarray):
        self.ar = ar

    def __array__(self,
                  dtype: Optional[npt.DTypeLike] = None,
                  *,
                  copy: Optional[bool] = None) -> np.ndarray:
        return self.ar.__array__(dtype,copy=copy)

Error message:

test.py:14: error: Argument 1 to "__array__" of "ndarray" has incompatible type "type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str | None | None"; expected "None"  [arg-type]
test.py:14: error: Argument "copy" to "__array__" of "ndarray" has incompatible type "builtins.bool | None"; expected "numpy.bool[builtins.bool] | None"  [arg-type]

Python and NumPy Versions:

  • Python 3.13.3
  • NumPy 2.3.0

Type-checker version and settings:

  • Mypy 1.16.0

Additional typing packages.

n/a

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    pFad - Phonifier reborn

    Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

    Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy