@@ -2229,16 +2229,57 @@ def __eq__(self, other):
22292229class SubclassWithKwargsTest (unittest .TestCase ):
22302230 def test_keywords_in_subclass (self ):
22312231 # count is not subclassable...
2232- for cls in (repeat , zip , filter , filterfalse , chain , map ,
2233- starmap , islice , takewhile , dropwhile , cycle , compress ):
2234- class Subclass (cls ):
2235- def __init__ (self , newarg = None , * args ):
2236- cls .__init__ (self , * args )
2237- try :
2238- Subclass (newarg = 1 )
2239- except TypeError as err :
2240- # we expect type errors because of wrong argument count
2241- self .assertNotIn ("keyword arguments" , err .args [0 ])
2232+ testcases = [
2233+ (repeat , (1 , 2 ), [1 , 1 ]),
2234+ (zip , ([1 , 2 ], 'ab' ), [(1 , 'a' ), (2 , 'b' )]),
2235+ (filter , (None , [0 , 1 ]), [1 ]),
2236+ (filterfalse , (None , [0 , 1 ]), [0 ]),
2237+ (chain , ([1 , 2 ], [3 , 4 ]), [1 , 2 , 3 ]),
2238+ (map , (str , [1 , 2 ]), ['1' , '2' ]),
2239+ (starmap , (operator .pow , ((2 , 3 ), (3 , 2 ))), [8 , 9 ]),
2240+ (islice , ([1 , 2 , 3 , 4 ], 1 , 3 ), [2 , 3 ]),
2241+ (takewhile , (isEven , [2 , 3 , 4 ]), [2 ]),
2242+ (dropwhile , (isEven , [2 , 3 , 4 ]), [3 , 4 ]),
2243+ (cycle , ([1 , 2 ],), [1 , 2 , 1 ]),
2244+ (compress , ('ABC' , [1 , 0 , 1 ]), ['A' , 'C' ]),
2245+ ]
2246+ for cls , args , result in testcases :
2247+ with self .subTest (cls ):
2248+ class subclass (cls ):
2249+ pass
2250+ u = subclass (* args )
2251+ self .assertIs (type (u ), subclass )
2252+ self .assertEqual (list (islice (u , 0 , 3 )), result )
2253+ with self .assertRaises (TypeError ):
2254+ subclass (* args , newarg = 3 )
2255+
2256+ for cls , args , result in testcases :
2257+ # Constructors of repeat, zip, compress accept keyword arguments.
2258+ # Their subclasses need overriding __new__ to support new
2259+ # keyword arguments.
2260+ if cls in [repeat , zip , compress ]:
2261+ continue
2262+ with self .subTest (cls ):
2263+ class subclass_with_init (cls ):
2264+ def __init__ (self , * args , newarg = None ):
2265+ self .newarg = newarg
2266+ u = subclass_with_init (* args , newarg = 3 )
2267+ self .assertIs (type (u ), subclass_with_init )
2268+ self .assertEqual (list (islice (u , 0 , 3 )), result )
2269+ self .assertEqual (u .newarg , 3 )
2270+
2271+ for cls , args , result in testcases :
2272+ with self .subTest (cls ):
2273+ class subclass_with_new (cls ):
2274+ def __new__ (cls , * args , newarg = None ):
2275+ self = super ().__new__ (cls , * args )
2276+ self .newarg = newarg
2277+ return self
2278+ u = subclass_with_new (* args , newarg = 3 )
2279+ self .assertIs (type (u ), subclass_with_new )
2280+ self .assertEqual (list (islice (u , 0 , 3 )), result )
2281+ self .assertEqual (u .newarg , 3 )
2282+
22422283
22432284@support .cpython_only
22442285class SizeofTest (unittest .TestCase ):
0 commit comments