@@ -41,6 +41,54 @@ def fn(**kw):
4141 self .assertIsInstance (res , dict )
4242 self .assertEqual (list (res .items ()), expected )
4343
44+ def test_kwargs_order_preserved (self ):
45+ # PEP 468: Preserving Keyword Argument Order
46+ def fn (** kw ):
47+ return list (kw )
48+
49+ self .assertEqual (fn (b = 1 , a = 2 , c = 3 ), ['b' , 'a' , 'c' ])
50+ self .assertEqual (fn (c = 3 , a = 2 , b = 1 ), ['c' , 'a' , 'b' ])
51+ # Unpacked mappings are merged in place, keeping their own order.
52+ self .assertEqual (fn (z = 0 , ** {'x' : 1 , 'a' : 2 }, y = 3 ),
53+ ['z' , 'x' , 'a' , 'y' ])
54+ self .assertEqual (fn (** {'b' : 1 }, ** {'a' : 2 }), ['b' , 'a' ])
55+ # Named parameters are removed from **kwargs without reordering
56+ # the rest.
57+ def fn2 (a , c = None , ** kw ):
58+ return list (kw )
59+
60+ self .assertEqual (fn2 (d = 1 , a = 2 , b = 3 , c = 4 , e = 5 ), ['d' , 'b' , 'e' ])
61+
62+ def test_kwargs_order_preserved_in_methods (self ):
63+ # PEP 468: Preserving Keyword Argument Order
64+ class C :
65+ def __init__ (self , ** kw ):
66+ self .init_kw = list (kw )
67+
68+ def meth (self , ** kw ):
69+ return list (kw )
70+
71+ @classmethod
72+ def cmeth (cls , ** kw ):
73+ return list (kw )
74+
75+ @staticmethod
76+ def smeth (** kw ):
77+ return list (kw )
78+
79+ c = C (b = 1 , a = 2 , c = 3 )
80+ self .assertEqual (c .init_kw , ['b' , 'a' , 'c' ])
81+ self .assertEqual (c .meth (b = 1 , a = 2 , c = 3 ), ['b' , 'a' , 'c' ])
82+ self .assertEqual (C .cmeth (b = 1 , a = 2 , c = 3 ), ['b' , 'a' , 'c' ])
83+ self .assertEqual (C .smeth (b = 1 , a = 2 , c = 3 ), ['b' , 'a' , 'c' ])
84+
85+ def test_kwargs_order_preserved_in_c_functions (self ):
86+ # PEP 468: Preserving Keyword Argument Order
87+ self .assertEqual (list (dict (b = 1 , a = 2 , c = 3 )), ['b' , 'a' , 'c' ])
88+ self .assertEqual (list (dict (** {'b' : 1 }, a = 2 )), ['b' , 'a' ])
89+ self .assertEqual (list (collections .OrderedDict (b = 1 , a = 2 , c = 3 )),
90+ ['b' , 'a' , 'c' ])
91+
4492 def test_frames_are_popped_after_failed_calls (self ):
4593 # GH-93252: stuff blows up if we don't pop the new frame after
4694 # recovering from failed calls:
0 commit comments