Skip to content

gh-77566: Clarify setdefault() behavior for dbm objects #137911

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

Closed
wants to merge 2 commits into from
Closed
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: 33 additions & 0 deletions Doc/library/dbm.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@
deleted, and the :keyword:`in` operator and the :meth:`!keys` method are
available, as well as :meth:`!get` and :meth:`!setdefault` methods.

.. note::
Unlike the standard :class:`dict` type, the :meth:`dict.setdefault` method
Copy link
Member

Choose a reason for hiding this comment

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

This is self-contradictory. You need to refer to :meth:`!setdefault` instead of :meth:`dict.setdefault` .

Also, I suggest to refer to :term:`mapping` instead of :class:`dict` above.

for DBM objects requires both the *key* and *default* parameters.

Key and values are always stored as :class:`bytes`. This means that when
strings are used they are implicitly converted to the default encoding before
being stored.
Expand Down Expand Up @@ -204,6 +208,14 @@

.. versionadded:: next

.. method:: sqlite3.setdefault(key, default)
Copy link
Member

Choose a reason for hiding this comment

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

No other methods are documented here, except reorganize(). Even close() is barely mentioned.


If *key* is in the database, return its value. If not, insert *key* with

Check warning on line 213 in Doc/library/dbm.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:exc reference target not found: dbm.sqlite3.error [ref.exc]
a value of *default* and return *default*. Unlike :meth:`dict.setdefault`,
both parameters are required. Calling ``setdefault(key)`` with only a key
will raise :exc:`dbm.sqlite3.error`.


:mod:`dbm.gnu` --- GNU database manager
---------------------------------------

Expand Down Expand Up @@ -323,6 +335,13 @@

.. versionadded:: 3.13

.. method:: gdbm.setdefault(key, default)

If *key* is in the database, return its value. If not, insert *key* with
a value of *default* and return *default*. Unlike :meth:`dict.setdefault`,
both parameters are required. Calling ``setdefault(key)`` with only a key
will raise :exc:`TypeError`.


:mod:`dbm.ndbm` --- New Database Manager
----------------------------------------
Expand Down Expand Up @@ -400,6 +419,13 @@

.. versionadded:: 3.13

.. method:: ndbm.setdefault(key, default)

If *key* is in the database, return its value. If not, insert *key* with

Check warning on line 424 in Doc/library/dbm.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:exc reference target not found: _dbm.error [ref.exc]
a value of *default* and return *default*. Unlike :meth:`dict.setdefault`,
both parameters are required. Calling ``setdefault(key)`` with only a key
will raise :exc:`_dbm.error`.


:mod:`dbm.dumb` --- Portable DBM implementation
-----------------------------------------------
Expand Down Expand Up @@ -501,3 +527,10 @@
that this factor changes for each :mod:`dbm` submodule.

.. versionadded:: next

.. method:: dumbdbm.setdefault(key, default)

If *key* is in the database, return its value. If not, insert *key* with
a value of *default* and return *default*. Unlike :meth:`dict.setdefault`,
both parameters are required. Calling ``setdefault(key)`` with only a key
will raise :exc:`TypeError`.
Loading