8000 gh-81682: Fix test failures when CPython is built without docstrings by serhiy-storchaka · Pull Request #113410 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-81682: Fix test failures when CPython is built without docstrings #113410

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
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
6 ch 8000 anges: 6 additions & 0 deletions Lib/idlelib/idle_test/test_calltip.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import types
import re
from idlelib.idle_test.mock_tk import Text
from test.support import MISSING_C_DOCSTRINGS


# Test Class TC is used in multiple get_argspec test methods
Expand Down Expand Up @@ -50,6 +51,8 @@ class Get_argspecTest(unittest.TestCase):
# but a red buildbot is better than a user crash (as has happened).
# For a simple mismatch, change the expected output to the actual.

@unittest.skipIf(MISSING_C_DOCSTRINGS,
"Signature information for builtins requires docstrings")
def test_builtins(self):

def tiptest(obj, out):
Expand Down Expand Up @@ -143,6 +146,8 @@ def f(): pass
f.__doc__ = 'a'*300
self.assertEqual(get_spec(f), f"()\n{'a'*(calltip._MAX_COLS-3) + '...'}")

@unittest.skipIf(MISSING_C_DOCSTRINGS,
"Signature information for builtins requires docstrings")
def test_multiline_docstring(self):
# Test fewer lines than max.
self.assertEqual(get_spec 8000 (range),
Expand All @@ -157,6 +162,7 @@ def test_multiline_docstring(self):
bytes(int) -> bytes object of size given by the parameter initialized with null bytes
bytes() -> empty bytes object''')

def test_multiline_docstring_2(self):
# Test more than max lines
def f(): pass
f.__doc__ = 'a\n' * 15
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_capi/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,8 @@ def __del__(self):
del L
self.assertEqual(PyList.num, 0)

@unittest.skipIf(MISSING_C_DOCSTRINGS,
"Signature information for builtins requires docstrings")
def test_heap_ctype_doc_and_text_signature(self):
self.assertEqual(_testcapi.HeapDocCType.__doc__, "somedoc")
self.assertEqual(_testcapi.HeapDocCType.__text_signature__, "(arg1, arg2)")
Expand Down
11 changes: 6 additions & 5 deletions Lib/test/test_coroutines.py
Original file line number Diff line number Diff line change
Expand Up @@ -953,11 +953,12 @@ async def b():

def test_corotype_1(self):
ct = types.CoroutineType
self.assertIn('into coroutine', ct.send.__doc__)
self.assertIn('inside coroutine', ct.close.__doc__)
self.assertIn('in coroutine', ct.throw.__doc__)
self.assertIn('of the coroutine', ct.__dict__['__name__'].__doc__)
self.assertIn('of the coroutine', ct.__dict__['__qualname__'].__doc__)
if not support.MISSING_C_DOCSTRINGS:
self.assertIn('into coroutine', ct.send.__doc__)
self.assertIn('inside coroutine', ct.close.__doc__)
self.assertIn('in coroutine', ct.throw.__doc__)
self.assertIn('of the coroutine', ct.__dict__['__name__'].__doc__)
self.assertIn('of the coroutine', ct.__dict__['__qualname__'].__doc__)
Copy link
Member

Choose a reason for hiding this comment

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

Not for this PR to change, but this seems a rather silly test -- it tests that the methods have certain text in their docstrings. I'm curious why that needs to be a test...

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 was added in 5376ba9 (bpo-24400/#68588) which introduced a distinct type for 'async def' coroutines. I guess it was the simplest way to check that these methods and data descriptors belong to the coroutine type and not inherited from the generator type. It could check the __objclass__ attribute instead.

I am planning to add signatures for all builtins, so docstrings of coroutine methods will change in the nearest future (they may already be changed in my branch).

self.assertEqual(ct.__name__, 'coroutine')

async def f(): pass
Expand Down
4 changes: 3 additions & 1 deletion Lib/test/test_curses.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from unittest.mock import MagicMock

from test.support import (requires, verbose, SaveSignals, cpython_only,
check_disallow_instantiation)
check_disallow_instantiation, MISSING_C_DOCSTRINGS)
from test.support.import_helper import import_module

# Optionally test curses module. This currently requires that the
Expand Down Expand Up @@ -1142,6 +1142,8 @@ def test_encoding(self):
with self.assertRaises(TypeError):
del stdscr.encoding

@unittest.skipIf(MISSING_C_DOCSTRINGS,
"Signature information for builtins requires docstrings")
def test_issue21088(self):
stdscr = self.stdscr
#
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,8 @@ def mycmp(x, y):
self.assertRaises(TypeError, hash, k)
self.assertNotIsInstance(k, collections.abc.Hashable)

@unittest.skipIf(support.MISSING_C_DOCSTRINGS,
"Signature information for builtins requires docstrings")
def test_cmp_to_signature(self):
self.assertEqual(str(Signature.from_callable(self.cmp_to_key)),
'(mycmp)')
Expand Down
4 changes: 3 additions & 1 deletion Lib/test/test_importlib/extension/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import warnings
import importlib.util
import importlib
from test.support import MISSING_C_DOCSTRINGS


class LoaderTests:
Expand Down Expand Up @@ -373,7 +374,8 @@ def test_nonascii(self):
with self.subTest(name):
module = self.load_module_by_name(name)
self.assertEqual(module.__name__, name)
self.assertEqual(module.__doc__, "Module named in %s" % lang)
if not MISSING_C_DOCSTRINGS:
self.assertEqual(module.__doc__, "Module named in %s" % lang)


(Frozen_MultiPhaseExtensionModuleTests,
Expand Down
5 changes: 4 additions & 1 deletion Lib/test/test_inspect/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -3990,6 +3990,8 @@ def foo(a, *, b:1): pass
foo_sig = MySignature.from_callable(foo)
self.assertIsInstance(foo_sig, MySignature)

@unittest.skipIf(MISSING_C_DOCSTRINGS,
"Signature information for builtins requires docstrings")
def test_signature_from_callable_class(self):
# A regression test for a class inheriting its signature from `object`.
class MySignature(inspect.Signature): pass
Expand Down Expand Up @@ -4080,7 +4082,8 @@ def test_signature_eval_str(self):
par('c', PORK, annotation="'MyClass'"),
)))

self.assertEqual(signature_func(isa.UnannotatedClass), sig())
if not MISSING_C_DOCSTRINGS:
self.assertEqual(signature_func(isa.UnannotatedClass), sig())
self.assertEqual(signature_func(isa.unannotated_function),
sig(
parameters=(
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_module/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_uninitialized(self):
self.fail("__name__ = %s" % repr(s))
except AttributeError:
pass
self.assertEqual(foo.__doc__, ModuleType.__doc__)
self.assertEqual(foo.__doc__, ModuleType.__doc__ or '')

def test_uninitialized_missing_getattr(self):
# Issue 8297
Expand Down
28 changes: 19 additions & 9 deletions Lib/test/test_pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from test.support import threading_helper
from test.support import (reap_children, captured_output, captured_stdout,
captured_stderr, is_emscripten, is_wasi,
requires_docstrings)
requires_docstrings, MISSING_C_DOCSTRINGS)
from test.support.os_helper import (TESTFN, rmtree, unlink)
from test import pydoc_mod

Expand Down Expand Up @@ -906,12 +906,13 @@ class A(builtins.object)
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables
| __dict__%s
|
| __weakref__
| list of weak references to the object
''' % __name__)
| __weakref__%s
''' % (__name__,
'' if MISSING_C_DOCSTRINGS else '\n | dictionary for instance variables',
'' if MISSING_C_DOCSTRINGS else '\n | list of weak references to the object',
))

def func(
arg1: Callable[[Annotated[int, 'Some doc']], str],
Expand Down Expand Up @@ -1154,13 +1155,15 @@ def test_generic_alias(self):
doc = pydoc.render_doc(typing.List[int], renderer=pydoc.plaintext)
self.assertIn('_GenericAlias in module typing', doc)
self.assertIn('List = class list(object)', doc)
self.assertIn(list.__doc__.strip().splitlines()[0], doc)
if not MISSING_C_DOCSTRINGS:
self.assertIn(list.__doc__.strip().splitlines()[0], doc)

self.assertEqual(pydoc.describe(list[int]), 'GenericAlias')
doc = pydoc.render_doc(list[int], renderer=pydoc.plaintext)
self.assertIn('GenericAlias in module builtins', doc)
self.assertIn('\nclass list(object)', doc)
self.assertIn(list.__doc__.strip().splitlines()[0], doc)
if not MISSING_C_DOCSTRINGS:
self.assertIn(list.__doc__.strip().splitlines()[0], doc)

def test_union_type(self):
self.assertEqual(pydoc.describe(typing.Union[int, str]), '_UnionGenericAlias')
Expand All @@ -1174,7 +1177,8 @@ def test_union_type(self):
doc = pydoc.render_doc(int | str, renderer=pydoc.plaintext)
self.assertIn('UnionType in module types object', doc)
self.assertIn('\nclass UnionType(builtins.object)', doc)
self.assertIn(types.UnionType.__doc__.strip().splitlines()[0], doc)
if not MISSING_C_DOCSTRINGS:
self.assertIn(types.UnionType.__doc__.strip().splitlines()[0], doc)

def test_special_form(self):
self.assertEqual(pydoc.describe(typing.NoReturn), '_SpecialForm')
Expand Down Expand Up @@ -1327,13 +1331,15 @@ def test_bound_builtin_classmethod_o(self):
"__class_getitem__(object, /) method of builtins.type instance")

@support.cpython_only
@requires_docstrings
def test_module_level_callable_unrepresentable_default(self):
import _testcapi
builtin = _testcapi.func_with_unrepresentable_signature
self.assertEqual(self._get_summary_line(builtin),
"func_with_unrepresentable_signature(a, b=<x>)")

@support.cpython_only
@requires_docstrings
def test_builtin_staticmethod_unrepresentable_default(self):
self.assertEqual(self._get_summary_line(str.maketrans),
"maketrans(x, y=<unrepresentable>, z=<unrepresentable>, /)")
Expand All @@ -1343,6 +1349,7 @@ def test_builtin_staticmethod_unrepresentable_default(self):
"staticmeth(a, b=<x>)")

@support.cpython_only
@requires_docstrings
def test_unbound_builtin_method_unrepresentable_default(self):
self.assertEqual(self._get_summary_line(dict.pop),
"pop(self, key, default=<unrepresentable>, /)")
Expand All @@ -1352,6 +1359,7 @@ def test_unbound_builtin_method_unrepresentable_default(self):
"meth(self, /, a, b=<x>)")

@support.cpython_only
@requires_docstrings
def test_bound_builtin_method_unrepresentable_default(self):
self.assertEqual(self._get_summary_line({}.pop),
"pop(key, default=<unrepresentable>, /) "
Expand All @@ -1363,6 +1371,7 @@ def test_bound_builtin_method_unrepresentable_default(self):
"method of _testcapi.DocStringUnrepresentableSignatureTest instance")

@support.cpython_only
@requires_docstrings
def test_unbound_builtin_classmethod_unrepresentable_default(self):
import _testcapi
cls = _testcapi.DocStringUnrepresentableSignatureTest
Expand All @@ -1371,6 +1380,7 @@ def test_unbound_builtin_classmethod_unrepresentable_default(self):
"classmeth(type, /, a, b=<x>)")

@support.cpython_only
@requires_docstrings
def test_bound_builtin_classmethod_unrepresentable_default(self):
import _testcapi
cls = _testcapi.DocStringUnrepresentableSignatureTest
Expand Down
5 changes: 3 additions & 2 deletions Lib/test/test_rlcompleter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from unittest.mock import patch
import builtins
import rlcompleter
from test.support import MISSING_C_DOCSTRINGS

class CompleteMe:
""" Trivial class used in testing rlcompleter.Completer. """
Expand Down Expand Up @@ -40,12 +41,12 @@ def test_global_matches(self):

# test with a customized namespace
self.assertEqual(self.completer.global_matches('CompleteM'),
['CompleteMe()'])
['CompleteMe(' if MISSING_C_DOCSTRINGS else 'CompleteMe()'])
self.assertEqual(self.completer.global_matches('eg'),
['egg('])
# XXX: see issue5256
self.assertEqual(self.completer.global_matches('CompleteM'),
['CompleteMe()'])
['CompleteMe(' if MISSING_C_DOCSTRINGS else 'CompleteMe()'])

def test_attr_matches(self):
# test with builtins namespace
Expand Down
4 changes: 3 additions & 1 deletion Lib/test/test_types.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Python test set -- part 6, built-in types

from test.support import run_with_locale, cpython_only
from test.support import run_with_locale, cpython_only, MISSING_C_DOCSTRINGS
import collections.abc
from collections import namedtuple
import copy
Expand Down Expand Up @@ -598,6 +598,8 @@ def test_slot_wrapper_types(self):
self.assertIsInstance(object.__lt__, types.WrapperDescriptorType)
self.assertIsInstance(int.__lt__, types.WrapperDescriptorType)

@unittest.skipIf(MISSING_C_DOCSTRINGS,
"Signature information for builtins requires docstrings")
def test_dunder_get_signature(self):
sig = inspect.signature(object.__init__.__get__)
self.assertEqual(list(sig.parameters), ["instance", "owner"])
Expand Down
3 changes: 3 additions & 0 deletions Lib/test/test_zoneinfo/test_zoneinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from datetime import date, datetime, time, timedelta, timezone
from functools import cached_property

from test.support import MISSING_C_DOCSTRINGS
from test.test_zoneinfo import _support as test_support
from test.test_zoneinfo._support import OS_ENV_LOCK, TZPATH_TEST_LOCK, ZoneInfoTestBase
from test.support.import_helper import import_module
Expand Down Expand Up @@ -404,6 +405,8 @@ def test_time_fixed_offset(self):
class CZoneInfoTest(ZoneInfoTest):
module = c_zoneinfo

@unittest.skipIf(MISSING_C_DOCSTRINGS,
"Signature information for builtins requires docstrings")
def test_signatures(self):
"""Ensure that C module has valid method signatures."""
import inspect
Expand Down
0