Skip to content

Conversation

@jorenham
Copy link
Member

@jorenham jorenham commented Oct 28, 2025

This converts the numpy.ma.core._frommethod method wrapper utility class to a function, and modifies the wrapped inspect.signature to match that of the wrapper.

Before:

In [2]: np.ma.compress
Out[2]: <numpy.ma.core._frommethod at 0x7559fd926ea0>

In [3]: inspect.signature(np.ma.compress)
Out[3]: <Signature (a, *args, **params)>

After:

In [2]: np.ma.compress
Out[2]: <function numpy.ma.core.compress(condition, a, axis=None, out=None)>

In [3]: inspect.signature(np.ma.compress)
Out[3]: <Signature (condition, a, axis=None, out=None)>

As you can see in the "before" output, the a parameter was incorrectly reported as being the first one, which isn't the case. That's why I've labeled this as a bug.

Before this, it wasn't possible to meaningfully annotate these _frommethod functions, as stubtest did not recognize them as functions, but rather as instances of the _frommethod type. But now that they're actual functions (i.e. instances of FunctionType), we can annotate them like any other function, allowing us to use overloads and such.

Note that there's also a similar situation for np.ma.core._convert2ma. But I figured I'd first start with this one in case there are any objections, and otherwise address _convert2ma in a follow-up.


Note that stubtest will not like this, so I'll now be working on updating the stubs. I'm not planning on touching core.py anymore, so that part can already be reviewed. (I'll have to do a quick scipy-stubs release in the meantime though.)

@jorenham jorenham changed the title BUG: Fix ma.core._frommethod function signatures BUG, TYP: Fix ma.core._frommethod function signatures Oct 29, 2025
@jorenham jorenham marked this pull request as ready for review October 29, 2025 01:28
@charris
Copy link
Member

charris commented Oct 29, 2025

Let's give this a shot. Thanks Joren.

@charris charris merged commit fb55b5b into numpy:main Oct 29, 2025
80 checks passed
@jorenham jorenham deleted the fix-ma.core._frommethod branch October 29, 2025 17:52
@jorenham jorenham added this to the 2.4.0 release milestone Oct 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants