GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — TYP: typehints for __array__ · Issue #29203 · numpy/numpy

TYP: typehints for __array__ #29203

New issue
Copy link
New issue
Copy link
Closed
#29222
#29204
Closed
TYP: typehints for __array__#29203
#29222
#29204
Copy link
Assignees
jorenham
Labels
41 - Static typing

Description

@MarDiehl
MarDiehl
opened on Jun 13, 2025
Issue body actions

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

Activity

  1. MarDiehl
    added
    41 - Static typing
    on Jun 13, 2025
  2. jorenham
    self-assigned this
    on Jun 13, 2025
  3. jorenham commented on Jun 13, 2025

    @jorenham
    jorenham
    on Jun 13, 2025
    Member
    More actions

    Hmm yea, that doesn't seem to be correct:

    numpy/numpy/__init__.pyi

    Lines 2071 to 2078 in 57eedf5

    @overload
    def __array__(
    self, dtype: None = ..., /, *, copy: bool | None = ...
    ) -> ndarray[_ShapeT_co, _DTypeT_co]: ...
    @overload
    def __array__(
    self, dtype: _DTypeT, /, *, copy: bool | None = ...
    ) -> ndarray[_ShapeT_co, _DTypeT]: ...

    The bool here is numpy.bool but should be builtins.bool.

    Reacted by Martin Diehl
  4. seberg
    closed this as completedin #29204on Jun 13, 2025
  5. MarDiehl
    added a commit that references this issue on Jun 16, 2025

    delegate the work to numpy

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

Metadata

Metadata

Assignees

  • @jorenham
    jorenham

Labels

41 - Static typing

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.