Issue #20184: Add signature introspection for 30 of the builtins

Also adds a test to test_inspect to track progress on builtin
introspection support, to ensure it doesn't regress in the future.
This commit is contained in:
Nick Coghlan 2014-08-17 14:01:19 +10:00
parent eed671910f
commit f9e227e5a9
3 changed files with 1248 additions and 378 deletions

View File

@ -1,3 +1,4 @@
import builtins
import collections import collections
import datetime import datetime
import functools import functools
@ -23,7 +24,7 @@ except ImportError:
ThreadPoolExecutor = None ThreadPoolExecutor = None
from test.support import run_unittest, TESTFN, DirsOnSysPath, cpython_only from test.support import run_unittest, TESTFN, DirsOnSysPath, cpython_only
from test.support import MISSING_C_DOCSTRINGS from test.support import MISSING_C_DOCSTRINGS, cpython_only
from test.script_helper import assert_python_ok, assert_python_failure from test.script_helper import assert_python_ok, assert_python_failure
from test import inspect_fodder as mod from test import inspect_fodder as mod
from test import inspect_fodder2 as mod2 from test import inspect_fodder2 as mod2
@ -1623,6 +1624,57 @@ class MyParameter(inspect.Parameter):
# used in test_signature_object_pickle # used in test_signature_object_pickle
pass pass
@cpython_only
@unittest.skipIf(MISSING_C_DOCSTRINGS,
"Signature information for builtins requires docstrings")
def test_builtins_have_signatures(self):
# This checks all builtin callables in CPython have signatures
# A few have signatures Signature can't yet handle, so we skip those
# since they will have to wait until PEP 457 adds the required
# introspection support to the inspect module
# Some others also haven't been converted yet for various other
# reasons, so we also skip those for the time being, but design
# the test to fail in order to indicate when it needs to be
# updated.
no_signature = set()
# These need PEP 457 groups
needs_groups = ["range", "slice", "dir", "getattr",
"next", "iter", "vars"]
no_signature |= needs_groups
# These need PEP 457 groups or a signature change to accept None
needs_semantic_update = ["round"]
no_signature |= needs_semantic_update
# These need *args support in Argument Clinic
needs_varargs = ["min", "max", "print", "__build_class__"]
no_signature |= needs_varargs
# These simply weren't covered in the initial AC conversion
# for builtin callables
not_converted_yet = ["open", "__import__"]
no_signature |= not_converted_yet
# These builtin types are expected to provide introspection info
types_with_signatures = set()
# Check the signatures we expect to be there
ns = vars(builtins)
for name, obj in sorted(ns.items()):
if not callable(obj):
continue
# The builtin types haven't been converted to AC yet
if isinstance(obj, type) and (name not in types_with_signatures):
# Note that this also skips all the exception types
no_signature.append(name)
if (name in no_signature):
# Not yet converted
continue
with self.subTest(builtin=name):
self.assertIsNotNone(inspect.signature(obj))
# Check callables that haven't been converted don't claim a signature
# This ensures this test will start failing as more signatures are
# added, so the affected items can be moved into the scope of the
# regression test above
for name in no_signature:
with self.subTest(builtin=name):
self.assertIsNone(ns[name].__text_signature__)
class TestSignatureObject(unittest.TestCase): class TestSignatureObject(unittest.TestCase):
@staticmethod @staticmethod

View File

@ -10,6 +10,9 @@ Release date: TBA
Core and Builtins Core and Builtins
----------------- -----------------
- Issue #20184: Argument Clinic based signature introspection added for
30 of the builtin functions.
- Issue #22116: C functions and methods (of the 'builtin_function_or_method' - Issue #22116: C functions and methods (of the 'builtin_function_or_method'
type) can now be weakref'ed. Patch by Wei Wu. type) can now be weakref'ed. Patch by Wei Wu.

File diff suppressed because it is too large Load Diff