Skip to content

gh-109961: Docs: Fix incorrect rendering of __replace__ in copy.rst #109968

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions Doc/library/copy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,27 +88,36 @@ pickle functions from the :mod:`copyreg` module.
single: __deepcopy__() (copy protocol)

In order for a class to define its own copy implementation, it can define
special methods :meth:`__copy__` and :meth:`__deepcopy__`. The former is called
to implement the shallow copy operation; no additional arguments are passed.
The latter is called to implement the deep copy operation; it is passed one
argument, the ``memo`` dictionary. If the :meth:`__deepcopy__` implementation needs
to make a deep copy of a component, it should call the :func:`deepcopy` function
with the component as first argument and the memo dictionary as second argument.
The memo dictionary should be treated as an opaque object.
special methods :meth:`~object.__copy__` and :meth:`~object.__deepcopy__`.

.. method:: object.__copy__(self)
Copy link
Member

Choose a reason for hiding this comment

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

But please keep :noindex:. There are already entries for __replace__ etc, and duplicates that refer to the same target are confusing.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hm, when adding noindex it starts to fail nitpick mode:

/Users/sobolev/Desktop/cpython/Doc/library/copy.rst:90: WARNING: py:meth reference target not found: object.__copy__
/Users/sobolev/Desktop/cpython/Doc/library/copy.rst:90: WARNING: py:meth reference target not found: object.__deepcopy__
/Users/sobolev/Desktop/cpython/Doc/library/copy.rst:112: WARNING: py:meth reference target not found: object.__replace__

Copy link
Member Author

@sobolevn sobolevn Sep 27, 2023

Choose a reason for hiding this comment

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

With two indexes we have:
Снимок экрана 2023-09-27 в 17 55 22

They link to:

  • copy.html#copy.object.__replace__
  • copy.html#index-2

Do we need the index directive itself now?

Copy link
Member

Choose a reason for hiding this comment

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

You could consider :noindexentry:?

A

Copy link
Member Author

Choose a reason for hiding this comment

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

It generated just one link in index.html: copy.html#index-2
No warnings 👍

Thanks, TIL about noindexentry :)

:noindexentry:

Called to implement the shallow copy operation;
no additional arguments are passed.

.. method:: object.__deepcopy__(self, memo)
:noindexentry:

Called to implement the deep copy operation; it is passed one
argument, the *memo* dictionary. If the ``__deepcopy__`` implementation needs
to make a deep copy of a component, it should call the :func:`deepcopy` function
with the component as first argument and the *memo* dictionary as second argument.
The *memo* dictionary should be treated as an opaque object.


.. index::
single: __replace__() (replace protocol)

Function :func:`replace` is more limited than :func:`copy` and :func:`deepcopy`,
and only supports named tuples created by :func:`~collections.namedtuple`,
:mod:`dataclasses`, and other classes which define method :meth:`!__replace__`.
:mod:`dataclasses`, and other classes which define method :meth:`~object.__replace__`.
Copy link
Member

Choose a reason for hiding this comment

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

This answers a question I had: with noindexentry, can we still link to the method.
I just hope it’s not indexed as copy.object instead of builtins.object!

Copy link
Member

Choose a reason for hiding this comment

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

Of course, it is copy.object. https://cpython-previews--109968.org.readthedocs.build/en/109968/library/copy.html#copy.object.__replace__

You need to add .. currentmodule:: . to make it builtin.


.. method:: __replace__(self, /, **changes)
:noindex:
.. method:: object.__replace__(self, /, **changes)
:noindexentry:

:meth:`!__replace__` should create a new object of the same type,
replacing fields with values from *changes*.
This method should create a new object of the same type,
replacing fields with values from *changes*.


.. seealso::
Expand Down
1 change: 0 additions & 1 deletion Doc/tools/.nitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ Doc/library/collections.rst
Doc/library/concurrent.futures.rst
Doc/library/configparser.rst
Doc/library/contextlib.rst
Doc/library/copy.rst
Doc/library/csv.rst
Doc/library/datetime.rst
Doc/library/dbm.rst
Expand Down