Skip to content

DOC: prefer passing scalar types to dtype= in examples#30394

Merged
charris merged 2 commits intonumpy:mainfrom
jorenham:consistent-dtype-docs
Dec 8, 2025
Merged

DOC: prefer passing scalar types to dtype= in examples#30394
charris merged 2 commits intonumpy:mainfrom
jorenham:consistent-dtype-docs

Conversation

@jorenham
Copy link
Copy Markdown
Member

@jorenham jorenham commented Dec 8, 2025

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.

@jorenham jorenham changed the title DOC: prefer scalar types for dtype= DOC: prefer passing scalar types to dtype= in examples Dec 8, 2025
@jorenham jorenham requested a review from Copilot December 8, 2025 17:05
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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=float with dtype=np.float64 across examples
  • Replace dtype=int with dtype=np.int_ across examples
  • Replace dtype=complex with dtype=np.complex128 across examples
  • Replace string-based dtypes like dtype='f4' with dtype=np.float32
  • Replace dtype=object with dtype=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.

Co-authored-by: Charles Harris <charlesr.harris@gmail.com>
@charris charris merged commit cee14ff into numpy:main Dec 8, 2025
71 checks passed
@charris
Copy link
Copy Markdown
Member

charris commented Dec 8, 2025

Thanks Joren.

@jorenham jorenham deleted the consistent-dtype-docs branch December 8, 2025 20:43
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.

3 participants