@@ -1431,6 +1431,115 @@ def testMessageClassName(self, message_module):
14311431 'TestAllTypes.NestedMessage' , nested .__class__ .__qualname__
14321432 )
14331433
1434+ def testAssignBoolToEnum (self , message_module ):
1435+ # TODO: change warning into error in 2026 Q1
1436+ # with self.assertRaises(TypeError):
1437+ with warnings .catch_warnings (record = True ) as w :
1438+ m = message_module .TestAllTypes (optional_nested_enum = True )
1439+ self .assertIn ('bool' , str (w [0 ].message ))
1440+ self .assertEqual (m .optional_nested_enum , 1 )
1441+
1442+ m = message_module .TestAllTypes (optional_nested_enum = 2 )
1443+ with warnings .catch_warnings (record = True ) as w :
1444+ m .optional_nested_enum = True
1445+ self .assertIn ('bool' , str (w [0 ].message ))
1446+ self .assertEqual (m .optional_nested_enum , 1 )
1447+
1448+ with warnings .catch_warnings (record = True ) as w :
1449+ m .optional_nested_enum = 2
1450+ self .assertFalse (w )
1451+ self .assertEqual (m .optional_nested_enum , 2 )
1452+
1453+ def testBoolToRepeatedEnum (self , message_module ):
1454+ with warnings .catch_warnings (record = True ) as w :
1455+ m = message_module .TestAllTypes (repeated_nested_enum = [True ])
1456+ self .assertIn ('bool' , str (w [0 ].message ))
1457+ self .assertEqual (m .repeated_nested_enum , [1 ])
1458+
1459+ m = message_module .TestAllTypes ()
1460+ with warnings .catch_warnings (record = True ) as w :
1461+ m .repeated_nested_enum .append (True )
1462+ self .assertIn ('bool' , str (w [0 ].message ))
1463+ self .assertEqual (m .repeated_nested_enum , [1 ])
1464+
1465+ def testBoolToOneofEnum (self , message_module ):
1466+ m = unittest_pb2 .TestOneof2 ()
1467+ with warnings .catch_warnings (record = True ) as w :
1468+ m .foo_enum = True
1469+ self .assertIn ('bool' , str (w [0 ].message ))
1470+ self .assertEqual (m .foo_enum , 1 )
1471+
1472+ def testBoolToMapEnum (self , message_module ):
1473+ m = map_unittest_pb2 .TestMap ()
1474+ with warnings .catch_warnings (record = True ) as w :
1475+ m .map_int32_enum [10 ] = True
1476+ self .assertIn ('bool' , str (w [0 ].message ))
1477+ self .assertEqual (m .map_int32_enum [10 ], 1 )
1478+
1479+ def testBoolToExtensionEnum (self , message_module ):
1480+ m = unittest_pb2 .TestAllExtensions ()
1481+ with warnings .catch_warnings (record = True ) as w :
1482+ m .Extensions [unittest_pb2 .optional_nested_enum_extension ] = True
1483+ self .assertIn ('bool' , str (w [0 ].message ))
1484+ self .assertEqual (
1485+ m .Extensions [unittest_pb2 .optional_nested_enum_extension ], 1
1486+ )
1487+
1488+ def testAssignBoolToInt (self , message_module ):
1489+ with warnings .catch_warnings (record = True ) as w :
1490+ m = message_module .TestAllTypes (optional_int32 = True )
1491+ self .assertIn ('bool' , str (w [0 ].message ))
1492+ self .assertEqual (m .optional_int32 , 1 )
1493+
1494+ m = message_module .TestAllTypes (optional_uint32 = 123 )
1495+ with warnings .catch_warnings (record = True ) as w :
1496+ m .optional_uint32 = True
1497+ self .assertIn ('bool' , str (w [0 ].message ))
1498+ self .assertEqual (m .optional_uint32 , 1 )
1499+
1500+ with warnings .catch_warnings (record = True ) as w :
1501+ m .optional_uint32 = 321
1502+ self .assertFalse (w )
1503+ self .assertEqual (m .optional_uint32 , 321 )
1504+
1505+ def testAssignBoolToRepeatedInt (self , message_module ):
1506+ with warnings .catch_warnings (record = True ) as w :
1507+ m = message_module .TestAllTypes (repeated_int64 = [True ])
1508+ self .assertIn ('bool' , str (w [0 ].message ))
1509+ self .assertEqual (m .repeated_int64 , [1 ])
1510+
1511+ m = message_module .TestAllTypes ()
1512+ with warnings .catch_warnings (record = True ) as w :
1513+ m .repeated_int64 .append (True )
1514+ self .assertIn ('bool' , str (w [0 ].message ))
1515+ self .assertEqual (m .repeated_int64 , [1 ])
1516+
1517+ def testAssignBoolToOneofInt (self , message_module ):
1518+ m = unittest_pb2 .TestOneof2 ()
1519+ with warnings .catch_warnings (record = True ) as w :
1520+ m .foo_int = True
1521+ self .assertIn ('bool' , str (w [0 ].message ))
1522+ self .assertEqual (m .foo_int , 1 )
1523+
1524+ def testAssignBoolToMapInt (self , message_module ):
1525+ m = map_unittest_pb2 .TestMap ()
1526+ with warnings .catch_warnings (record = True ) as w :
1527+ m .map_int32_int32 [10 ] = True
1528+ self .assertIn ('bool' , str (w [0 ].message ))
1529+ self .assertEqual (m .map_int32_int32 [10 ], 1 )
1530+
1531+ with warnings .catch_warnings (record = True ) as w :
1532+ m .map_int32_int32 [True ] = 1
1533+ self .assertIn ('bool' , str (w [0 ].message ))
1534+ self .assertEqual (m .map_int32_int32 [1 ], 1 )
1535+
1536+ def testAssignBoolToExtensionInt (self , message_module ):
1537+ m = unittest_pb2 .TestAllExtensions ()
1538+ with warnings .catch_warnings (record = True ) as w :
1539+ m .Extensions [unittest_pb2 .optional_int32_extension ] = True
1540+ self .assertIn ('bool' , str (w [0 ].message ))
1541+ self .assertEqual (m .Extensions [unittest_pb2 .optional_int32_extension ], 1 )
1542+
14341543
14351544@testing_refleaks .TestCase
14361545class TestRecursiveGroup (unittest .TestCase ):
0 commit comments