Skip to content

Commit

Permalink
fix: allow non-hashable type args (#643)
Browse files Browse the repository at this point in the history
  • Loading branch information
adhtruong authored Feb 9, 2025
1 parent 3c1e68f commit 64fa119
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
7 changes: 5 additions & 2 deletions polyfactory/field_meta.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from dataclasses import asdict, is_dataclass
from typing import TYPE_CHECKING, Any, Literal, Mapping, Pattern, TypedDict, cast
from typing import TYPE_CHECKING, Any, Hashable, Literal, Mapping, Pattern, TypedDict, cast

from typing_extensions import get_args, get_origin

Expand Down Expand Up @@ -93,7 +93,10 @@ def type_args(self) -> tuple[Any, ...]:
:returns: a tuple of types.
"""
return tuple(TYPE_MAPPING.get(arg, arg) for arg in get_args(self.annotation))
return tuple(
TYPE_MAPPING.get(arg, arg) if isinstance(arg, Hashable) else arg # pyright: ignore[reportCallIssue,reportArgumentType]
for arg in get_args(self.annotation)
)

@classmethod
def from_type(
Expand Down
10 changes: 10 additions & 0 deletions tests/test_complex_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,13 @@ class MyFactory(DataclassFactory):
result = MyFactory.build()

assert result.sequence_dict


def test_build_with_callable() -> None:
@dataclass
class A:
foo: Callable[[str], int]

factory = DataclassFactory.create_factory(A)
with pytest.raises(ParameterException, match="Unsupported type:"):
factory.build()

0 comments on commit 64fa119

Please sign in to comment.