Skip to content

Commit ddc8091

Browse files
author
rodrigo.nogueira
committed
fix: Transformer override with trait and base transformer (#1119)
When a field has both a base Transformer and a Trait that overrides it, user-provided overrides were bypassing the base transformer when the trait was not active. Fixed by propagating CAPTURE_OVERRIDES from no_declaration in Maybe class, ensuring overrides are forwarded to the base transformer.
1 parent ae9f2f4 commit ddc8091

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

factory/declarations.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,8 @@ def __init__(self, decider, yes_declaration=SKIP, no_declaration=SKIP):
511511

512512
self.FACTORY_BUILDER_PHASE = used_phases.pop() if used_phases else enums.BuilderPhase.ATTRIBUTE_RESOLUTION
513513

514+
self.CAPTURE_OVERRIDES = getattr(no_declaration, 'CAPTURE_OVERRIDES', False)
515+
514516
def evaluate_post(self, instance, step, overrides):
515517
"""Handle post-generation declarations"""
516518
decider_phase = enums.get_builder_phase(self.decider)

tests/test_transformer.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,31 @@ def test_trait_transform_applies_supplied(self):
228228
self.assertEqual(instance.one, "ONE")
229229
self.assertEqual(instance.two, "two")
230230
self.assertIsNone(instance.three)
231+
232+
233+
class TransformerTraitWithBaseTest(TestCase):
234+
"""Tests for Transformer + Trait interaction when base Transformer exists.
235+
236+
Regression tests for issue #1119.
237+
"""
238+
def test_base_transformer_applies_when_trait_inactive(self):
239+
"""When a trait is not active, the base transformer should still apply to overrides."""
240+
class TestFactory(factory.StubFactory):
241+
foo = factory.Transformer(123, transform=hex)
242+
243+
class Params:
244+
string = factory.Trait(foo=factory.Transformer(234, transform=str))
245+
246+
instance = TestFactory(foo=345)
247+
self.assertEqual(instance.foo, "0x159")
248+
249+
def test_trait_transformer_applies_when_trait_active(self):
250+
"""When a trait is active, the trait's transformer should apply."""
251+
class TestFactory(factory.StubFactory):
252+
foo = factory.Transformer(123, transform=hex)
253+
254+
class Params:
255+
string = factory.Trait(foo=factory.Transformer(234, transform=str))
256+
257+
instance = TestFactory(string=True, foo=345)
258+
self.assertEqual(instance.foo, "345")

0 commit comments

Comments
 (0)