|
| 1 | +""" |
| 2 | +Tests for GitHub issue #188: Class attribute bug in neat.attributes |
| 3 | +
|
| 4 | +This tests that _config_items is properly isolated between instances |
| 5 | +rather than being shared at the class level. |
| 6 | +""" |
| 7 | + |
| 8 | +import unittest |
| 9 | +from neat.attributes import StringAttribute, FloatAttribute, BoolAttribute, IntegerAttribute |
| 10 | + |
| 11 | + |
| 12 | +class TestIssue188AttributeIsolation(unittest.TestCase): |
| 13 | + """Test that attribute instances don't share _config_items.""" |
| 14 | + |
| 15 | + def test_string_attribute_isolation(self): |
| 16 | + """Test that StringAttribute instances have separate _config_items.""" |
| 17 | + attr1 = StringAttribute('activation', options='sigmoid') |
| 18 | + attr2 = StringAttribute('aggregation', options='sum') |
| 19 | + |
| 20 | + # Should NOT share the same object |
| 21 | + self.assertIsNot(attr1._config_items, attr2._config_items, |
| 22 | + "StringAttribute instances should not share _config_items") |
| 23 | + |
| 24 | + # Should have different option values |
| 25 | + self.assertEqual(attr1._config_items['options'][1], 'sigmoid') |
| 26 | + self.assertEqual(attr2._config_items['options'][1], 'sum') |
| 27 | + |
| 28 | + def test_string_attribute_mutation_isolation(self): |
| 29 | + """Test that modifying one instance doesn't affect another.""" |
| 30 | + attr1 = StringAttribute('foo', options='option_a') |
| 31 | + attr2 = StringAttribute('bar', options='option_b') |
| 32 | + |
| 33 | + # Modify attr1's _config_items |
| 34 | + attr1._config_items['options'] = ['MODIFIED', 'TEST'] |
| 35 | + |
| 36 | + # attr2 should NOT be affected |
| 37 | + self.assertEqual(attr2._config_items['options'][1], 'option_b', |
| 38 | + "Modifying attr1 should not affect attr2") |
| 39 | + |
| 40 | + def test_float_attribute_isolation(self): |
| 41 | + """Test that FloatAttribute instances have separate _config_items.""" |
| 42 | + attr1 = FloatAttribute('weight', init_mean=0.0) |
| 43 | + attr2 = FloatAttribute('bias', init_mean=1.0) |
| 44 | + |
| 45 | + self.assertIsNot(attr1._config_items, attr2._config_items) |
| 46 | + self.assertEqual(attr1._config_items['init_mean'][1], 0.0) |
| 47 | + self.assertEqual(attr2._config_items['init_mean'][1], 1.0) |
| 48 | + |
| 49 | + def test_bool_attribute_isolation(self): |
| 50 | + """Test that BoolAttribute instances have separate _config_items.""" |
| 51 | + attr1 = BoolAttribute('enabled', default='True') |
| 52 | + attr2 = BoolAttribute('flag', default='False') |
| 53 | + |
| 54 | + self.assertIsNot(attr1._config_items, attr2._config_items) |
| 55 | + self.assertEqual(attr1._config_items['default'][1], 'True') |
| 56 | + self.assertEqual(attr2._config_items['default'][1], 'False') |
| 57 | + |
| 58 | + def test_integer_attribute_isolation(self): |
| 59 | + """Test that IntegerAttribute instances have separate _config_items.""" |
| 60 | + attr1 = IntegerAttribute('count', min_value=0) |
| 61 | + attr2 = IntegerAttribute('index', min_value=1) |
| 62 | + |
| 63 | + self.assertIsNot(attr1._config_items, attr2._config_items) |
| 64 | + self.assertEqual(attr1._config_items['min_value'][1], 0) |
| 65 | + self.assertEqual(attr2._config_items['min_value'][1], 1) |
| 66 | + |
| 67 | + def test_mixed_attribute_types_isolation(self): |
| 68 | + """Test that different attribute types don't interfere with each other.""" |
| 69 | + float_attr = FloatAttribute('weight') |
| 70 | + string_attr = StringAttribute('activation') |
| 71 | + bool_attr = BoolAttribute('enabled') |
| 72 | + int_attr = IntegerAttribute('count') |
| 73 | + |
| 74 | + # Each should have its own _config_items |
| 75 | + items_list = [ |
| 76 | + float_attr._config_items, |
| 77 | + string_attr._config_items, |
| 78 | + bool_attr._config_items, |
| 79 | + int_attr._config_items |
| 80 | + ] |
| 81 | + |
| 82 | + # All should be different objects |
| 83 | + for i, items_i in enumerate(items_list): |
| 84 | + for j, items_j in enumerate(items_list): |
| 85 | + if i != j: |
| 86 | + self.assertIsNot(items_i, items_j, |
| 87 | + f"Attribute instances {i} and {j} should not share _config_items") |
| 88 | + |
| 89 | + def test_genes_pattern(self): |
| 90 | + """Test the actual usage pattern from DefaultNodeGene.""" |
| 91 | + # This is how it's used in genes.py |
| 92 | + attr_activation = StringAttribute('activation', options='') |
| 93 | + attr_aggregation = StringAttribute('aggregation', options='') |
| 94 | + |
| 95 | + # Should have separate _config_items even with same default value |
| 96 | + self.assertIsNot(attr_activation._config_items, attr_aggregation._config_items, |
| 97 | + "activation and aggregation attributes should have separate _config_items") |
| 98 | + |
| 99 | + # Both should have empty string as options default |
| 100 | + self.assertEqual(attr_activation._config_items['options'][1], '') |
| 101 | + self.assertEqual(attr_aggregation._config_items['options'][1], '') |
| 102 | + |
| 103 | + # Modifying one should not affect the other |
| 104 | + attr_activation._config_items['options'][1] = 'modified' |
| 105 | + self.assertEqual(attr_aggregation._config_items['options'][1], '', |
| 106 | + "Modifying activation should not affect aggregation") |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == '__main__': |
| 110 | + unittest.main() |
0 commit comments