Is there a way to get a list of Factory Boy subfactory objects from with the correct Django object type?
I have a factory boy factory that uses Django. I need to access a list of the generated objects from a SubFactory. However, whenever I attempt to get that list, I receive TypeErrors that say that my generated objects are "SubFactory" instead of the type of the object I actually need. Any idea what I'm missing?
# factories.py
class PartNoRelationFactory(factory.django.DjangoModelFactory):
class Meta:
model = PartNoRelation
id = factory.Sequence(lambda n: n + 1)
parent_part_no = factory.SubFactory(PART_NO_FACTORY)
child_part_no = factory.SubFactory(PART_NO_FACTORY)
# test_factories.py
def mock_child_part_nos(arg_1: PartNo) -> 'list[PartNo]':
mock_part_no_relation_one = PartNoRelationFactory()
mock_part_no_relation_two = PartNoRelationFactory()
mock_part_no_relation_three = PartNoRelationFactory()
return [
mock_part_no_relation_one.child_part_no,
mock_part_no_relation_two.child_part_no,
mock_part_no_relation_three.child_part_no
]
# error: "Expression of type "list[SubFactory]" cannot be assigned to return type "list[PartNo]"