@@ -1477,58 +1477,60 @@ def testMessageClassName(self, message_module):
14771477 'TestAllTypes.NestedMessage' , nested .__class__ .__qualname__
14781478 )
14791479
1480+ def create_bool_to_int (self , message_module , ** kwargs ):
1481+ with self .assertRaises (TypeError ) as e :
1482+ m = message_module .TestAllTypes (** kwargs )
1483+ self .assertIn ('bool' , str (e .exception ))
1484+ return None
1485+
1486+ def assign_bool_to_int (self , msg , field_name , value ):
1487+ old_value = getattr (msg , field_name )
1488+ with self .assertRaises (TypeError ) as e :
1489+ setattr (msg , field_name , value )
1490+ self .assertIn ('bool' , str (e .exception ))
1491+ self .assertEqual (getattr (msg , field_name ), old_value )
1492+
1493+ def assign_bool_to_map_or_extension (self , msg , field_name , key , value ):
1494+ with self .assertRaises (TypeError ) as e :
1495+ getattr (msg , field_name )[key ] = value
1496+ self .assertIn ('bool' , str (e .exception ))
1497+
14801498 def testAssignBoolToEnum (self , message_module ):
1481- # TODO: change warning into error in 2026 Q1
1482- # with self.assertRaises(TypeError):
1483- with warnings .catch_warnings (record = True ) as w :
1484- m = message_module .TestAllTypes (optional_nested_enum = True )
1485- self .assertIn ('bool' , str (w [0 ].message ))
1486- self .assertEqual (m .optional_nested_enum , 1 )
1499+ m = self .create_bool_to_int (message_module , optional_nested_enum = True )
1500+ if m is not None :
1501+ self .assertEqual (m .optional_nested_enum , 1 )
14871502
14881503 m = message_module .TestAllTypes (optional_nested_enum = 2 )
1489- with warnings .catch_warnings (record = True ) as w :
1490- m .optional_nested_enum = True
1491- self .assertIn ('bool' , str (w [0 ].message ))
1492- self .assertEqual (m .optional_nested_enum , 1 )
1504+ self .assign_bool_to_int (m , 'optional_nested_enum' , True )
14931505
14941506 with warnings .catch_warnings (record = True ) as w :
14951507 m .optional_nested_enum = 2
14961508 self .assertFalse (w )
14971509 self .assertEqual (m .optional_nested_enum , 2 )
14981510
14991511 def testBoolToRepeatedEnum (self , message_module ):
1500- with warnings .catch_warnings (record = True ) as w :
1501- m = message_module .TestAllTypes (repeated_nested_enum = [True ])
1502- self .assertIn ('bool' , str (w [0 ].message ))
1503- self .assertEqual (m .repeated_nested_enum , [1 ])
1512+ m = self .create_bool_to_int (message_module , repeated_nested_enum = [True ])
1513+ if m is not None :
1514+ self .assertEqual (m .repeated_nested_enum , [1 ])
15041515
15051516 m = message_module .TestAllTypes ()
1506- with warnings . catch_warnings ( record = True ) as w :
1507- m . repeated_nested_enum . append ( True )
1508- self .assertIn ('bool' , str (w [ 0 ]. message ))
1509- self .assertEqual (m .repeated_nested_enum , [1 ])
1517+ with self . assertRaises ( TypeError ) as e :
1518+ m = message_module . TestAllTypes ( repeated_nested_enum = [ True ] )
1519+ self .assertIn ('bool' , str (e . exception ))
1520+ self .assertEqual (m .repeated_nested_enum , [])
15101521
15111522 def testBoolToOneofEnum (self , message_module ):
15121523 m = unittest_pb2 .TestOneof2 ()
1513- with warnings .catch_warnings (record = True ) as w :
1514- m .foo_enum = True
1515- self .assertIn ('bool' , str (w [0 ].message ))
1516- self .assertEqual (m .foo_enum , 1 )
1524+ self .assign_bool_to_int (m , 'foo_enum' , True )
15171525
15181526 def testBoolToMapEnum (self , message_module ):
15191527 m = map_unittest_pb2 .TestMap ()
1520- with warnings .catch_warnings (record = True ) as w :
1521- m .map_int32_enum [10 ] = True
1522- self .assertIn ('bool' , str (w [0 ].message ))
1523- self .assertEqual (m .map_int32_enum [10 ], 1 )
1528+ self .assign_bool_to_map_or_extension (m , 'map_int32_enum' , 10 , True )
15241529
15251530 def testBoolToExtensionEnum (self , message_module ):
15261531 m = unittest_pb2 .TestAllExtensions ()
1527- with warnings .catch_warnings (record = True ) as w :
1528- m .Extensions [unittest_pb2 .optional_nested_enum_extension ] = True
1529- self .assertIn ('bool' , str (w [0 ].message ))
1530- self .assertEqual (
1531- m .Extensions [unittest_pb2 .optional_nested_enum_extension ], 1
1532+ self .assign_bool_to_map_or_extension (
1533+ m , 'Extensions' , unittest_pb2 .optional_nested_enum_extension , True
15321534 )
15331535
15341536 def testClosedEnumExtension (self , message_module ):
@@ -1547,59 +1549,43 @@ def testClosedEnumExtension(self, message_module):
15471549 )
15481550
15491551 def testAssignBoolToInt (self , message_module ):
1550- with warnings .catch_warnings (record = True ) as w :
1551- m = message_module .TestAllTypes (optional_int32 = True )
1552- self .assertIn ('bool' , str (w [0 ].message ))
1553- self .assertEqual (m .optional_int32 , 1 )
1552+ m = self .create_bool_to_int (message_module , optional_int32 = True )
1553+ if m is not None :
1554+ self .assertEqual (m .optional_int32 , 1 )
15541555
15551556 m = message_module .TestAllTypes (optional_uint32 = 123 )
1556- with warnings .catch_warnings (record = True ) as w :
1557- m .optional_uint32 = True
1558- self .assertIn ('bool' , str (w [0 ].message ))
1559- self .assertEqual (m .optional_uint32 , 1 )
1557+ self .assign_bool_to_int (m , 'optional_uint32' , True )
15601558
15611559 with warnings .catch_warnings (record = True ) as w :
15621560 m .optional_uint32 = 321
15631561 self .assertFalse (w )
15641562 self .assertEqual (m .optional_uint32 , 321 )
15651563
15661564 def testAssignBoolToRepeatedInt (self , message_module ):
1567- with warnings .catch_warnings (record = True ) as w :
1568- m = message_module .TestAllTypes (repeated_int64 = [True ])
1569- self .assertIn ('bool' , str (w [0 ].message ))
1570- self .assertEqual (m .repeated_int64 , [1 ])
1565+ m = self .create_bool_to_int (message_module , repeated_int64 = [True ])
1566+ if m is not None :
1567+ self .assertEqual (m .repeated_int64 , [1 ])
15711568
15721569 m = message_module .TestAllTypes ()
1573- with warnings . catch_warnings ( record = True ) as w :
1570+ with self . assertRaises ( TypeError ) as e :
15741571 m .repeated_int64 .append (True )
1575- self .assertIn ('bool' , str (w [ 0 ]. message ))
1576- self .assertEqual (m .repeated_int64 , [1 ])
1572+ self .assertIn ('bool' , str (e . exception ))
1573+ self .assertEqual (m .repeated_int64 , [])
15771574
15781575 def testAssignBoolToOneofInt (self , message_module ):
15791576 m = unittest_pb2 .TestOneof2 ()
1580- with warnings .catch_warnings (record = True ) as w :
1581- m .foo_int = True
1582- self .assertIn ('bool' , str (w [0 ].message ))
1583- self .assertEqual (m .foo_int , 1 )
1577+ self .assign_bool_to_int (m , 'foo_int' , True )
15841578
15851579 def testAssignBoolToMapInt (self , message_module ):
15861580 m = map_unittest_pb2 .TestMap ()
1587- with warnings .catch_warnings (record = True ) as w :
1588- m .map_int32_int32 [10 ] = True
1589- self .assertIn ('bool' , str (w [0 ].message ))
1590- self .assertEqual (m .map_int32_int32 [10 ], 1 )
1591-
1592- with warnings .catch_warnings (record = True ) as w :
1593- m .map_int32_int32 [True ] = 1
1594- self .assertIn ('bool' , str (w [0 ].message ))
1595- self .assertEqual (m .map_int32_int32 [1 ], 1 )
1581+ self .assign_bool_to_map_or_extension (m , 'map_int32_int32' , 10 , True )
1582+ self .assign_bool_to_map_or_extension (m , 'map_int32_int32' , True , 1 )
15961583
15971584 def testAssignBoolToExtensionInt (self , message_module ):
15981585 m = unittest_pb2 .TestAllExtensions ()
1599- with warnings .catch_warnings (record = True ) as w :
1600- m .Extensions [unittest_pb2 .optional_int32_extension ] = True
1601- self .assertIn ('bool' , str (w [0 ].message ))
1602- self .assertEqual (m .Extensions [unittest_pb2 .optional_int32_extension ], 1 )
1586+ self .assign_bool_to_map_or_extension (
1587+ m , 'Extensions' , unittest_pb2 .optional_int32_extension , True
1588+ )
16031589
16041590
16051591@testing_refleaks .TestCase
0 commit comments