Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(95725)

Delta Between Two Patch Sets: Lib/test/test_inspect.py

Issue 16510: Using appropriate checks in tests
Left Patch Set: Created 5 years, 9 months ago
Right Patch Set: Created 5 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_hmac.py ('k') | Lib/test/test_iter.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 import collections
2 import datetime
3 import functools
4 import importlib
5 import inspect
6 import io
7 import linecache
8 import os
9 from os.path import normcase
10 import _pickle
1 import re 11 import re
12 import shutil
2 import sys 13 import sys
3 import types 14 import types
15 import unicodedata
4 import unittest 16 import unittest
5 import inspect 17
6 import linecache
7 import datetime
8 import collections
9 import os
10 import shutil
11 import functools
12 import importlib
13 from os.path import normcase
14 try: 18 try:
15 from concurrent.futures import ThreadPoolExecutor 19 from concurrent.futures import ThreadPoolExecutor
16 except ImportError: 20 except ImportError:
17 ThreadPoolExecutor = None 21 ThreadPoolExecutor = None
18 22
19 from test.support import run_unittest, TESTFN, DirsOnSysPath 23 from test.support import run_unittest, TESTFN, DirsOnSysPath, cpython_only
24 from test.support import MISSING_C_DOCSTRINGS
20 from test.script_helper import assert_python_ok, assert_python_failure 25 from test.script_helper import assert_python_ok, assert_python_failure
21 from test import inspect_fodder as mod 26 from test import inspect_fodder as mod
22 from test import inspect_fodder2 as mod2 27 from test import inspect_fodder2 as mod2
23 28
24 # C module for test_findsource_binary
25 import unicodedata
26 29
27 # Functions tested in this suite: 30 # Functions tested in this suite:
28 # ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode, 31 # ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode,
29 # isbuiltin, isroutine, isgenerator, isgeneratorfunction, getmembers, 32 # isbuiltin, isroutine, isgenerator, isgeneratorfunction, getmembers,
30 # getdoc, getfile, getmodule, getsourcefile, getcomments, getsource, 33 # getdoc, getfile, getmodule, getsourcefile, getcomments, getsource,
31 # getclasstree, getargspec, getargvalues, formatargspec, formatargvalues, 34 # getclasstree, getargspec, getargvalues, formatargspec, formatargvalues,
32 # currentframe, stack, trace, isdatadescriptor 35 # currentframe, stack, trace, isdatadescriptor
33 36
34 # NOTE: There are some additional tests relating to interaction with 37 # NOTE: There are some additional tests relating to interaction with
35 # zipimport in the test_zipimport_support test module. 38 # zipimport in the test_zipimport_support test module.
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 self.assertEqual(inspect.getsourcefile(co), None) 311 self.assertEqual(inspect.getsourcefile(co), None)
309 linecache.cache[co.co_filename] = (1, None, "None", co.co_filename) 312 linecache.cache[co.co_filename] = (1, None, "None", co.co_filename)
310 try: 313 try:
311 self.assertEqual(normcase(inspect.getsourcefile(co)), fn) 314 self.assertEqual(normcase(inspect.getsourcefile(co)), fn)
312 finally: 315 finally:
313 del linecache.cache[co.co_filename] 316 del linecache.cache[co.co_filename]
314 317
315 def test_getfile(self): 318 def test_getfile(self):
316 self.assertEqual(inspect.getfile(mod.StupidGit), mod.__file__) 319 self.assertEqual(inspect.getfile(mod.StupidGit), mod.__file__)
317 320
321 def test_getfile_class_without_module(self):
322 class CM(type):
323 @property
324 def __module__(cls):
325 raise AttributeError
326 class C(metaclass=CM):
327 pass
328 with self.assertRaises(TypeError):
329 inspect.getfile(C)
330
318 def test_getmodule_recursion(self): 331 def test_getmodule_recursion(self):
319 from types import ModuleType 332 from types import ModuleType
320 name = '__inspect_dummy' 333 name = '__inspect_dummy'
321 m = sys.modules[name] = ModuleType(name) 334 m = sys.modules[name] = ModuleType(name)
322 m.__file__ = "<string>" # hopefully not a real filename... 335 m.__file__ = "<string>" # hopefully not a real filename...
323 m.__loader__ = "dummy" # pretend the filename is understood by a loader 336 m.__loader__ = "dummy" # pretend the filename is understood by a loader
324 exec("def x(): pass", m.__dict__) 337 exec("def x(): pass", m.__dict__)
325 self.assertEqual(inspect.getsourcefile(m.x.__code__), '<string>') 338 self.assertEqual(inspect.getsourcefile(m.x.__code__), '<string>')
326 del sys.modules[name] 339 del sys.modules[name]
327 inspect.getmodule(compile('a=10','','single')) 340 inspect.getmodule(compile('a=10','','single'))
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after
557 kwonlydefaults_e={'arg2':1}, 570 kwonlydefaults_e={'arg2':1},
558 formatted='(*arg1, arg2=1)') 571 formatted='(*arg1, arg2=1)')
559 572
560 self.assertFullArgSpecEquals(mod2.annotated, ['arg1'], 573 self.assertFullArgSpecEquals(mod2.annotated, ['arg1'],
561 ann_e={'arg1' : list}, 574 ann_e={'arg1' : list},
562 formatted='(arg1: list)') 575 formatted='(arg1: list)')
563 self.assertFullArgSpecEquals(mod2.keyword_only_arg, [], 576 self.assertFullArgSpecEquals(mod2.keyword_only_arg, [],
564 kwonlyargs_e=['arg'], 577 kwonlyargs_e=['arg'],
565 formatted='(*, arg)') 578 formatted='(*, arg)')
566 579
580 def test_getfullargspec_signature_attr(self):
581 def test():
582 pass
583 spam_param = inspect.Parameter('spam', inspect.Parameter.POSITIONAL_ONLY )
584 test.__signature__ = inspect.Signature(parameters=(spam_param,))
585
586 self.assertFullArgSpecEquals(test, args_e=['spam'], formatted='(spam)')
587
588 def test_getfullargspec_signature_annos(self):
589 def test(a:'spam') -> 'ham': pass
590 spec = inspect.getfullargspec(test)
591 self.assertEqual(test.__annotations__, spec.annotations)
592
593 def test(): pass
594 spec = inspect.getfullargspec(test)
595 self.assertEqual(test.__annotations__, spec.annotations)
596
597 @unittest.skipIf(MISSING_C_DOCSTRINGS,
598 "Signature information for builtins requires docstrings")
599 def test_getfullargspec_builtin_methods(self):
600 self.assertFullArgSpecEquals(_pickle.Pickler.dump,
601 args_e=['self', 'obj'], formatted='(self, o bj)')
602
603 self.assertFullArgSpecEquals(_pickle.Pickler(io.BytesIO()).dump,
604 args_e=['self', 'obj'], formatted='(self, o bj)')
605
606 @cpython_only
607 @unittest.skipIf(MISSING_C_DOCSTRINGS,
608 "Signature information for builtins requires docstrings")
609 def test_getfullagrspec_builtin_func(self):
610 import _testcapi
611 builtin = _testcapi.docstring_with_signature_with_defaults
612 spec = inspect.getfullargspec(builtin)
613 self.assertEqual(spec.defaults[0], 'avocado')
614
615 @cpython_only
616 @unittest.skipIf(MISSING_C_DOCSTRINGS,
617 "Signature information for builtins requires docstrings")
618 def test_getfullagrspec_builtin_func_no_signature(self):
619 import _testcapi
620 builtin = _testcapi.docstring_no_signature
621 with self.assertRaises(TypeError):
622 inspect.getfullargspec(builtin)
567 623
568 def test_getargspec_method(self): 624 def test_getargspec_method(self):
569 class A(object): 625 class A(object):
570 def m(self): 626 def m(self):
571 pass 627 pass
572 self.assertArgSpecEquals(A.m, ['self']) 628 self.assertArgSpecEquals(A.m, ['self'])
573 629
574 def test_classify_newstyle(self): 630 def test_classify_newstyle(self):
575 class A(object): 631 class A(object):
576 632
577 def s(): pass 633 def s(): pass
578 s = staticmethod(s) 634 s = staticmethod(s)
579 635
580 def c(cls): pass 636 def c(cls): pass
581 c = classmethod(c) 637 c = classmethod(c)
582 638
583 def getp(self): pass 639 def getp(self): pass
584 p = property(getp) 640 p = property(getp)
585 641
586 def m(self): pass 642 def m(self): pass
587 643
588 def m1(self): pass 644 def m1(self): pass
589 645
590 datablob = '1' 646 datablob = '1'
591 647
592 dd = _BrokenDataDescriptor() 648 dd = _BrokenDataDescriptor()
593 md = _BrokenMethodDescriptor() 649 md = _BrokenMethodDescriptor()
594 650
595 attrs = attrs_wo_objs(A) 651 attrs = attrs_wo_objs(A)
652
653 self.assertIn(('__new__', 'method', object), attrs, 'missing __new__')
654 self.assertIn(('__init__', 'method', object), attrs, 'missing __init__')
655
596 self.assertIn(('s', 'static method', A), attrs, 'missing static method') 656 self.assertIn(('s', 'static method', A), attrs, 'missing static method')
597 self.assertIn(('c', 'class method', A), attrs, 'missing class method') 657 self.assertIn(('c', 'class method', A), attrs, 'missing class method')
598 self.assertIn(('p', 'property', A), attrs, 'missing property') 658 self.assertIn(('p', 'property', A), attrs, 'missing property')
599 self.assertIn(('m', 'method', A), attrs, 659 self.assertIn(('m', 'method', A), attrs,
600 'missing plain method: %r' % attrs) 660 'missing plain method: %r' % attrs)
601 self.assertIn(('m1', 'method', A), attrs, 'missing plain method') 661 self.assertIn(('m1', 'method', A), attrs, 'missing plain method')
602 self.assertIn(('datablob', 'data', A), attrs, 'missing data') 662 self.assertIn(('datablob', 'data', A), attrs, 'missing data')
603 self.assertIn(('md', 'method', A), attrs, 'missing method descriptor') 663 self.assertIn(('md', 'method', A), attrs, 'missing method descriptor')
604 self.assertIn(('dd', 'data', A), attrs, 'missing data descriptor') 664 self.assertIn(('dd', 'data', A), attrs, 'missing data descriptor')
605 665
(...skipping 895 matching lines...) Expand 10 before | Expand all | Expand 10 after
1501 for param in sig.parameters.values()), 1561 for param in sig.parameters.values()),
1502 (... if sig.return_annotation is sig.empty 1562 (... if sig.return_annotation is sig.empty
1503 else sig.return_annotation)) 1563 else sig.return_annotation))
1504 1564
1505 def test_signature_object(self): 1565 def test_signature_object(self):
1506 S = inspect.Signature 1566 S = inspect.Signature
1507 P = inspect.Parameter 1567 P = inspect.Parameter
1508 1568
1509 self.assertEqual(str(S()), '()') 1569 self.assertEqual(str(S()), '()')
1510 1570
1511 def test(po, pk, *args, ko, **kwargs): 1571 def test(po, pk, pod=42, pkd=100, *args, ko, **kwargs):
1512 pass 1572 pass
1513 sig = inspect.signature(test) 1573 sig = inspect.signature(test)
1514 po = sig.parameters['po'].replace(kind=P.POSITIONAL_ONLY) 1574 po = sig.parameters['po'].replace(kind=P.POSITIONAL_ONLY)
1575 pod = sig.parameters['pod'].replace(kind=P.POSITIONAL_ONLY)
1515 pk = sig.parameters['pk'] 1576 pk = sig.parameters['pk']
1577 pkd = sig.parameters['pkd']
1516 args = sig.parameters['args'] 1578 args = sig.parameters['args']
1517 ko = sig.parameters['ko'] 1579 ko = sig.parameters['ko']
1518 kwargs = sig.parameters['kwargs'] 1580 kwargs = sig.parameters['kwargs']
1519 1581
1520 S((po, pk, args, ko, kwargs)) 1582 S((po, pk, args, ko, kwargs))
1521 1583
1522 with self.assertRaisesRegex(ValueError, 'wrong parameter order'): 1584 with self.assertRaisesRegex(ValueError, 'wrong parameter order'):
1523 S((pk, po, args, ko, kwargs)) 1585 S((pk, po, args, ko, kwargs))
1524 1586
1525 with self.assertRaisesRegex(ValueError, 'wrong parameter order'): 1587 with self.assertRaisesRegex(ValueError, 'wrong parameter order'):
1526 S((po, args, pk, ko, kwargs)) 1588 S((po, args, pk, ko, kwargs))
1527 1589
1528 with self.assertRaisesRegex(ValueError, 'wrong parameter order'): 1590 with self.assertRaisesRegex(ValueError, 'wrong parameter order'):
1529 S((args, po, pk, ko, kwargs)) 1591 S((args, po, pk, ko, kwargs))
1530 1592
1531 with self.assertRaisesRegex(ValueError, 'wrong parameter order'): 1593 with self.assertRaisesRegex(ValueError, 'wrong parameter order'):
1532 S((po, pk, args, kwargs, ko)) 1594 S((po, pk, args, kwargs, ko))
1533 1595
1534 kwargs2 = kwargs.replace(name='args') 1596 kwargs2 = kwargs.replace(name='args')
1535 with self.assertRaisesRegex(ValueError, 'duplicate parameter name'): 1597 with self.assertRaisesRegex(ValueError, 'duplicate parameter name'):
1536 S((po, pk, args, kwargs2, ko)) 1598 S((po, pk, args, kwargs2, ko))
1599
1600 with self.assertRaisesRegex(ValueError, 'follows default argument'):
1601 S((pod, po))
1602
1603 with self.assertRaisesRegex(ValueError, 'follows default argument'):
1604 S((po, pkd, pk))
1605
1606 with self.assertRaisesRegex(ValueError, 'follows default argument'):
1607 S((pkd, pk))
1537 1608
1538 def test_signature_immutability(self): 1609 def test_signature_immutability(self):
1539 def test(a): 1610 def test(a):
1540 pass 1611 pass
1541 sig = inspect.signature(test) 1612 sig = inspect.signature(test)
1542 1613
1543 with self.assertRaises(AttributeError): 1614 with self.assertRaises(AttributeError):
1544 sig.foo = 'bar' 1615 sig.foo = 'bar'
1545 1616
1546 with self.assertRaises(TypeError): 1617 with self.assertRaises(TypeError):
(...skipping 25 matching lines...) Expand all
1572 pass 1643 pass
1573 self.assertEqual(self.signature(test), 1644 self.assertEqual(self.signature(test),
1574 ((('a', ..., ..., "positional_or_keyword"), 1645 ((('a', ..., ..., "positional_or_keyword"),
1575 ('b', 10, 'foo', "positional_or_keyword"), 1646 ('b', 10, 'foo', "positional_or_keyword"),
1576 ('args', ..., 'bar', "var_positional"), 1647 ('args', ..., 'bar', "var_positional"),
1577 ('spam', ..., 'baz', "keyword_only"), 1648 ('spam', ..., 'baz', "keyword_only"),
1578 ('ham', 123, ..., "keyword_only"), 1649 ('ham', 123, ..., "keyword_only"),
1579 ('kwargs', ..., int, "var_keyword")), 1650 ('kwargs', ..., int, "var_keyword")),
1580 ...)) 1651 ...))
1581 1652
1582 def test_signature_on_builtin_function(self): 1653 @cpython_only
1583 with self.assertRaisesRegex(ValueError, 'not supported by signature'): 1654 @unittest.skipIf(MISSING_C_DOCSTRINGS,
1584 inspect.signature(type) 1655 "Signature information for builtins requires docstrings")
1585 with self.assertRaisesRegex(ValueError, 'not supported by signature'): 1656 def test_signature_on_builtins(self):
1586 # support for 'wrapper_descriptor' 1657 import _testcapi
1587 inspect.signature(type.__call__) 1658
1588 with self.assertRaisesRegex(ValueError, 'not supported by signature'): 1659 def test_unbound_method(o):
1589 # support for 'method-wrapper' 1660 """Use this to test unbound methods (things that should have a self) """
1590 inspect.signature(min.__call__) 1661 signature = inspect.signature(o)
1591 with self.assertRaisesRegex(ValueError, 1662 self.assertTrue(isinstance(signature, inspect.Signature))
1592 'no signature found for builtin function'): 1663 self.assertEqual(list(signature.parameters.values())[0].name, 'self' )
1593 # support for 'method-wrapper' 1664 return signature
1594 inspect.signature(min) 1665
1666 def test_callable(o):
1667 """Use this to test bound methods or normal callables (things that d on't expect self)"""
1668 signature = inspect.signature(o)
1669 self.assertTrue(isinstance(signature, inspect.Signature))
1670 if signature.parameters:
1671 self.assertNotEqual(list(signature.parameters.values())[0].name, 'self')
1672 return signature
1673
1674 signature = test_callable(_testcapi.docstring_with_signature_with_defaul ts)
1675 def p(name): return signature.parameters[name].default
1676 self.assertEqual(p('s'), 'avocado')
1677 self.assertEqual(p('b'), b'bytes')
1678 self.assertEqual(p('d'), 3.14)
1679 self.assertEqual(p('i'), 35)
1680 self.assertEqual(p('n'), None)
1681 self.assertEqual(p('t'), True)
1682 self.assertEqual(p('f'), False)
1683 self.assertEqual(p('local'), 3)
1684 self.assertEqual(p('sys'), sys.maxsize)
1685 self.assertEqual(p('exp'), sys.maxsize - 1)
1686
1687 test_callable(type)
1688 test_callable(object)
1689
1690 # normal method
1691 # (PyMethodDescr_Type, "method_descriptor")
1692 test_unbound_method(_pickle.Pickler.dump)
1693 d = _pickle.Pickler(io.StringIO())
1694 test_callable(d.dump)
1695
1696 # static method
1697 test_callable(str.maketrans)
1698 test_callable('abc'.maketrans)
1699
1700 # class method
1701 test_callable(dict.fromkeys)
1702 test_callable({}.fromkeys)
1703
1704 # wrapper around slot (PyWrapperDescr_Type, "wrapper_descriptor")
1705 test_unbound_method(type.__call__)
1706 test_unbound_method(int.__add__)
1707 test_callable((3).__add__)
1708
1709 # _PyMethodWrapper_Type
1710 # support for 'method-wrapper'
1711 test_callable(min.__call__)
1712
1713 class ThisWorksNow:
1714 __call__ = type
1715 test_callable(ThisWorksNow())
1716
1717 @cpython_only
1718 @unittest.skipIf(MISSING_C_DOCSTRINGS,
1719 "Signature information for builtins requires docstrings")
1720 def test_signature_on_decorated_builtins(self):
1721 import _testcapi
1722 func = _testcapi.docstring_with_signature_with_defaults
1723
1724 def decorator(func):
1725 @functools.wraps(func)
1726 def wrapper(*args, **kwargs) -> int:
1727 return func(*args, **kwargs)
1728 return wrapper
1729
1730 decorated_func = decorator(func)
1731
1732 self.assertEqual(inspect.signature(func),
1733 inspect.signature(decorated_func))
1734
1735 @cpython_only
1736 def test_signature_on_builtins_no_signature(self):
1737 import _testcapi
1738 with self.assertRaisesRegex(ValueError, 'no signature found for builtin' ):
1739 inspect.signature(_testcapi.docstring_no_signature)
1595 1740
1596 def test_signature_on_non_function(self): 1741 def test_signature_on_non_function(self):
1597 with self.assertRaisesRegex(TypeError, 'is not a callable object'): 1742 with self.assertRaisesRegex(TypeError, 'is not a callable object'):
1598 inspect.signature(42) 1743 inspect.signature(42)
1599 1744
1600 with self.assertRaisesRegex(TypeError, 'is not a Python function'): 1745 with self.assertRaisesRegex(TypeError, 'is not a Python function'):
1601 inspect.Signature.from_function(42) 1746 inspect.Signature.from_function(42)
1602 1747
1748 def test_signature_from_builtin_errors(self):
1749 with self.assertRaisesRegex(TypeError, 'is not a Python builtin'):
1750 inspect.Signature.from_builtin(42)
1751
1752 def test_signature_from_functionlike_object(self):
1753 def func(a,b, *args, kwonly=True, kwonlyreq, **kwargs):
1754 pass
1755
1756 class funclike:
1757 # Has to be callable, and have correct
1758 # __code__, __annotations__, __defaults__, __name__,
1759 # and __kwdefaults__ attributes
1760
1761 def __init__(self, func):
1762 self.__name__ = func.__name__
1763 self.__code__ = func.__code__
1764 self.__annotations__ = func.__annotations__
1765 self.__defaults__ = func.__defaults__
1766 self.__kwdefaults__ = func.__kwdefaults__
1767 self.func = func
1768
1769 def __call__(self, *args, **kwargs):
1770 return self.func(*args, **kwargs)
1771
1772 sig_func = inspect.Signature.from_function(func)
1773
1774 sig_funclike = inspect.Signature.from_function(funclike(func))
1775 self.assertEqual(sig_funclike, sig_func)
1776
1777 sig_funclike = inspect.signature(funclike(func))
1778 self.assertEqual(sig_funclike, sig_func)
1779
1780 # If object is not a duck type of function, then
1781 # signature will try to get a signature for its '__call__'
1782 # method
1783 fl = funclike(func)
1784 del fl.__defaults__
1785 self.assertEqual(self.signature(fl),
1786 ((('args', ..., ..., "var_positional"),
1787 ('kwargs', ..., ..., "var_keyword")),
1788 ...))
1789
1790 def test_signature_functionlike_class(self):
1791 # We only want to duck type function-like objects,
1792 # not classes.
1793
1794 def func(a,b, *args, kwonly=True, kwonlyreq, **kwargs):
1795 pass
1796
1797 class funclike:
1798 def __init__(self, marker):
1799 pass
1800
1801 __name__ = func.__name__
1802 __code__ = func.__code__
1803 __annotations__ = func.__annotations__
1804 __defaults__ = func.__defaults__
1805 __kwdefaults__ = func.__kwdefaults__
1806
1807 with self.assertRaisesRegex(TypeError, 'is not a Python function'):
1808 inspect.Signature.from_function(funclike)
1809
1810 self.assertEqual(str(inspect.signature(funclike)), '(marker)')
1811
1603 def test_signature_on_method(self): 1812 def test_signature_on_method(self):
1604 class Test: 1813 class Test:
1605 def foo(self, arg1, arg2=1) -> int: 1814 def __init__(*args):
1606 pass 1815 pass
1607 1816 def m1(self, arg1, arg2=1) -> int:
1608 meth = Test().foo 1817 pass
1609 1818 def m2(*args):
1610 self.assertEqual(self.signature(meth), 1819 pass
1820 def __call__(*, a):
1821 pass
1822
1823 self.assertEqual(self.signature(Test().m1),
1611 ((('arg1', ..., ..., "positional_or_keyword"), 1824 ((('arg1', ..., ..., "positional_or_keyword"),
1612 ('arg2', 1, ..., "positional_or_keyword")), 1825 ('arg2', 1, ..., "positional_or_keyword")),
1613 int)) 1826 int))
1827
1828 self.assertEqual(self.signature(Test().m2),
1829 ((('args', ..., ..., "var_positional"),),
1830 ...))
1831
1832 self.assertEqual(self.signature(Test),
1833 ((('args', ..., ..., "var_positional"),),
1834 ...))
1835
1836 with self.assertRaisesRegex(ValueError, 'invalid method signature'):
1837 self.signature(Test())
1614 1838
1615 def test_signature_on_classmethod(self): 1839 def test_signature_on_classmethod(self):
1616 class Test: 1840 class Test:
1617 @classmethod 1841 @classmethod
1618 def foo(cls, arg1, *, arg2=1): 1842 def foo(cls, arg1, *, arg2=1):
1619 pass 1843 pass
1620 1844
1621 meth = Test().foo 1845 meth = Test().foo
1622 self.assertEqual(self.signature(meth), 1846 self.assertEqual(self.signature(meth),
1623 ((('arg1', ..., ..., "positional_or_keyword"), 1847 ((('arg1', ..., ..., "positional_or_keyword"),
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
1798 ba = inspect.signature(_foo).bind(11, 12) 2022 ba = inspect.signature(_foo).bind(11, 12)
1799 self.assertEqual(_foo(*ba.args, **ba.kwargs), (11, 12, 13)) 2023 self.assertEqual(_foo(*ba.args, **ba.kwargs), (11, 12, 13))
1800 ba = inspect.signature(_foo).bind(11, b=12) 2024 ba = inspect.signature(_foo).bind(11, b=12)
1801 self.assertEqual(_foo(*ba.args, **ba.kwargs), (11, 12, 13)) 2025 self.assertEqual(_foo(*ba.args, **ba.kwargs), (11, 12, 13))
1802 ba = inspect.signature(_foo).bind(b=12) 2026 ba = inspect.signature(_foo).bind(b=12)
1803 self.assertEqual(_foo(*ba.args, **ba.kwargs), (10, 12, 13)) 2027 self.assertEqual(_foo(*ba.args, **ba.kwargs), (10, 12, 13))
1804 _foo = partial(_foo, b=10) 2028 _foo = partial(_foo, b=10)
1805 ba = inspect.signature(_foo).bind(12, 14) 2029 ba = inspect.signature(_foo).bind(12, 14)
1806 self.assertEqual(_foo(*ba.args, **ba.kwargs), (12, 14, 13)) 2030 self.assertEqual(_foo(*ba.args, **ba.kwargs), (12, 14, 13))
1807 2031
2032 def test_signature_on_partialmethod(self):
2033 from functools import partialmethod
2034
2035 class Spam:
2036 def test():
2037 pass
2038 ham = partialmethod(test)
2039
2040 with self.assertRaisesRegex(ValueError, "has incorrect arguments"):
2041 inspect.signature(Spam.ham)
2042
2043 class Spam:
2044 def test(it, a, *, c) -> 'spam':
2045 pass
2046 ham = partialmethod(test, c=1)
2047
2048 self.assertEqual(self.signature(Spam.ham),
2049 ((('it', ..., ..., 'positional_or_keyword'),
2050 ('a', ..., ..., 'positional_or_keyword'),
2051 ('c', 1, ..., 'keyword_only')),
2052 'spam'))
2053
2054 self.assertEqual(self.signature(Spam().ham),
2055 ((('a', ..., ..., 'positional_or_keyword'),
2056 ('c', 1, ..., 'keyword_only')),
2057 'spam'))
2058
2059 def test_signature_on_fake_partialmethod(self):
2060 def foo(a): pass
2061 foo._partialmethod = 'spam'
2062 self.assertEqual(str(inspect.signature(foo)), '(a)')
2063
1808 def test_signature_on_decorated(self): 2064 def test_signature_on_decorated(self):
1809 import functools 2065 import functools
1810 2066
1811 def decorator(func): 2067 def decorator(func):
1812 @functools.wraps(func) 2068 @functools.wraps(func)
1813 def wrapper(*args, **kwargs) -> int: 2069 def wrapper(*args, **kwargs) -> int:
1814 return func(*args, **kwargs) 2070 return func(*args, **kwargs)
1815 return wrapper 2071 return wrapper
1816 2072
1817 class Foo: 2073 class Foo:
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
1939 def __init__(self, b): 2195 def __init__(self, b):
1940 pass 2196 pass
1941 2197
1942 self.assertEqual(self.signature(CM), 2198 self.assertEqual(self.signature(CM),
1943 ((('name', ..., ..., "positional_or_keyword"), 2199 ((('name', ..., ..., "positional_or_keyword"),
1944 ('bases', ..., ..., "positional_or_keyword"), 2200 ('bases', ..., ..., "positional_or_keyword"),
1945 ('dct', ..., ..., "positional_or_keyword"), 2201 ('dct', ..., ..., "positional_or_keyword"),
1946 ('bar', 2, ..., "keyword_only")), 2202 ('bar', 2, ..., "keyword_only")),
1947 ...)) 2203 ...))
1948 2204
2205 @unittest.skipIf(MISSING_C_DOCSTRINGS,
2206 "Signature information for builtins requires docstrings")
2207 def test_signature_on_class_without_init(self):
2208 # Test classes without user-defined __init__ or __new__
2209 class C: pass
2210 self.assertEqual(str(inspect.signature(C)), '()')
2211 class D(C): pass
2212 self.assertEqual(str(inspect.signature(D)), '()')
2213
2214 # Test meta-classes without user-defined __init__ or __new__
2215 class C(type): pass
2216 self.assertEqual(str(inspect.signature(C)),
2217 '(object_or_name, bases, dict)')
2218 class D(C): pass
2219 self.assertEqual(str(inspect.signature(D)),
2220 '(object_or_name, bases, dict)')
2221
2222 @unittest.skipIf(MISSING_C_DOCSTRINGS,
2223 "Signature information for builtins requires docstrings")
2224 def test_signature_on_builtin_class(self):
2225 self.assertEqual(str(inspect.signature(_pickle.Pickler)),
2226 '(file, protocol=None, fix_imports=True)')
2227
2228 class P(_pickle.Pickler): pass
2229 class EmptyTrait: pass
2230 class P2(EmptyTrait, P): pass
2231 self.assertEqual(str(inspect.signature(P)),
2232 '(file, protocol=None, fix_imports=True)')
2233 self.assertEqual(str(inspect.signature(P2)),
2234 '(file, protocol=None, fix_imports=True)')
2235
2236 class P3(P2):
2237 def __init__(self, spam):
2238 pass
2239 self.assertEqual(str(inspect.signature(P3)), '(spam)')
2240
2241 class MetaP(type):
2242 def __call__(cls, foo, bar):
2243 pass
2244 class P4(P2, metaclass=MetaP):
2245 pass
2246 self.assertEqual(str(inspect.signature(P4)), '(foo, bar)')
2247
1949 def test_signature_on_callable_objects(self): 2248 def test_signature_on_callable_objects(self):
1950 class Foo: 2249 class Foo:
1951 def __call__(self, a): 2250 def __call__(self, a):
1952 pass 2251 pass
1953 2252
1954 self.assertEqual(self.signature(Foo()), 2253 self.assertEqual(self.signature(Foo()),
1955 ((('a', ..., ..., "positional_or_keyword"),), 2254 ((('a', ..., ..., "positional_or_keyword"),),
1956 ...)) 2255 ...))
1957 2256
1958 class Spam: 2257 class Spam:
1959 pass 2258 pass
1960 with self.assertRaisesRegex(TypeError, "is not a callable object"): 2259 with self.assertRaisesRegex(TypeError, "is not a callable object"):
1961 inspect.signature(Spam()) 2260 inspect.signature(Spam())
1962 2261
1963 class Bar(Spam, Foo): 2262 class Bar(Spam, Foo):
1964 pass 2263 pass
1965 2264
1966 self.assertEqual(self.signature(Bar()), 2265 self.assertEqual(self.signature(Bar()),
1967 ((('a', ..., ..., "positional_or_keyword"),), 2266 ((('a', ..., ..., "positional_or_keyword"),),
1968 ...)) 2267 ...))
1969
1970 class ToFail:
1971 __call__ = type
1972 with self.assertRaisesRegex(ValueError, "not supported by signature"):
1973 inspect.signature(ToFail())
1974
1975 2268
1976 class Wrapped: 2269 class Wrapped:
1977 pass 2270 pass
1978 Wrapped.__wrapped__ = lambda a: None 2271 Wrapped.__wrapped__ = lambda a: None
1979 self.assertEqual(self.signature(Wrapped), 2272 self.assertEqual(self.signature(Wrapped),
1980 ((('a', ..., ..., "positional_or_keyword"),), 2273 ((('a', ..., ..., "positional_or_keyword"),),
1981 ...)) 2274 ...))
1982 # wrapper loop: 2275 # wrapper loop:
1983 Wrapped.__wrapped__ = Wrapped 2276 Wrapped.__wrapped__ = Wrapped
1984 with self.assertRaisesRegex(ValueError, 'wrapper loop'): 2277 with self.assertRaisesRegex(ValueError, 'wrapper loop'):
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
2049 pass 2342 pass
2050 self.assertEqual(str(inspect.signature(foo)), 2343 self.assertEqual(str(inspect.signature(foo)),
2051 '(a:int=1, *args, b, c=None, **kwargs) -> 42') 2344 '(a:int=1, *args, b, c=None, **kwargs) -> 42')
2052 2345
2053 def foo(): 2346 def foo():
2054 pass 2347 pass
2055 self.assertEqual(str(inspect.signature(foo)), '()') 2348 self.assertEqual(str(inspect.signature(foo)), '()')
2056 2349
2057 def test_signature_str_positional_only(self): 2350 def test_signature_str_positional_only(self):
2058 P = inspect.Parameter 2351 P = inspect.Parameter
2352 S = inspect.Signature
2059 2353
2060 def test(a_po, *, b, **kwargs): 2354 def test(a_po, *, b, **kwargs):
2061 return a_po, kwargs 2355 return a_po, kwargs
2062 2356
2063 sig = inspect.signature(test) 2357 sig = inspect.signature(test)
2064 new_params = list(sig.parameters.values()) 2358 new_params = list(sig.parameters.values())
2065 new_params[0] = new_params[0].replace(kind=P.POSITIONAL_ONLY) 2359 new_params[0] = new_params[0].replace(kind=P.POSITIONAL_ONLY)
2066 test.__signature__ = sig.replace(parameters=new_params) 2360 test.__signature__ = sig.replace(parameters=new_params)
2067 2361
2068 self.assertEqual(str(inspect.signature(test)), 2362 self.assertEqual(str(inspect.signature(test)),
2069 '(<a_po>, *, b, **kwargs)') 2363 '(a_po, /, *, b, **kwargs)')
2070 2364
2071 sig = inspect.signature(test) 2365 self.assertEqual(str(S(parameters=[P('foo', P.POSITIONAL_ONLY)])),
2072 new_params = list(sig.parameters.values()) 2366 '(foo, /)')
2073 new_params[0] = new_params[0].replace(name=None) 2367
2074 test.__signature__ = sig.replace(parameters=new_params) 2368 self.assertEqual(str(S(parameters=[
2075 self.assertEqual(str(inspect.signature(test)), 2369 P('foo', P.POSITIONAL_ONLY),
2076 '(<0>, *, b, **kwargs)') 2370 P('bar', P.VAR_KEYWORD)])),
2371 '(foo, /, **bar)')
2372
2373 self.assertEqual(str(S(parameters=[
2374 P('foo', P.POSITIONAL_ONLY),
2375 P('bar', P.VAR_POSITIONAL)])),
2376 '(foo, /, *bar)')
2077 2377
2078 def test_signature_replace_anno(self): 2378 def test_signature_replace_anno(self):
2079 def test() -> 42: 2379 def test() -> 42:
2080 pass 2380 pass
2081 2381
2082 sig = inspect.signature(test) 2382 sig = inspect.signature(test)
2083 sig = sig.replace(return_annotation=None) 2383 sig = sig.replace(return_annotation=None)
2084 self.assertIsNone(sig.return_annotation) 2384 self.assertIsNone(sig.return_annotation)
2085 sig = sig.replace(return_annotation=sig.empty) 2385 sig = sig.replace(return_annotation=sig.empty)
2086 self.assertIs(sig.return_annotation, sig.empty) 2386 self.assertIs(sig.return_annotation, sig.empty)
(...skipping 18 matching lines...) Expand all
2105 self.assertEqual(p.default, 10) 2405 self.assertEqual(p.default, 10)
2106 self.assertIs(p.annotation, p.empty) 2406 self.assertIs(p.annotation, p.empty)
2107 self.assertEqual(p.kind, inspect.Parameter.POSITIONAL_ONLY) 2407 self.assertEqual(p.kind, inspect.Parameter.POSITIONAL_ONLY)
2108 2408
2109 with self.assertRaisesRegex(ValueError, 'invalid value'): 2409 with self.assertRaisesRegex(ValueError, 'invalid value'):
2110 inspect.Parameter('foo', default=10, kind='123') 2410 inspect.Parameter('foo', default=10, kind='123')
2111 2411
2112 with self.assertRaisesRegex(ValueError, 'not a valid parameter name'): 2412 with self.assertRaisesRegex(ValueError, 'not a valid parameter name'):
2113 inspect.Parameter('1', kind=inspect.Parameter.VAR_KEYWORD) 2413 inspect.Parameter('1', kind=inspect.Parameter.VAR_KEYWORD)
2114 2414
2415 with self.assertRaisesRegex(TypeError, 'name must be a str'):
2416 inspect.Parameter(None, kind=inspect.Parameter.VAR_KEYWORD)
2417
2115 with self.assertRaisesRegex(ValueError, 2418 with self.assertRaisesRegex(ValueError,
2116 'non-positional-only parameter'): 2419 'is not a valid parameter name'):
2117 inspect.Parameter(None, kind=inspect.Parameter.VAR_KEYWORD) 2420 inspect.Parameter('$', kind=inspect.Parameter.VAR_KEYWORD)
2118 2421
2119 with self.assertRaisesRegex(ValueError, 'cannot have default values'): 2422 with self.assertRaisesRegex(ValueError, 'cannot have default values'):
2120 inspect.Parameter('a', default=42, 2423 inspect.Parameter('a', default=42,
2121 kind=inspect.Parameter.VAR_KEYWORD) 2424 kind=inspect.Parameter.VAR_KEYWORD)
2122 2425
2123 with self.assertRaisesRegex(ValueError, 'cannot have default values'): 2426 with self.assertRaisesRegex(ValueError, 'cannot have default values'):
2124 inspect.Parameter('a', default=42, 2427 inspect.Parameter('a', default=42,
2125 kind=inspect.Parameter.VAR_POSITIONAL) 2428 kind=inspect.Parameter.VAR_POSITIONAL)
2126 2429
2127 p = inspect.Parameter('a', default=42, 2430 p = inspect.Parameter('a', default=42,
(...skipping 29 matching lines...) Expand all
2157 2460
2158 p2 = p.replace(annotation=1) 2461 p2 = p.replace(annotation=1)
2159 self.assertEqual(p2.annotation, 1) 2462 self.assertEqual(p2.annotation, 1)
2160 p2 = p2.replace(annotation=p2.empty) 2463 p2 = p2.replace(annotation=p2.empty)
2161 self.assertEqual(p, p2) 2464 self.assertEqual(p, p2)
2162 2465
2163 p2 = p2.replace(name='bar') 2466 p2 = p2.replace(name='bar')
2164 self.assertEqual(p2.name, 'bar') 2467 self.assertEqual(p2.name, 'bar')
2165 self.assertNotEqual(p2, p) 2468 self.assertNotEqual(p2, p)
2166 2469
2167 with self.assertRaisesRegex(ValueError, 'not a valid parameter name'): 2470 with self.assertRaisesRegex(ValueError,
2471 'name is a required attribute'):
2168 p2 = p2.replace(name=p2.empty) 2472 p2 = p2.replace(name=p2.empty)
2169 2473
2170 p2 = p2.replace(name='foo', default=None) 2474 p2 = p2.replace(name='foo', default=None)
2171 self.assertIsNone(p2.default) 2475 self.assertIsNone(p2.default)
2172 self.assertNotEqual(p2, p) 2476 self.assertNotEqual(p2, p)
2173 2477
2174 p2 = p2.replace(name='foo', default=p2.empty) 2478 p2 = p2.replace(name='foo', default=p2.empty)
2175 self.assertIs(p2.default, p2.empty) 2479 self.assertIs(p2.default, p2.empty)
2176 2480
2177 2481
2178 p2 = p2.replace(default=42, kind=p2.POSITIONAL_OR_KEYWORD) 2482 p2 = p2.replace(default=42, kind=p2.POSITIONAL_OR_KEYWORD)
2179 self.assertEqual(p2.kind, p2.POSITIONAL_OR_KEYWORD) 2483 self.assertEqual(p2.kind, p2.POSITIONAL_OR_KEYWORD)
2180 self.assertNotEqual(p2, p) 2484 self.assertNotEqual(p2, p)
2181 2485
2182 with self.assertRaisesRegex(ValueError, 'invalid value for'): 2486 with self.assertRaisesRegex(ValueError, 'invalid value for'):
2183 p2 = p2.replace(kind=p2.empty) 2487 p2 = p2.replace(kind=p2.empty)
2184 2488
2185 p2 = p2.replace(kind=p2.KEYWORD_ONLY) 2489 p2 = p2.replace(kind=p2.KEYWORD_ONLY)
2186 self.assertEqual(p2, p) 2490 self.assertEqual(p2, p)
2187 2491
2188 def test_signature_parameter_positional_only(self): 2492 def test_signature_parameter_positional_only(self):
2189 p = inspect.Parameter(None, kind=inspect.Parameter.POSITIONAL_ONLY) 2493 with self.assertRaisesRegex(TypeError, 'name must be a str'):
2190 self.assertEqual(str(p), '<>') 2494 inspect.Parameter(None, kind=inspect.Parameter.POSITIONAL_ONLY)
2191
2192 p = p.replace(name='1')
2193 self.assertEqual(str(p), '<1>')
2194 2495
2195 def test_signature_parameter_immutability(self): 2496 def test_signature_parameter_immutability(self):
2196 p = inspect.Parameter(None, kind=inspect.Parameter.POSITIONAL_ONLY) 2497 p = inspect.Parameter('spam', kind=inspect.Parameter.KEYWORD_ONLY)
2197 2498
2198 with self.assertRaises(AttributeError): 2499 with self.assertRaises(AttributeError):
2199 p.foo = 'bar' 2500 p.foo = 'bar'
2200 2501
2201 with self.assertRaises(AttributeError): 2502 with self.assertRaises(AttributeError):
2202 p.kind = 123 2503 p.kind = 123
2203 2504
2204 2505
2205 class TestSignatureBind(unittest.TestCase): 2506 class TestSignatureBind(unittest.TestCase):
2206 @staticmethod 2507 @staticmethod
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
2384 sig = inspect.signature(test) 2685 sig = inspect.signature(test)
2385 new_params = collections.OrderedDict(tuple(sig.parameters.items())) 2686 new_params = collections.OrderedDict(tuple(sig.parameters.items()))
2386 for name in ('a_po', 'b_po', 'c_po'): 2687 for name in ('a_po', 'b_po', 'c_po'):
2387 new_params[name] = new_params[name].replace(kind=P.POSITIONAL_ONLY) 2688 new_params[name] = new_params[name].replace(kind=P.POSITIONAL_ONLY)
2388 new_sig = sig.replace(parameters=new_params.values()) 2689 new_sig = sig.replace(parameters=new_params.values())
2389 test.__signature__ = new_sig 2690 test.__signature__ = new_sig
2390 2691
2391 self.assertEqual(self.call(test, 1, 2, 4, 5, bar=6), 2692 self.assertEqual(self.call(test, 1, 2, 4, 5, bar=6),
2392 (1, 2, 4, 5, 6, {})) 2693 (1, 2, 4, 5, 6, {}))
2393 2694
2695 self.assertEqual(self.call(test, 1, 2),
2696 (1, 2, 3, 42, 50, {}))
2697
2698 self.assertEqual(self.call(test, 1, 2, foo=4, bar=5),
2699 (1, 2, 3, 4, 5, {}))
2700
2701 with self.assertRaisesRegex(TypeError, "but was passed as a keyword"):
2702 self.call(test, 1, 2, foo=4, bar=5, c_po=10)
2703
2394 with self.assertRaisesRegex(TypeError, "parameter is positional only"): 2704 with self.assertRaisesRegex(TypeError, "parameter is positional only"):
2395 self.call(test, 1, 2, c_po=4) 2705 self.call(test, 1, 2, c_po=4)
2396 2706
2397 with self.assertRaisesRegex(TypeError, "parameter is positional only"): 2707 with self.assertRaisesRegex(TypeError, "parameter is positional only"):
2398 self.call(test, a_po=1, b_po=2) 2708 self.call(test, a_po=1, b_po=2)
2399 2709
2400 def test_signature_bind_with_self_arg(self): 2710 def test_signature_bind_with_self_arg(self):
2401 # Issue #17071: one of the parameters is named "self 2711 # Issue #17071: one of the parameters is named "self
2402 def test(a, self, b): 2712 def test(a, self, b):
2403 pass 2713 pass
2404 sig = inspect.signature(test) 2714 sig = inspect.signature(test)
2405 ba = sig.bind(1, 2, 3) 2715 ba = sig.bind(1, 2, 3)
2406 self.assertEqual(ba.args, (1, 2, 3)) 2716 self.assertEqual(ba.args, (1, 2, 3))
2407 ba = sig.bind(1, self=2, b=3) 2717 ba = sig.bind(1, self=2, b=3)
2408 self.assertEqual(ba.args, (1, 2, 3)) 2718 self.assertEqual(ba.args, (1, 2, 3))
2409 2719
2720 def test_signature_bind_vararg_name(self):
2721 def test(a, *args):
2722 return a, args
2723 sig = inspect.signature(test)
2724
2725 with self.assertRaisesRegex(TypeError, "too many keyword arguments"):
2726 sig.bind(a=0, args=1)
2727
2728 def test(*args, **kwargs):
2729 return args, kwargs
2730 self.assertEqual(self.call(test, args=1), ((), {'args': 1}))
2731
2732 sig = inspect.signature(test)
2733 ba = sig.bind(args=1)
2734 self.assertEqual(ba.arguments, {'kwargs': {'args': 1}})
2735
2410 2736
2411 class TestBoundArguments(unittest.TestCase): 2737 class TestBoundArguments(unittest.TestCase):
2412 def test_signature_bound_arguments_unhashable(self): 2738 def test_signature_bound_arguments_unhashable(self):
2413 def foo(a): pass 2739 def foo(a): pass
2414 ba = inspect.signature(foo).bind(1) 2740 ba = inspect.signature(foo).bind(1)
2415 2741
2416 with self.assertRaisesRegex(TypeError, 'unhashable type'): 2742 with self.assertRaisesRegex(TypeError, 'unhashable type'):
2417 hash(ba) 2743 hash(ba)
2418 2744
2419 def test_signature_bound_arguments_equality(self): 2745 def test_signature_bound_arguments_equality(self):
2420 def foo(a): pass 2746 def foo(a): pass
2421 ba = inspect.signature(foo).bind(1) 2747 ba = inspect.signature(foo).bind(1)
2422 self.assertEqual(ba, ba) 2748 self.assertEqual(ba, ba)
2423 2749
2424 ba2 = inspect.signature(foo).bind(1) 2750 ba2 = inspect.signature(foo).bind(1)
2425 self.assertEqual(ba, ba2) 2751 self.assertEqual(ba, ba2)
2426 2752
2427 ba3 = inspect.signature(foo).bind(2) 2753 ba3 = inspect.signature(foo).bind(2)
2428 self.assertNotEqual(ba, ba3) 2754 self.assertNotEqual(ba, ba3)
2429 ba3.arguments['a'] = 1 2755 ba3.arguments['a'] = 1
2430 self.assertEqual(ba, ba3) 2756 self.assertEqual(ba, ba3)
2431 2757
2432 def bar(b): pass 2758 def bar(b): pass
2433 ba4 = inspect.signature(bar).bind(1) 2759 ba4 = inspect.signature(bar).bind(1)
2434 self.assertNotEqual(ba, ba4) 2760 self.assertNotEqual(ba, ba4)
2761
2762
2763 class TestSignaturePrivateHelpers(unittest.TestCase):
2764 def test_signature_get_bound_param(self):
2765 getter = inspect._signature_get_bound_param
2766
2767 self.assertEqual(getter('($self)'), 'self')
2768 self.assertEqual(getter('($self, obj)'), 'self')
2769 self.assertEqual(getter('($cls, /, obj)'), 'cls')
2435 2770
2436 2771
2437 class TestUnwrap(unittest.TestCase): 2772 class TestUnwrap(unittest.TestCase):
2438 2773
2439 def test_unwrap_one(self): 2774 def test_unwrap_one(self):
2440 def func(a, b): 2775 def func(a, b):
2441 return a + b 2776 return a + b
2442 wrapper = functools.lru_cache(maxsize=20)(func) 2777 wrapper = functools.lru_cache(maxsize=20)(func)
2443 self.assertIs(inspect.unwrap(wrapper), func) 2778 self.assertIs(inspect.unwrap(wrapper), func)
2444 2779
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
2518 self.assertEqual(lines, ["Can't get info for builtin modules."]) 2853 self.assertEqual(lines, ["Can't get info for builtin modules."])
2519 2854
2520 def test_details(self): 2855 def test_details(self):
2521 module = importlib.import_module('unittest') 2856 module = importlib.import_module('unittest')
2522 rc, out, err = assert_python_ok('-m', 'inspect', 2857 rc, out, err = assert_python_ok('-m', 'inspect',
2523 'unittest', '--details') 2858 'unittest', '--details')
2524 output = out.decode() 2859 output = out.decode()
2525 # Just a quick sanity check on the output 2860 # Just a quick sanity check on the output
2526 self.assertIn(module.__name__, output) 2861 self.assertIn(module.__name__, output)
2527 self.assertIn(module.__file__, output) 2862 self.assertIn(module.__file__, output)
2528 self.assertIn(module.__cached__, output) 2863 if not sys.flags.optimize:
2864 self.assertIn(module.__cached__, output)
2529 self.assertEqual(err, b'') 2865 self.assertEqual(err, b'')
2530 2866
2531 2867
2532 2868
2533 2869
2534 def test_main(): 2870 def test_main():
2535 run_unittest( 2871 run_unittest(
2536 TestDecorators, TestRetrievingSourceCode, TestOneliners, TestBuggyCases, 2872 TestDecorators, TestRetrievingSourceCode, TestOneliners, TestBuggyCases,
2537 TestInterpreterStack, TestClassesAndFunctions, TestPredicates, 2873 TestInterpreterStack, TestClassesAndFunctions, TestPredicates,
2538 TestGetcallargsFunctions, TestGetcallargsMethods, 2874 TestGetcallargsFunctions, TestGetcallargsMethods,
2539 TestGetcallargsUnboundMethods, TestGetattrStatic, TestGetGeneratorState, 2875 TestGetcallargsUnboundMethods, TestGetattrStatic, TestGetGeneratorState,
2540 TestNoEOL, TestSignatureObject, TestSignatureBind, TestParameterObject, 2876 TestNoEOL, TestSignatureObject, TestSignatureBind, TestParameterObject,
2541 TestBoundArguments, TestGetClosureVars, TestUnwrap, TestMain 2877 TestBoundArguments, TestSignaturePrivateHelpers, TestGetClosureVars,
2878 TestUnwrap, TestMain
2542 ) 2879 )
2543 2880
2544 if __name__ == "__main__": 2881 if __name__ == "__main__":
2545 test_main() 2882 test_main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+