DOC: prefer passing scalar types to dtype= in examples#30394
Merged
charris merged 2 commits intonumpy:mainfrom Dec 8, 2025
Merged
DOC: prefer passing scalar types to dtype= in examples#30394charris merged 2 commits intonumpy:mainfrom
dtype= in examples#30394charris merged 2 commits intonumpy:mainfrom
Conversation
dtype=dtype= in examples
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates NumPy documentation and examples to prefer scalar type objects (e.g., np.float64, np.int_, np.complex128) over string or builtin type names (e.g., 'float', float, int, 'complex') in dtype= arguments. This change improves consistency across the codebase and enables better static type analysis, as scalar types provide more explicit dtype information that type checkers can verify.
Key changes:
- Replace
dtype=floatwithdtype=np.float64across examples - Replace
dtype=intwithdtype=np.int_across examples - Replace
dtype=complexwithdtype=np.complex128across examples - Replace string-based dtypes like
dtype='f4'withdtype=np.float32 - Replace
dtype=objectwithdtype=np.object_in documentation
Reviewed changes
Copilot reviewed 40 out of 40 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| numpy/random/mtrand.pyx | Updated documentation to clarify default dtype is intp |
| numpy/random/_generator.pyx | Changed dtype=float to dtype=np.float64 in normalization example |
| numpy/matrixlib/defmatrix.py | Changed dtype='float' to dtype=np.float64 in matrix sum examples |
| numpy/matlib.py | Changed dtype=int to dtype=np.int_ and dtype=float to dtype=np.float64 |
| numpy/ma/extras.py | Changed dtype=int to dtype=np.int_ in masked array examples |
| numpy/ma/core.py | Changed dtype=object to dtype=np.object_ and dtype=float to dtype=np.float64 |
| numpy/linalg/_linalg.py | Changed dtype=object to dtype=np.object_ and dtype=complex to dtype=np.complex128 |
| numpy/lib/_type_check_impl.py | Changed string dtypes to scalar types (e.g., "U" to np.str_, dtype=complex to np.complex128) |
| numpy/lib/_twodim_base_impl.py | Changed dtype=int to dtype=np.int_ in eye() and tri() examples |
| numpy/lib/_npyio_impl.py | Changed dtype="U" to dtype=np.str_ in loadtxt example |
| numpy/lib/_index_tricks_impl.py | Changed dtype=int to dtype=np.int_ in diag_indices example |
| numpy/lib/_function_base_impl.py | Changed dtype=float to dtype=np.float64, dtype=int to dtype=np.int_, and dtype=bool to dtype=np.bool (incorrect - should be np.bool_) |
| numpy/lib/_array_utils_impl.py | Changed dtype='f' to dtype=np.float32 in byte_bounds example |
| numpy/fft/_pocketfft.py | Changed dtype=complex to dtype=np.complex128 in ifft and ifftn examples |
| numpy/fft/_helper.py | Changed dtype=float to dtype=np.float64 in fftfreq and rfftfreq examples |
| numpy/doc/ufuncs.py | Changed dtype=float to dtype=np.float64 in ufunc examples |
| numpy/ctypeslib/_ctypeslib.py | Changed np.dtype('int8') to np.dtype(np.int8) |
| numpy/_core/numerictypes.py | Changed np.dtype('str') to np.dtype(np.str_) |
| numpy/_core/numeric.py | Changed dtype=float to dtype=np.float64, dtype=int to dtype=np.int_, dtype=object to np.object_ |
| numpy/_core/multiarray.py | Changed string-based dtype specifications to scalar types |
| numpy/_core/memmap.py | Changed dtype='float32' to dtype=np.float32 throughout examples |
| numpy/_core/function_base.py | Changed dtype=int to dtype=np.int_ in linspace and geomspace examples |
| numpy/_core/fromnumeric.py | Changed dtype=bool to dtype=np.bool (incorrect - should be np.bool_), dtype=float to dtype=np.float64 |
| numpy/_core/code_generators/ufunc_docstrings.py | Changed dtype=float to dtype=np.float64, dtype=complex to dtype=np.complex128, dtype=object to dtype=np.object_ |
| numpy/_core/_add_newdocs.py | Changed string-based dtypes to scalar types throughout |
| doc/source/user/quickstart.rst | Changed dtype=complex to dtype=np.complex128, dtype=int to dtype=np.int_ |
| doc/source/user/c-info.ufunc-tutorial.rst | Changed dtype="f4" to dtype=np.float32 |
| doc/source/user/basics.ufuncs.rst | Changed dtype=float to dtype=np.float64, dtype=int to dtype=np.int_ |
| doc/source/user/basics.subclassing.rst | Changed dtype=float to dtype=np.float64, removed trailing whitespace |
| doc/source/user/basics.io.genfromtxt.rst | Changed dtype=float to dtype=np.float64, dtype=int to dtype=np.int_ |
| doc/source/user/basics.creation.rst | Changed dtype=float to dtype=np.float64, removed trailing whitespace |
| doc/source/user/absolute_beginners.rst | Changed dtype=int to dtype=np.int_ |
| doc/source/reference/thread_safety.rst | Changed dtype=object to dtype=np.object_ |
| doc/source/reference/c-api/array.rst | Changed dtype=object to dtype=np.object_ |
| doc/source/reference/arrays.promotion.rst | Changed string-based dtypes to scalar types |
| doc/source/reference/arrays.dtypes.rst | Changed dtype=float to dtype=np.float64 |
| doc/source/reference/arrays.datetime.rst | Changed dtype='datetime64' to dtype=np.datetime64 |
| doc/source/reference/arrays.classes.rst | Changed dtype=float to dtype=np.float64 |
| doc/neps/nep-0055-string_dtype.rst | Changed dtype=object to dtype=np.object_ |
| doc/neps/nep-0050-scalar-promotion.rst | Changed dtype=object to dtype=np.object_ |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
charris
reviewed
Dec 8, 2025
Co-authored-by: Charles Harris <charlesr.harris@gmail.com>
Member
|
Thanks Joren. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Besides the obvious advantages of consistent docs, passing scalar-types as
dtype=is beneficial for static typing purposes, because in case of strings or builtin types the dtype can usually not be statically determined.