python / python/mypy

Option to remove unsupported annotations from `numpy.ndarray` and `List` in stubgen

Open
#12,992 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature topic-stubgen
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Feature

Command-line arguments for stubgen that when enabled will remove unsupported annotations from numpy.ndarray and List
like numpy.ndarray[numpy.float32[3,1]] and List[float[3]] which pybind11 outputs.

FYI: This feature has been implemented for numpy.ndarray in pybind11-stubgen.

Pitch

The stubs generated from extensions made with pybind11 using mypy.stubs are invalid when Eigen types or std::array are used in the interface.

As an example, this output from stubgen,

    def as_numpy(self) -> numpy.ndarray[numpy.float32[3,1]]: ...

will give the following errors when used together with Mypy:

error: Bad number of arguments for type alias, expected: 0, given: 2
error: "ndarray" expects 2 type arguments, but 1 given
error: Invalid type: try using Literal[2] instead?
error: Invalid type: try using Literal[1] instead?

And this

def split(axis_aligned_bounding_box: AxisAlignedBoundingBox, max_extents: List[float[3]]) -> List[AxisAlignedBoundingBox]: ...

will give these errors:

error: "float" expects no type arguments, but 1 given
error: Invalid type: try using Literal[3] instead?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the stubgen command-line entry point and trace how annotations such as numpy.ndarray[...] and List[...] are emitted. Add an option that removes the unsupported annotations, then verify the generated stubs no longer produce the described mypy errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
cli, devtools, tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.