Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't blindly suppress ValueError #450

Merged
merged 2 commits into from
Dec 8, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
guacs marked this conversation as resolved.
Show resolved Hide resolved
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
Loading