diff options
author | larryhastings <larry@hastings.org> | 2018-01-28 19:13:09 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-28 19:13:09 (GMT) |
commit | f36ba12809d5db1b76464d8f1f04dad8d685ec78 (patch) | |
tree | 62de617e1bd3fc4609e1f95338755887db22f4b5 /Lib/test/test_inspect.py | |
parent | bec2372b7e1da5dfdbadaf242aa8e994b164cace (diff) | |
download | cpython-f36ba12809d5db1b76464d8f1f04dad8d685ec78.zip cpython-f36ba12809d5db1b76464d8f1f04dad8d685ec78.tar.gz cpython-f36ba12809d5db1b76464d8f1f04dad8d685ec78.tar.bz2 |
bpo-32697: Definition order of kwonly params is now guaranteed preserved. (#5391)
Definition order of kwonly params is now guaranteed preserved.
Diffstat (limited to 'Lib/test/test_inspect.py')
-rw-r--r-- | Lib/test/test_inspect.py | 47 |
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): |