Skip to content

Commit 5fb5334

Browse files
authored
Improve type annotations for stricter type checking (ty) (#572)
1 parent dd46418 commit 5fb5334

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

model_bakery/baker.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ def __init__(
377377
if isinstance(_model, str):
378378
self.model = cast(type[M], self.finder.get_model(_model))
379379
else:
380-
self.model = cast(type[M], _model)
380+
self.model = _model
381381

382382
self.init_type_mapping()
383383

@@ -719,7 +719,11 @@ def _handle_m2m(self, instance: Model):
719719
m2m_relation.source_field_name: instance,
720720
m2m_relation.target_field_name: value,
721721
}
722-
make(through_model, _using=self._using, **base_kwargs)
722+
make(
723+
cast(type[Model], through_model),
724+
_using=self._using,
725+
**base_kwargs,
726+
)
723727

724728
def _handle_generic_foreign_keys(
725729
self, instance: Model, attrs: dict[str, Any], commit: bool = True
@@ -772,7 +776,7 @@ def _handle_generic_foreign_keys(
772776
def _remote_field(
773777
self, field: ForeignKey | OneToOneField
774778
) -> OneToOneRel | ManyToOneRel:
775-
return field.remote_field
779+
return cast(OneToOneRel | ManyToOneRel, field.remote_field)
776780

777781
def generate_value(self, field: Field, commit: bool = True) -> Any: # noqa: C901
778782
"""Call the associated generator with a field passing all required args.
@@ -813,8 +817,8 @@ def generate_value(self, field: Field, commit: bool = True) -> Any: # noqa: C90
813817
)
814818
elif is_content_type_fk:
815819
generator = self.type_mapping[contenttypes_models.ContentType]
816-
elif generators.get(field.__class__):
817-
generator = generators.get(field.__class__)
820+
elif gen := generators.get(field.__class__):
821+
generator = gen
818822
elif field.__class__ in self.type_mapping:
819823
generator = self.type_mapping[field.__class__]
820824
else:

0 commit comments

Comments
 (0)