1- import re
2- import sys
3- import types
4- import unittest
5- import inspect
6- import linecache
7- import datetime
1+ import _testcapi
82import collections
9- import os
10- import shutil
3+ import datetime
114import functools
125import importlib
6+ import inspect
7+ import io
8+ import linecache
9+ import os
1310from os .path import normcase
11+ import _pickle
12+ import re
13+ import shutil
14+ import sys
15+ import types
16+ import unicodedata
17+ import unittest
18+
1419try :
1520 from concurrent .futures import ThreadPoolExecutor
1621except ImportError :
1722 ThreadPoolExecutor = None
18- import _testcapi
1923
2024from test .support import run_unittest , TESTFN , DirsOnSysPath
2125from test .support import MISSING_C_DOCSTRINGS
2226from test .script_helper import assert_python_ok , assert_python_failure
2327from test import inspect_fodder as mod
2428from test import inspect_fodder2 as mod2
2529
26- # C module for test_findsource_binary
27- import unicodedata
2830
2931# Functions tested in this suite:
3032# ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode,
@@ -1582,23 +1584,30 @@ def test(a, b:'foo'=10, *args:'bar', spam:'baz', ham=123, **kwargs:int):
15821584 ...))
15831585
15841586 def test_signature_on_unsupported_builtins (self ):
1585- with self .assertRaisesRegex (ValueError , 'not supported by signature' ):
1586- inspect .signature (type )
1587- with self .assertRaisesRegex (ValueError , 'not supported by signature' ):
1588- # support for 'wrapper_descriptor'
1589- inspect .signature (type .__call__ )
1590- with self .assertRaisesRegex (ValueError , 'not supported by signature' ):
1591- # support for 'method-wrapper'
1592- inspect .signature (min .__call__ )
1587+ with self .assertRaisesRegex (ValueError , 'no signature found' ):
1588+ # min simply doesn't have a signature (yet)
1589+ inspect .signature (min )
15931590
15941591 @unittest .skipIf (MISSING_C_DOCSTRINGS ,
15951592 "Signature information for builtins requires docstrings" )
15961593 def test_signature_on_builtins (self ):
1597- # min doesn't have a signature (yet)
1598- self .assertEqual (inspect .signature (min ), None )
15991594
1600- signature = inspect .signature (_testcapi .docstring_with_signature_with_defaults )
1601- self .assertTrue (isinstance (signature , inspect .Signature ))
1595+ def test_unbound_method (o ):
1596+ """Use this to test unbound methods (things that should have a self)"""
1597+ signature = inspect .signature (o )
1598+ self .assertTrue (isinstance (signature , inspect .Signature ))
1599+ self .assertEqual (list (signature .parameters .values ())[0 ].name , 'self' )
1600+ return signature
1601+
1602+ def test_callable (o ):
1603+ """Use this to test bound methods or normal callables (things that don't expect self)"""
1604+ signature = inspect .signature (o )
1605+ self .assertTrue (isinstance (signature , inspect .Signature ))
1606+ if signature .parameters :
1607+ self .assertNotEqual (list (signature .parameters .values ())[0 ].name , 'self' )
1608+ return signature
1609+
1610+ signature = test_callable (_testcapi .docstring_with_signature_with_defaults )
16021611 def p (name ): return signature .parameters [name ].default
16031612 self .assertEqual (p ('s' ), 'avocado' )
16041613 self .assertEqual (p ('b' ), b'bytes' )
@@ -1611,6 +1620,41 @@ def p(name): return signature.parameters[name].default
16111620 self .assertEqual (p ('sys' ), sys .maxsize )
16121621 self .assertEqual (p ('exp' ), sys .maxsize - 1 )
16131622
1623+ test_callable (type )
1624+ test_callable (object )
1625+
1626+ # normal method
1627+ # (PyMethodDescr_Type, "method_descriptor")
1628+ test_unbound_method (_pickle .Pickler .dump )
1629+ d = _pickle .Pickler (io .StringIO ())
1630+ test_callable (d .dump )
1631+
1632+ # static method
1633+ test_callable (str .maketrans )
1634+ test_callable ('abc' .maketrans )
1635+
1636+ # class method
1637+ test_callable (dict .fromkeys )
1638+ test_callable ({}.fromkeys )
1639+
1640+ # wrapper around slot (PyWrapperDescr_Type, "wrapper_descriptor")
1641+ test_unbound_method (type .__call__ )
1642+ test_unbound_method (int .__add__ )
1643+ test_callable ((3 ).__add__ )
1644+
1645+ # _PyMethodWrapper_Type
1646+ # support for 'method-wrapper'
1647+ test_callable (min .__call__ )
1648+
1649+ class ThisWorksNow :
1650+ __call__ = type
1651+ test_callable (ThisWorksNow ())
1652+
1653+
1654+ def test_signature_on_builtins_no_signature (self ):
1655+ with self .assertRaisesRegex (ValueError , 'no signature found for builtin' ):
1656+ inspect .signature (_testcapi .docstring_no_signature )
1657+
16141658 def test_signature_on_non_function (self ):
16151659 with self .assertRaisesRegex (TypeError , 'is not a callable object' ):
16161660 inspect .signature (42 )
@@ -1985,12 +2029,6 @@ class Bar(Spam, Foo):
19852029 ((('a' , ..., ..., "positional_or_keyword" ),),
19862030 ...))
19872031
1988- class ToFail :
1989- __call__ = type
1990- with self .assertRaisesRegex (ValueError , "not supported by signature" ):
1991- inspect .signature (ToFail ())
1992-
1993-
19942032 class Wrapped :
19952033 pass
19962034 Wrapped .__wrapped__ = lambda a : None
0 commit comments