wjakob / nanobind

nanobind: tiny and efficient C++/Python bindings
BSD 3-Clause "New" or "Revised" License
2.14k stars 161 forks source link

[BUG]: Stubgen produces mismatched brackets when parsing numpy annotation #625

Open WangYuNeng opened 6 days ago

WangYuNeng commented 6 days ago

Problem description

I have a annotation with numpy.ndarray[[('value', '<f8'), ('value', '<f8')]] and the stub generation will produce annotation as Annotated[ArrayLike, dict([('time', '<f8'), ('value', '<f8'))]] a bracket misplaced

I suspect the root cause is the nested brackets. Here is the matching rule. https://github.com/wjakob/nanobind/blob/bd6256570235f38a2c7a2193f2469cde00c7bca5/src/stubgen.py#L253-L256

Line 634: sub will look for the leftmost match so s=[('value', '<f8'), ('value', '<f8') in the beginning and the return value is Annotated[ArrayLike, dict([('time', '<f8'), ('value', '<f8'))] (which misses a right bracket), and because the rightmost bracket is not substituted, the resulting annotation becomes Annotated[ArrayLike, dict([('time', '<f8'), ('value', '<f8'))]] with an extra outmost right bracket. https://github.com/wjakob/nanobind/blob/bd6256570235f38a2c7a2193f2469cde00c7bca5/src/stubgen.py#L620-L634

Reproducible example code

No response