Skip to content

Commit 31985e2

Browse files
committed
gh-137112: Improve str() docstring defaults and wording
1 parent 8b669d5 commit 31985e2

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

Lib/test/test_pydoc/test_pydoc.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,17 @@ def run_pydoc_pager(request, what, expected_first_line):
801801
run_pydoc_pager('sys', 'sys', 'Help on built-in module sys:')
802802
run_pydoc_pager(sys, 'sys', 'Help on built-in module sys:')
803803

804+
@requires_docstrings
805+
def test_str_docstring_signature(self):
806+
doc = pydoc.TextDoc()
807+
text = clean_text(doc.docclass(str))
808+
self.assertIn(" | str(object='') -> str", text)
809+
self.assertIn(" | str(object=b'', encoding='utf-8', errors='strict') -> str", text)
810+
self.assertNotIn("str(bytes_or_buffer", text)
811+
self.assertIn("bytes-like object", text)
812+
self.assertNotIn("encoding defaults", text)
813+
self.assertNotIn("errors defaults", text)
814+
804815
def test_showtopic(self):
805816
with captured_stdout() as showtopic_io:
806817
helper = pydoc.Helper()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Updated ``str()`` help text to show the default ``encoding`` and ``errors``
2+
values and to describe decoding input as a bytes-like object, matching the
3+
documentation signature.

Objects/unicodeobject.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13968,15 +13968,13 @@ _PyUnicode_ExactDealloc(PyObject *op)
1396813968

1396913969
PyDoc_STRVAR(unicode_doc,
1397013970
"str(object='') -> str\n\
13971-
str(bytes_or_buffer[, encoding[, errors]]) -> str\n\
13971+
str(object=b'', encoding='utf-8', errors='strict') -> str\n\
1397213972
\n\
1397313973
Create a new string object from the given object. If encoding or\n\
13974-
errors is specified, then the object must expose a data buffer\n\
13974+
errors is specified, then the object must be a bytes-like object\n\
1397513975
that will be decoded using the given encoding and error handler.\n\
1397613976
Otherwise, returns the result of object.__str__() (if defined)\n\
13977-
or repr(object).\n\
13978-
encoding defaults to 'utf-8'.\n\
13979-
errors defaults to 'strict'.");
13977+
or repr(object).");
1398013978

1398113979
static PyObject *unicode_iter(PyObject *seq);
1398213980

0 commit comments

Comments
 (0)