From 6961eaa3b65fc63b716e854bd2eb28b5bc96e029 Mon Sep 17 00:00:00 2001 From: guacs <126393040+guacs@users.noreply.github.com> Date: Fri, 8 Dec 2023 16:57:34 +0530 Subject: [PATCH] fix: don't blindly suppress ValueError (#450) --- polyfactory/factories/base.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/polyfactory/factories/base.py b/polyfactory/factories/base.py index 07a6c97d..9febf9b0 100644 --- a/polyfactory/factories/base.py +++ b/polyfactory/factories/base.py @@ -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): @@ -541,8 +541,11 @@ 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, @@ -550,7 +553,6 @@ def get_constrained_field_value(cls, annotation: Any, field_meta: FieldMeta) -> 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,