@@ -1787,6 +1787,44 @@ def test_hash(self):
17871787 with self .assertRaisesRegex (TypeError , "unhashable type: 'list'" ):
17881788 hash (fd )
17891789
1790+ def test_fromkeys (self ):
1791+ self .assertEqual (frozendict .fromkeys ('abc' ),
1792+ frozendict (a = None , b = None , c = None ))
1793+
1794+ # Subclass which overrides the constructor
1795+ created = frozendict (x = 1 )
1796+ class FrozenDictSubclass (frozendict ):
1797+ def __new__ (self ):
1798+ return created
1799+
1800+ fd = FrozenDictSubclass .fromkeys ("abc" )
1801+ self .assertEqual (fd , frozendict (x = 1 , a = None , b = None , c = None ))
1802+ self .assertEqual (type (fd ), FrozenDictSubclass )
1803+ self .assertEqual (created , frozendict (x = 1 ))
1804+
1805+ fd = FrozenDictSubclass .fromkeys (frozendict (y = 2 ))
1806+ self .assertEqual (fd , frozendict (x = 1 , y = None ))
1807+ self .assertEqual (type (fd ), FrozenDictSubclass )
1808+ self .assertEqual (created , frozendict (x = 1 ))
1809+
1810+ # Subclass which doesn't override the constructor
1811+ class FrozenDictSubclass2 (frozendict ):
1812+ pass
1813+
1814+ fd = FrozenDictSubclass2 .fromkeys ("abc" )
1815+ self .assertEqual (fd , frozendict (a = None , b = None , c = None ))
1816+ self .assertEqual (type (fd ), FrozenDictSubclass2 )
1817+
1818+ # Dict subclass which overrides the constructor
1819+ class DictSubclass (dict ):
1820+ def __new__ (self ):
1821+ return created
1822+
1823+ fd = DictSubclass .fromkeys ("abc" )
1824+ self .assertEqual (fd , frozendict (x = 1 , a = None , b = None , c = None ))
1825+ self .assertEqual (type (fd ), DictSubclass )
1826+ self .assertEqual (created , frozendict (x = 1 ))
1827+
17901828
17911829if __name__ == "__main__" :
17921830 unittest .main ()
0 commit comments