|
2 | 2 |
|
3 | 3 | import copy |
4 | 4 | from contextlib import suppress |
| 5 | +from dataclasses import is_dataclass |
5 | 6 | from datetime import timezone |
6 | 7 | from functools import partial |
7 | 8 | from os.path import realpath |
|
55 | 56 | ModelField, # pyright: ignore[attr-defined,reportAttributeAccessIssue] |
56 | 57 | Undefined, # pyright: ignore[attr-defined,reportAttributeAccessIssue] |
57 | 58 | ) |
58 | | - from pydantic.dataclasses import is_pydantic_dataclass |
59 | 59 |
|
60 | 60 | # Keep this import last to prevent warnings from pydantic if pydantic v2 |
61 | 61 | # is installed. |
|
69 | 69 |
|
70 | 70 | # v2 specific imports |
71 | 71 | from pydantic import BaseModel as BaseModelV2 |
72 | | - from pydantic.dataclasses import is_pydantic_dataclass |
73 | 72 | from pydantic_core import PydanticUndefined as UndefinedV2 |
74 | 73 | from pydantic_core import to_json |
75 | 74 |
|
|
101 | 100 |
|
102 | 101 | from typing_extensions import NotRequired, TypeGuard |
103 | 102 |
|
| 103 | + from pydantic.dataclasses import PydanticDataclass # pyright: ignore[reportPrivateImportUsage] |
| 104 | + |
104 | 105 | ModelT = TypeVar("ModelT", bound="BaseModelV1 | BaseModelV2") # pyright: ignore[reportInvalidTypeForm] |
105 | 106 | T = TypeVar("T") |
106 | 107 |
|
@@ -634,6 +635,11 @@ def _is_pydantic_v2_model(model: Any) -> TypeGuard[BaseModelV2]: # pyright: ign |
634 | 635 | return not _IS_PYDANTIC_V1 and is_safe_subclass(model, BaseModelV2) |
635 | 636 |
|
636 | 637 |
|
| 638 | +def is_pydantic_dataclass(cls: type[Any]) -> TypeGuard[PydanticDataclass]: |
| 639 | + # This method is available in the `pydantic.dataclasses` module for python >= 3.9 |
| 640 | + return is_dataclass(cls) and "__pydantic_validator__" in cls.__dict__ |
| 641 | + |
| 642 | + |
637 | 643 | class PydanticDataclassFactory(ModelFactory[T]): # type: ignore[type-var] |
638 | 644 | """Base factory for pydantic dataclasses""" |
639 | 645 |
|
|
0 commit comments