Skip to content

Commit

Permalink
fix: don't blindly suppress ValueError (#450)
Browse files Browse the repository at this point in the history
  • Loading branch information
guacs committed Dec 8, 2023
1 parent 1a0da0d commit 6961eaa
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions polyfactory/factories/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ def create_factory(
)

@classmethod
def get_constrained_field_value(cls, annotation: Any, field_meta: FieldMeta) -> Any: # noqa: C901, PLR0911
def get_constrained_field_value(cls, annotation: Any, field_meta: FieldMeta) -> Any: # noqa: C901, PLR0911, PLR0912
try:
constraints = cast("Constraints", field_meta.constraints)
if is_safe_subclass(annotation, float):
Expand Down Expand Up @@ -541,16 +541,18 @@ def get_constrained_field_value(cls, annotation: Any, field_meta: FieldMeta) ->
pattern=constraints.get("pattern"),
)

with suppress(ValueError):
try:
collection_type = get_collection_type(annotation)
except ValueError:
collection_type = None
if collection_type is not None:
if collection_type == dict:
return handle_constrained_mapping(
factory=cls,
field_meta=field_meta,
min_items=constraints.get("min_length"),
max_items=constraints.get("max_length"),
)

return handle_constrained_collection(
collection_type=collection_type, # type: ignore[type-var]
factory=cls,
Expand Down

0 comments on commit 6961eaa

Please sign in to comment.