@@ -57,6 +57,31 @@ def revise(filename, *args):
5757
5858git = mod .StupidGit ()
5959
60+
61+ def signatures_with_lexicographic_keyword_only_parameters ():
62+ """
63+ Yields a whole bunch of functions with only keyword-only parameters,
64+ where those parameters are always in lexicographically sorted order.
65+ """
66+ parameters = ['a' , 'bar' , 'c' , 'delta' , 'ephraim' , 'magical' , 'yoyo' , 'z' ]
67+ for i in range (1 , 2 ** len (parameters )):
68+ p = []
69+ bit = 1
70+ for j in range (len (parameters )):
71+ if i & (bit << j ):
72+ p .append (parameters [j ])
73+ fn_text = "def foo(*, " + ", " .join (p ) + "): pass"
74+ symbols = {}
75+ exec (fn_text , symbols , symbols )
76+ yield symbols ['foo' ]
77+
78+
79+ def unsorted_keyword_only_parameters_fn (* , throw , out , the , baby , with_ ,
80+ the_ , bathwater ):
81+ pass
82+
83+ unsorted_keyword_only_parameters = 'throw out the baby with_ the_ bathwater' .split ()
84+
6085class IsTestBase (unittest .TestCase ):
6186 predicates = set ([inspect .isbuiltin , inspect .isclass , inspect .iscode ,
6287 inspect .isframe , inspect .isfunction , inspect .ismethod ,
@@ -829,6 +854,17 @@ def test_getfullagrspec_builtin_func_no_signature(self):
829854 with self .assertRaises (TypeError ):
830855 inspect .getfullargspec (builtin )
831856
857+ def test_getfullargspec_definition_order_preserved_on_kwonly (self ):
858+ for fn in signatures_with_lexicographic_keyword_only_parameters ():
859+ signature = inspect .getfullargspec (fn )
860+ l = list (signature .kwonlyargs )
861+ sorted_l = sorted (l )
862+ self .assertTrue (l )
863+ self .assertEqual (l , sorted_l )
864+ signature = inspect .getfullargspec (unsorted_keyword_only_parameters_fn )
865+ l = list (signature .kwonlyargs )
866+ self .assertEqual (l , unsorted_keyword_only_parameters )
867+
832868 def test_getargspec_method (self ):
833869 class A (object ):
834870 def m (self ):
@@ -2969,6 +3005,17 @@ class MySignature(inspect.Signature): pass
29693005 sig = MySignature .from_callable (_pickle .Pickler )
29703006 self .assertTrue (isinstance (sig , MySignature ))
29713007
3008+ def test_signature_definition_order_preserved_on_kwonly (self ):
3009+ for fn in signatures_with_lexicographic_keyword_only_parameters ():
3010+ signature = inspect .signature (fn )
3011+ l = list (signature .parameters )
3012+ sorted_l = sorted (l )
3013+ self .assertTrue (l )
3014+ self .assertEqual (l , sorted_l )
3015+ signature = inspect .signature (unsorted_keyword_only_parameters_fn )
3016+ l = list (signature .parameters )
3017+ self .assertEqual (l , unsorted_keyword_only_parameters )
3018+
29723019
29733020class TestParameterObject (unittest .TestCase ):
29743021 def test_signature_parameter_kinds (self ):
0 commit comments