Skip to content

DOC: Clarify copy parameter semantics in nan_to_num (gh-29045) - #32575

Merged
mattip merged 3 commits into
numpy:mainfrom
Rvish-glitch:fix-nan-to-num-copy-docs
Sep 11, 2026
Merged

mattip merged 3 commits into
numpy:mainfrom
Rvish-glitch:fix-nan-to-num-copy-docs

Conversation

@Rvish-glitch

Copy link
Copy Markdown
Contributor

PR summary

Closes gh-29045

This PR updates the docstring of np.nan_to_num and its Returns section in numpy/lib/_type_check_impl.py to accurately reflect NumPy 2.0+ copy keyword semantics:

  • Updates copy parameter description from bool to bool or None.
  • Clarifies that in NumPy 2.0+, copy=False raises a ValueError if a copy cannot be avoided (such as when passing sequences or when type casting is required), rather than silently copying.
  • Documents that copy=None provides the "copy only if needed" behavior (or in-place if possible).
  • Updates the Returns section to state that x itself may be returned when copy is False or None.

This follows the guidance from @seberg on gh-29045 noting that this was purely a documentation discrepancy with the NumPy 2.0 copy keyword policy.

First time committer introduction

Hi everyone! I am Anush, first-time contributor to NumPy. I use Python and NumPy regularly for my projects and open-source learning. I noticed this documentation discrepancy regarding NumPy 2.0's copy semantics in nan_to_num and wanted to contribute by clarifying it for users.

AI Disclosure

An AI coding assistant (Google Antigravity / Gemini) was used to help locate the issue and draft the initial docstring wording. The changes, runtime behavior (copy=True, copy=False, copy=None), and edge cases were manually tested and verified locally, and this pull request is reviewed and submitted by me.

@jorenham jorenham left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

don't forget to also update the type stubs :)

@overload # np.generic | np.ndarray (`ndarray` subclasses pass through)
def nan_to_num[ScalarOrArrayT: np.generic | np.ndarray](
x: ScalarOrArrayT,
copy: bool = True,
nan: float = 0.0,
posinf: float | None = None,
neginf: float | None = None,
) -> ScalarOrArrayT: ...
@overload # >0-d <known dtype>
def nan_to_num[ScalarT: np.generic](
x: _NestedSequence[_ArrayLike[ScalarT]],
copy: bool = True,
nan: float = 0.0,
posinf: float | None = None,
neginf: float | None = None,
) -> NDArray[ScalarT]: ...
@overload # ?-d <known dtype>
def nan_to_num[DTypeT: np.dtype](
x: _SupportsArray[DTypeT],
copy: bool = True,
nan: float = 0.0,
posinf: float | None = None,
neginf: float | None = None,
) -> np.ndarray[_AnyShape, DTypeT] | Any: ...
@overload # 0-d ~bool
def nan_to_num(
x: bool,
copy: bool = True,
nan: float = 0.0,
posinf: float | None = None,
neginf: float | None = None,
) -> np.bool: ...
@overload # 0-d +int
def nan_to_num(
x: int,
copy: bool = True,
nan: float = 0.0,
posinf: float | None = None,
neginf: float | None = None,
) -> np.int_ | Any: ...
@overload # 0-d +float
def nan_to_num(
x: float,
copy: bool = True,
nan: float = 0.0,
posinf: float | None = None,
neginf: float | None = None,
) -> np.float64 | Any: ...
@overload # 0-d +complex
def nan_to_num(
x: complex,
copy: bool = True,
nan: float = 0.0,
posinf: float | None = None,
neginf: float | None = None,
) -> np.complex128 | Any: ...
@overload # >0-d ~bool
def nan_to_num(
x: _NestedSequence[bool],
copy: bool = True,
nan: float = 0.0,
posinf: float | None = None,
neginf: float | None = None,
) -> NDArray[np.bool]: ...
@overload # >0-d ~int
def nan_to_num(
x: _NestedSequence[list[int]] | list[int],
copy: bool = True,
nan: float = 0.0,
posinf: float | None = None,
neginf: float | None = None,
) -> NDArray[np.int_]: ...
@overload # >0-d ~float
def nan_to_num(
x: _NestedSequence[list[float]] | list[float],
copy: bool = True,
nan: float = 0.0,
posinf: float | None = None,
neginf: float | None = None,
) -> NDArray[np.float64]: ...
@overload # >0-d ~complex
def nan_to_num(
x: _NestedSequence[list[complex]] | list[complex],
copy: bool = True,
nan: float = 0.0,
posinf: float | None = None,
neginf: float | None = None,
) -> NDArray[np.complex128]: ...
@overload # >0-d <unknown dtype>
def nan_to_num(
x: _NestedSequence[ArrayLike],
copy: bool = True,
nan: float = 0.0,
posinf: float | None = None,
neginf: float | None = None,
) -> np.ndarray: ...
@overload # ?-d <unknown dtype>
def nan_to_num(
x: ArrayLike,
copy: bool = True,
nan: float = 0.0,
posinf: float | None = None,
neginf: float | None = None,
) -> np.ndarray | Any: ...

