6161assert d1 .keys ().isdisjoint (d2 .keys ())
6262d2 = {"b" : 2 , "d" : 4 }
6363assert not d1 .items ().isdisjoint (d2 .items ())
64- assert not d1 .keys ().isdisjoint (d2 .keys ())
64+ assert not d1 .keys ().isdisjoint (d2 .keys ())
65+
66+
67+ assert dict (a = 2 , b = 3 ) == {'a' : 2 , 'b' : 3 }
68+ assert dict ({'a' : 2 , 'b' : 3 }, b = 4 ) == {'a' : 2 , 'b' : 4 }
69+ assert dict ([('a' , 2 ), ('b' , 3 )]) == {'a' : 2 , 'b' : 3 }
70+
71+ assert {} == {}
72+ assert not {'a' : 2 } == {}
73+ assert not {} == {'a' : 2 }
74+ assert not {'b' : 2 } == {'a' : 2 }
75+ assert not {'a' : 4 } == {'a' : 2 }
76+ assert {'a' : 2 } == {'a' : 2 }
77+
78+ nan = float ('nan' )
79+ assert {'a' : nan } == {'a' : nan }
80+
81+ a = {'g' : 5 }
82+ b = {'a' : a , 'd' : 9 }
83+ c = dict (b )
84+ c ['d' ] = 3
85+ c ['a' ]['g' ] = 2
86+ assert a == {'g' : 2 }
87+ assert b == {'a' : a , 'd' : 9 }
88+
89+ a .clear ()
90+ assert len (a ) == 0
91+
92+ a = {'a' : 5 , 'b' : 6 }
93+ res = set ()
94+ for value in a .values ():
95+ res .add (value )
96+ assert res == set ([5 ,6 ])
97+
98+ count = 0
99+ for (key , value ) in a .items ():
100+ assert a [key ] == value
101+ count += 1
102+ assert count == len (a )
103+
104+ res = set ()
105+ for key in a .keys ():
106+ res .add (key )
107+ assert res == set (['a' ,'b' ])
108+
109+ # Deleted values are correctly skipped over:
110+ x = {'a' : 1 , 'b' : 2 , 'c' : 3 , 'd' : 3 }
111+ del x ['c' ]
112+ it = iter (x .items ())
113+ assert ('a' , 1 ) == next (it )
114+ assert ('b' , 2 ) == next (it )
115+ assert ('d' , 3 ) == next (it )
116+ with assert_raises (StopIteration ):
117+ next (it )
118+
119+ with assert_raises (KeyError ) as cm :
120+ del x [10 ]
121+ assert cm .exception .args [0 ] == 10
122+
123+ # Iterating a dictionary is just its keys:
124+ assert ['a' , 'b' , 'd' ] == list (x )
125+
126+ # Iterating view captures dictionary when iterated.
127+ data = {1 : 2 , 3 : 4 }
128+ items = data .items ()
129+ assert list (items ) == [(1 , 2 ), (3 , 4 )]
130+ data [5 ] = 6
131+ assert list (items ) == [(1 , 2 ), (3 , 4 ), (5 , 6 )]
132+
133+ # Values can be changed during iteration.
134+ data = {1 : 2 , 3 : 4 }
135+ items = iter (data .items ())
136+ assert (1 , 2 ) == next (items )
137+ data [3 ] = "changed"
138+ assert (3 , "changed" ) == next (items )
139+
140+ # But we can't add or delete items during iteration.
141+ d = {}
142+ a = iter (d .items ())
143+ d ['a' ] = 2
144+ b = iter (d .items ())
145+ assert ('a' , 2 ) == next (b )
146+ with assert_raises (RuntimeError ):
147+ next (a )
148+ del d ['a' ]
149+ with assert_raises (RuntimeError ):
150+ next (b )
151+
152+ # View isn't itself an iterator.
153+ with assert_raises (TypeError ):
154+ next (data .keys ())
155+
156+ assert len (data .keys ()) == 2
157+
158+ x = {}
159+ x [1 ] = 1
160+ assert x [1 ] == 1
161+
162+ x [7 ] = 7
163+ x [2 ] = 2
164+ x [(5 , 6 )] = 5
165+
166+ with assert_raises (TypeError ):
167+ x [[]] # Unhashable type.
168+
169+ x ["here" ] = "here"
170+ assert x .get ("not here" , "default" ) == "default"
171+ assert x .get ("here" , "default" ) == "here"
172+ assert x .get ("not here" ) == None
173+
174+ class LengthDict (dict ):
175+ def __getitem__ (self , k ):
176+ return len (k )
177+
178+ x = LengthDict ()
179+ assert type (x ) == LengthDict
180+ assert x ['word' ] == 4
181+ assert x .get ('word' ) is None
182+
183+ assert 5 == eval ("a + word" , LengthDict ())
184+
185+
186+ class Squares (dict ):
187+ def __missing__ (self , k ):
188+ v = k * k
189+ self [k ] = v
190+ return v
191+
192+ x = Squares ()
193+ assert x [- 5 ] == 25
194+
195+ # An object that hashes to the same value always, and compares equal if any its values match.
196+ class Hashable (object ):
197+ def __init__ (self , * args ):
198+ self .values = args
199+ def __hash__ (self ):
200+ return 1
201+ def __eq__ (self , other ):
202+ for x in self .values :
203+ for y in other .values :
204+ if x == y :
205+ return True
206+ return False
207+
208+ x = {}
209+ x [Hashable (1 ,2 )] = 8
210+
211+ assert x [Hashable (1 ,2 )] == 8
212+ assert x [Hashable (3 ,1 )] == 8
213+
214+ x [Hashable (8 )] = 19
215+ x [Hashable (19 ,8 )] = 1
216+ assert x [Hashable (8 )] == 1
217+ assert len (x ) == 2
218+
219+ assert list ({'a' : 2 , 'b' : 10 }) == ['a' , 'b' ]
220+ x = {}
221+ x ['a' ] = 2
222+ x ['b' ] = 10
223+ assert list (x ) == ['a' , 'b' ]
224+
225+ y = x .copy ()
226+ x ['c' ] = 12
227+ assert y == {'a' : 2 , 'b' : 10 }
228+
229+ y .update ({'c' : 19 , "d" : - 1 , 'b' : 12 })
230+ assert y == {'a' : 2 , 'b' : 12 , 'c' : 19 , 'd' : - 1 }
231+
232+ y .update (y )
233+ assert y == {'a' : 2 , 'b' : 12 , 'c' : 19 , 'd' : - 1 } # hasn't changed
234+
235+ # KeyError has object that used as key as an .args[0]
236+ with assert_raises (KeyError ) as cm :
237+ x ['not here' ]
238+ assert cm .exception .args [0 ] == "not here"
239+ with assert_raises (KeyError ) as cm :
240+ x .pop ('not here' )
241+ assert cm .exception .args [0 ] == "not here"
242+
243+ with assert_raises (KeyError ) as cm :
244+ x [10 ]
245+ assert cm .exception .args [0 ] == 10
246+ with assert_raises (KeyError ) as cm :
247+ x .pop (10 )
248+ assert cm .exception .args [0 ] == 10
249+
250+ class MyClass : pass
251+ obj = MyClass ()
252+
253+ with assert_raises (KeyError ) as cm :
254+ x [obj ]
255+ assert cm .exception .args [0 ] == obj
256+ with assert_raises (KeyError ) as cm :
257+ x .pop (obj )
258+ assert cm .exception .args [0 ] == obj
259+
260+ x = {1 : 'a' , '1' : None }
261+ assert x .pop (1 ) == 'a'
262+ assert x .pop ('1' ) is None
263+ assert x == {}
264+
265+ x = {1 : 'a' }
266+ assert (1 , 'a' ) == x .popitem ()
267+ assert x == {}
268+ with assert_raises (KeyError ) as cm :
269+ x .popitem ()
270+ assert cm .exception .args == ('popitem(): dictionary is empty' ,)
271+
272+ x = {'a' : 4 }
273+ assert 4 == x .setdefault ('a' , 0 )
274+ assert x ['a' ] == 4
275+ assert 0 == x .setdefault ('b' , 0 )
276+ assert x ['b' ] == 0
277+ assert None == x .setdefault ('c' )
278+ assert x ['c' ] is None
279+
280+ assert {1 : None , "b" : None } == dict .fromkeys ([1 , "b" ])
281+ assert {1 : 0 , "b" : 0 } == dict .fromkeys ([1 , "b" ], 0 )
282+
283+ x = {'a' : 1 , 'b' : 1 , 'c' : 1 }
284+ y = {'b' : 2 , 'c' : 2 , 'd' : 2 }
285+ z = {'c' : 3 , 'd' : 3 , 'e' : 3 }
286+
287+ w = {1 : 1 , ** x , 2 : 2 , ** y , 3 : 3 , ** z , 4 : 4 }
288+ assert w == {1 : 1 , 'a' : 1 , 'b' : 2 , 'c' : 3 , 2 : 2 , 'd' : 3 , 3 : 3 , 'e' : 3 , 4 : 4 }
289+
290+ assert str ({True : True , 1.0 : 1.0 }) == str ({True : 1.0 })
291+
292+ class A :
293+ def __hash__ (self ):
294+ return 1
295+ def __eq__ (self , other ):
296+ return isinstance (other , A )
297+ class B :
298+ def __hash__ (self ):
299+ return 1
300+ def __eq__ (self , other ):
301+ return isinstance (other , B )
302+
303+ s = {1 : 0 , A (): 1 , B (): 2 }
304+ assert len (s ) == 3
305+ assert s [1 ] == 0
306+ assert s [A ()] == 1
307+ assert s [B ()] == 2
308+
309+ # Test dict usage in set with star expressions!
310+ a = {'bla' : 2 }
311+ b = {'c' : 44 , 'bla' : 332 , 'd' : 6 }
312+ x = ['bla' , 'c' , 'd' , 'f' ]
313+ c = {* a , * b , * x }
314+ # print(c, type(c))
315+ assert isinstance (c , set )
316+ assert c == {'bla' , 'c' , 'd' , 'f' }
317+
318+ assert not {}.__ne__ ({})
319+ assert {}.__ne__ ({'a' :'b' })
320+ assert {}.__ne__ (1 ) == NotImplemented
321+
322+ it = iter ({0 : 1 , 2 : 3 , 4 :5 , 6 :7 })
323+ assert it .__length_hint__ () == 4
324+ next (it )
325+ assert it .__length_hint__ () == 3
326+ next (it )
327+ assert it .__length_hint__ () == 2
328+ next (it )
329+ assert it .__length_hint__ () == 1
330+ next (it )
331+ assert it .__length_hint__ () == 0
332+ assert_raises (StopIteration , next , it )
333+ assert it .__length_hint__ () == 0
0 commit comments