Skip to content

Conversation

@jorenham
Copy link
Member

@jorenham jorenham commented Nov 4, 2025

Following #30104, #30114, #30121, #30124, #30126, #30137, and #30138; this adds the missing __text_signature__s to empty, zeros, and updates the relevant stubs.
Note that because ones and full are python functions, they did not have this issue.

This also adds docs for the device parameter of zeros, which apparently was missing.


Before:

>>> import numpy as np
>>> from inspect import signature
>>> signature(np.empty)
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    signature(np.empty)
    ~~~~~~~~~^^^^^^^^^^
  File "/home/joren/.local/share/uv/python/cpython-3.14-linux-x86_64-gnu/lib/python3.14/inspect.py", line 3312, in signature
    return Signature.from_callable(obj, follow_wrapped=follow_wrapped,
           ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                                   globals=globals, locals=locals, eval_str=eval_str,
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                                   annotation_format=annotation_format)
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/joren/.local/share/uv/python/cpython-3.14-linux-x86_64-gnu/lib/python3.14/inspect.py", line 3027, in from_callable
    return _signature_from_callable(obj, sigcls=cls,
                                    follow_wrapper_chains=follow_wrapped,
                                    globals=globals, locals=locals, eval_str=eval_str,
                                    annotation_format=annotation_format)
  File "/home/joren/.local/share/uv/python/cpython-3.14-linux-x86_64-gnu/lib/python3.14/inspect.py", line 2508, in _signature_from_callable
    return _signature_from_builtin(sigcls, obj,
                                   skip_bound_arg=skip_bound_arg)
  File "/home/joren/.local/share/uv/python/cpython-3.14-linux-x86_64-gnu/lib/python3.14/inspect.py", line 2292, in _signature_from_builtin
    raise ValueError("no signature found for builtin {!r}".format(func))
ValueError: no signature found for builtin <built-in function empty>

After:

>>> import numpy as np
>>> from inspect import signature
>>> signature(np.empty)
<Signature (shape, dtype=None, order='C', *, device=None, like=None)>
>>> signature(np.zeros)
<Signature (shape, dtype=None, order='C', *, device=None, like=None)>

@jorenham jorenham added this to the 2.4.0 release milestone Nov 4, 2025
@jorenham jorenham changed the title BUG, TYP: empty and zeros runtime signatures BUG,DOC, TYP: empty and zeros runtime signatures Nov 4, 2025
@jorenham jorenham changed the title BUG,DOC, TYP: empty and zeros runtime signatures BUG,DOC, TYP: empty and zeros runtime signatures, and missing device parameter docs Nov 4, 2025
@jorenham jorenham changed the title BUG,DOC, TYP: empty and zeros runtime signatures, and missing device parameter docs BUG, DOC, TYP: empty and zeros runtime signatures, and missing device parameter docs Nov 4, 2025
@github-actions
Copy link

github-actions bot commented Nov 4, 2025

Diff from mypy_primer, showing the effect of this PR on type check results on a corpus of open source code:

xarray (https://github.com/pydata/xarray)
+ xarray/tests/test_namedarray.py: note: In member "test_from_array_with_0d_object" of class "TestNamedArray":
+ xarray/tests/test_namedarray.py:249: error: Need type annotation for "narr"  [var-annotated]

@jorenham
Copy link
Member Author

jorenham commented Nov 4, 2025

the primer error is a bit weird, but it's not an indiciation of a regression or something. Basically "need type annotations" means that mypy isn't able to infer the type itself, but in many cases where it also can't infer the type, mypy doesn't report this error.
So this diff doesn't really tell us much.

https://github.com/pydata/xarray/blob/ea35e64cec1a72e4c3270f60c7a937e59a36f0ce/xarray/tests/test_namedarray.py#L246-L250

add_newdoc('numpy._core.multiarray', 'empty',
"""
empty(shape, dtype=float, order='C', *, device=None, like=None)
empty(shape, dtype=None, order='C', *, device=None, like=None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, python does pass None here, but that defaults to float64. I guess this is more accurate.

@charris charris merged commit 3b44dc0 into numpy:main Nov 4, 2025
79 checks passed
@charris
Copy link
Member

charris commented Nov 4, 2025

Thanks Joren.

@jorenham jorenham deleted the fix-empty-zeros-signatures branch November 4, 2025 16:41
charris pushed a commit that referenced this pull request Nov 6, 2025
This fixes `inspect.signature` for

- `np.random.BitGenerator`
- `np.random.Generator`
- `np.random.MT19937`
- `np.random.PCG64`
- `np.random.PCG64DXSM`
- `np.random.Philox`
- `np.random.RandomState`
- `np.random.SFC64`
- `np.random.SeedSequence`
- `np.random.bit_generator.SeedlessSeedSequence`

This also fixes a typo in `bit_generator.pxd` that accidentally defined an empty unused class `np.random.bit_generator.SeedlessSequence`.

Related to #30104, #30114, #30121, #30124, #30126, #30137, #30138, #30140, #30143, #30146, #30147, and #30155
cakedev0 pushed a commit to cakedev0/numpy that referenced this pull request Dec 5, 2025
…g ``device`` parameter docs (numpy#30140)

* BUG: ``empty`` and ``zeros`` runtime signatures

* TYP: `empty`, `zeros`, and `ones` parameter defaults and minor shape-type fix

* TST: Update `_convert2ma` test signature for `np.ma.empty`
cakedev0 pushed a commit to cakedev0/numpy that referenced this pull request Dec 5, 2025
This fixes `inspect.signature` for

- `np.random.BitGenerator`
- `np.random.Generator`
- `np.random.MT19937`
- `np.random.PCG64`
- `np.random.PCG64DXSM`
- `np.random.Philox`
- `np.random.RandomState`
- `np.random.SFC64`
- `np.random.SeedSequence`
- `np.random.bit_generator.SeedlessSeedSequence`

This also fixes a typo in `bit_generator.pxd` that accidentally defined an empty unused class `np.random.bit_generator.SeedlessSequence`.

Related to numpy#30104, numpy#30114, numpy#30121, numpy#30124, numpy#30126, numpy#30137, numpy#30138, numpy#30140, numpy#30143, numpy#30146, numpy#30147, and numpy#30155
IndifferentArea pushed a commit to IndifferentArea/numpy that referenced this pull request Dec 7, 2025
…g ``device`` parameter docs (numpy#30140)

* BUG: ``empty`` and ``zeros`` runtime signatures

* TYP: `empty`, `zeros`, and `ones` parameter defaults and minor shape-type fix

* TST: Update `_convert2ma` test signature for `np.ma.empty`
IndifferentArea pushed a commit to IndifferentArea/numpy that referenced this pull request Dec 7, 2025
This fixes `inspect.signature` for

- `np.random.BitGenerator`
- `np.random.Generator`
- `np.random.MT19937`
- `np.random.PCG64`
- `np.random.PCG64DXSM`
- `np.random.Philox`
- `np.random.RandomState`
- `np.random.SFC64`
- `np.random.SeedSequence`
- `np.random.bit_generator.SeedlessSeedSequence`

This also fixes a typo in `bit_generator.pxd` that accidentally defined an empty unused class `np.random.bit_generator.SeedlessSequence`.

Related to numpy#30104, numpy#30114, numpy#30121, numpy#30124, numpy#30126, numpy#30137, numpy#30138, numpy#30140, numpy#30143, numpy#30146, numpy#30147, and numpy#30155
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