summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_inspect.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index cb51f8a..1a856f6 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -57,6 +57,31 @@ def revise(filename, *args):
git = mod.StupidGit()
+
+def signatures_with_lexicographic_keyword_only_parameters():
+ """
+ Yields a whole bunch of functions with only keyword-only parameters,
+ where those parameters are always in lexicographically sorted order.
+ """
+ parameters = ['a', 'bar', 'c', 'delta', 'ephraim', 'magical', 'yoyo', 'z']
+ for i in range(1, 2**len(parameters)):
+ p = []
+ bit = 1
+ for j in range(len(parameters)):
+ if i & (bit << j):
+ p.append(parameters[j])
+ fn_text = "def foo(*, " + ", ".join(p) + "): pass"
+ symbols = {}
+ exec(fn_text, symbols, symbols)
+ yield symbols['foo']
+
+
+def unsorted_keyword_only_parameters_fn(*, throw, out, the, baby, with_,
+ the_, bathwater):
+ pass
+
+unsorted_keyword_only_parameters = 'throw out the baby with_ the_ bathwater'.split()
+
class IsTestBase(unittest.TestCase):
predicates = set([inspect.isbuiltin, inspect.isclass, inspect.iscode,
inspect.isframe, inspect.isfunction, inspect.ismethod,
@@ -829,6 +854,17 @@ class TestClassesAndFunctions(unittest.TestCase):
with self.assertRaises(TypeError):
inspect.getfullargspec(builtin)
+ def test_getfullargspec_definition_order_preserved_on_kwonly(self):
+ for fn in signatures_with_lexicographic_keyword_only_parameters():
+ signature = inspect.getfullargspec(fn)
+ l = list(signature.kwonlyargs)
+ sorted_l = sorted(l)
+ self.assertTrue(l)
+ self.assertEqual(l, sorted_l)
+ signature = inspect.getfullargspec(unsorted_keyword_only_parameters_fn)
+ l = list(signature.kwonlyargs)
+ self.assertEqual(l, unsorted_keyword_only_parameters)
+
def test_getargspec_method(self):
class A(object):
def m(self):
@@ -2969,6 +3005,17 @@ class TestSignatureObject(unittest.TestCase):
sig = MySignature.from_callable(_pickle.Pickler)
self.assertTrue(isinstance(sig, MySignature))
+ def test_signature_definition_order_preserved_on_kwonly(self):
+ for fn in signatures_with_lexicographic_keyword_only_parameters():
+ signature = inspect.signature(fn)
+ l = list(signature.parameters)
+ sorted_l = sorted(l)
+ self.assertTrue(l)
+ self.assertEqual(l, sorted_l)
+ signature = inspect.signature(unsorted_keyword_only_parameters_fn)
+ l = list(signature.parameters)
+ self.assertEqual(l, unsorted_keyword_only_parameters)
+
class TestParameterObject(unittest.TestCase):
def test_signature_parameter_kinds(self):