@jorenham jorenham left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

stub changes look good

@Rvish-glitch

Copy link
Copy Markdown
Contributor Author

don't forget to also update the type stubs :)

@overload # np.generic | np.ndarray (`ndarray` subclasses pass through)
def nan_to_num[ScalarOrArrayT: np.generic | np.ndarray](
x: ScalarOrArrayT,
copy: bool = True,
nan: float = 0.0,
posinf: float | None = None,
neginf: float | None = None,
) -> ScalarOrArrayT: ...
@overload # >0-d <known dtype>
def nan_to_num[ScalarT: np.generic](
x: _NestedSequence[_ArrayLike[ScalarT]],
copy: bool = True,
nan: float = 0.0,
posinf: float | None = None,
neginf: float | None = None,
) -> NDArray[ScalarT]: ...
@overload # ?-d <known dtype>
def nan_to_num[DTypeT: np.dtype](
x: _SupportsArray[DTypeT],
copy: bool = True,
nan: float = 0.0,
posinf: float | None = None,
neginf: float | None = None,
) -> np.ndarray[_AnyShape, DTypeT] | Any: ...
@overload # 0-d ~bool
def nan_to_num(
x: bool,
copy: bool = True,
nan: float = 0.0,
posinf: float | None = None,
neginf: float | None = None,
) -> np.bool: ...
@overload # 0-d +int
def nan_to_num(
x: int,
copy: bool = True,
nan: float = 0.0,
posinf: float | None = None,
neginf: float | None = None,
) -> np.int_ | Any: ...
@overload # 0-d +float
def nan_to_num(
x: float,
copy: bool = True,
nan: float = 0.0,
posinf: float | None = None,
neginf: float | None = None,
) -> np.float64 | Any: ...
@overload # 0-d +complex
def nan_to_num(
x: complex,
copy: bool = True,
nan: float = 0.0,
posinf: float | None = None,
neginf: float | None = None,
) -> np.complex128 | Any: ...
@overload # >0-d ~bool
def nan_to_num(
x: _NestedSequence[bool],
copy: bool = True,
nan: float = 0.0,
posinf: float | None = None,
neginf: float | None = None,
) -> NDArray[np.bool]: ...
@overload # >0-d ~int
def nan_to_num(
x: _NestedSequence[list[int]] | list[int],
copy: bool = True,
nan: float = 0.0,
posinf: float | None = None,
neginf: float | None = None,
) -> NDArray[np.int_]: ...
@overload # >0-d ~float
def nan_to_num(
x: _NestedSequence[list[float]] | list[float],
copy: bool = True,
nan: float = 0.0,
posinf: float | None = None,
neginf: float | None = None,
) -> NDArray[np.float64]: ...
@overload # >0-d ~complex
def nan_to_num(
x: _NestedSequence[list[complex]] | list[complex],
copy: bool = True,
nan: float = 0.0,
posinf: float | None = None,
neginf: float | None = None,
) -> NDArray[np.complex128]: ...
@overload # >0-d <unknown dtype>
def nan_to_num(
x: _NestedSequence[ArrayLike],
copy: bool = True,
nan: float = 0.0,
posinf: float | None = None,
neginf: float | None = None,
) -> np.ndarray: ...
@overload # ?-d <unknown dtype>
def nan_to_num(
x: ArrayLike,
copy: bool = True,
nan: float = 0.0,
posinf: float | None = None,
neginf: float | None = None,
) -> np.ndarray | Any: ...

stub changes look good

Done! Updated all nan_to_num overloads in numpy/lib/_type_check_impl.pyi to copy: bool | None = True.

Thanks for pointing that out!

Comment on lines -393 to -394
in-place (False). The in-place operation only occurs if
casting to an array does not require a copy.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You shouldn't remove this sentence you deleted

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, thanks! I've restored the sentence and pushed an update in commit 623baa3.

@ngoldbaum ngoldbaum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you!

@mattip
mattip merged commit 8bc90ab into numpy:main Sep 11, 2026
93 checks passed
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.

DOC: np.nan_to_num(..., copy=) uses NumPy 1.x description

4 participants