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