Skip to content

Commit

Permalink
fix: ListField fake generation when it contains Optional. Closes #232 (
Browse files Browse the repository at this point in the history
…#244)

Co-authored-by: Marcos Schroh <marcos.schroh@kpn.com>
  • Loading branch information
marcosschroh and marcosschroh committed Feb 16, 2023
1 parent b8ccaaa commit ab77d0a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
9 changes: 4 additions & 5 deletions dataclasses_avroschema/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,23 +287,22 @@ def generate_items_type(self) -> typing.Any:
items_type = self.type.__args__[0]

if utils.is_union(items_type):
self.items_type = UnionField(
self.internal_field = UnionField(
self.name,
items_type,
default=self.default,
default_factory=self.default_factory,
model_metadata=self.model_metadata,
parent=self.parent,
).get_avro_type()
)
else:
self.internal_field = AvroField(
self.name, items_type, model_metadata=self.model_metadata, parent=self.parent
)
self.items_type = self.internal_field.get_avro_type()

self.items_type = self.internal_field.get_avro_type()

def fake(self) -> typing.List:
# return a list of one element with the type specified
# TODO: check when the internal value is self reference which seems to return `None`
return [self.internal_field.fake()]


Expand Down
1 change: 1 addition & 0 deletions tests/fake/test_fake.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ def test_self_one_to_many_relationship() -> None:
class User(AvroModel):
name: str
age: int
points: typing.List[typing.Optional[types.Float32]]
teamates: typing.Optional[typing.List[typing.Type["User"]]] = None

assert isinstance(User.fake(), User)
Expand Down

0 comments on commit ab77d0a

Please sign in to comment.