27

As a follow-up to this question I have a function that will return a 2D numpy.array of fixed columns but variable rows

import numpy.typing as npt

def example() -> npt.ArrayLike:
    data = np.array([[1,2,3],
                     [4,5,6],
                     ...,
                     [x,y,z]])

How can I specifically hint that the returned array will be 3 columns by N (variable) rows?

4
  • 1
    Does this answer your question? Type hinting / annotation (PEP 484) for numpy.ndarray Commented Mar 16, 2021 at 14:28
  • 1
    Numpy Typing documentation doesn't say anything about possible dimensions. So maybe you cannot easily currently. However with Python new type, you may be able to achieve a custom solution. Depends on what you want to do with the type hints, I guess. Commented Mar 16, 2021 at 14:28
  • There is an answer there that specifies the shape of the numpy array... Commented Mar 16, 2021 at 14:38
  • @SamuelNLP, that link was asked years ago before the addition of the numpy typing module. I've had to add a big disclaimer to the accepted answer because people kept giving it a thumbs down. Stick with a reference to the current numpy developments. Commented Mar 16, 2021 at 17:06

1 Answer 1

24

Update on October 24, 2022

This is still not possible currently, but according to this comment on a Numpy GitHub issue, it will be possible once mypy supports PEP 646. Please see the relevant issue on mypy's GitHub repo. That issue is open at the time of writing.

Python 3.11 has been released today with support for PEP646. Once mypy supports PEP646, users will be able to type-hint the shapes of Numpy arrays.


Older answer

It seems like it is not possible to type-hint the shape (or data type) of a numpy.ndarray at this point (September 13, 2022). There are, however, some recent pull requests to numpy working towards this goal.

  • https://github.com/numpy/numpy/pull/17719

    makes the np.ndarray class generic w.r.t. its shape and dtype: np.ndarray[~Shape, ~DType]

    However, an explicit non-goal of that PR is to make runtime-subscriptable aliases for numpy.ndarray. According to that PR, those changes will come in later PRs.

  • https://github.com/numpy/numpy/issues/16544

    Issue discussing typing support for shapes. It is still open at the time of writing.

PEP 646 is related to this and has been accepted into Python 3.11. According to numpy/numpy issue #16544, one will be able to type-hint shape and data type of arrays after type checkers like mypy add support for PEP 646.


This is possible to do with the nptyping package, but that is not part of numpy.

from typing import Any
from nptyping import NDArray

# Nx3 array with Any data type.
NDArray[(Any, 3), Any]
Sign up to request clarification or add additional context in comments.

3 Comments

Did numpy land support for PEP 646, could provide some examples?
Is there an update on this?
@Royi - sort of... see github.com/numpy/numpy/issues/16544#issuecomment-1804251364 feel free to update this answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.