61
61
# is installed.
62
62
from pydantic import PyObject
63
63
64
- # prevent unbound variable warnings
64
+ # Prevent unbound variable warnings
65
65
BaseModelV2 = BaseModelV1
66
66
UndefinedV2 = Undefined
67
+
68
+ if TYPE_CHECKING :
69
+ from pydantic .dataclasses import Dataclass as PydanticDataclassV1 # pyright: ignore[reportPrivateImportUsage]
70
+
71
+ # Prevent unbound variable warnings
72
+ PydanticDataclassV2 = PydanticDataclassV1
67
73
except ImportError :
68
74
# pydantic v2
69
75
92
98
from pydantic .v1 .color import Color # type: ignore[assignment]
93
99
from pydantic .v1 .fields import DeferredType , ModelField , Undefined
94
100
101
+ if TYPE_CHECKING :
102
+ from pydantic .dataclasses import PydanticDataclass as PydanticDataclassV2 # pyright: ignore[reportPrivateImportUsage]
95
103
96
104
if TYPE_CHECKING :
97
105
from collections import abc
100
108
101
109
from typing_extensions import NotRequired , TypeGuard
102
110
103
- from pydantic .dataclasses import PydanticDataclass # pyright: ignore[reportPrivateImportUsage]
104
111
105
112
ModelT = TypeVar ("ModelT" , bound = "BaseModelV1 | BaseModelV2" ) # pyright: ignore[reportInvalidTypeForm]
106
113
T = TypeVar ("T" )
@@ -635,8 +642,11 @@ def _is_pydantic_v2_model(model: Any) -> TypeGuard[BaseModelV2]: # pyright: ign
635
642
return not _IS_PYDANTIC_V1 and is_safe_subclass (model , BaseModelV2 )
636
643
637
644
638
- def is_pydantic_dataclass (cls : type [Any ]) -> TypeGuard [PydanticDataclass ]:
639
- # This method is available in the `pydantic.dataclasses` module for python >= 3.9
645
+ def _is_pydantic_v1_dataclass (cls : type [Any ]) -> TypeGuard [PydanticDataclassV1 ]:
646
+ return is_dataclass (cls ) and "__pydantic_model__" in cls .__dict__
647
+
648
+
649
+ def _is_pydantic_v2_dataclass (cls : type [Any ]) -> TypeGuard [PydanticDataclassV2 ]:
640
650
return is_dataclass (cls ) and "__pydantic_validator__" in cls .__dict__
641
651
642
652
@@ -647,27 +657,37 @@ class PydanticDataclassFactory(ModelFactory[T]): # type: ignore[type-var]
647
657
648
658
@classmethod
649
659
def is_supported_type (cls , value : Any ) -> TypeGuard [type [T ]]:
650
- return is_pydantic_dataclass (value )
660
+ return _is_pydantic_v1_dataclass ( value ) or _is_pydantic_v2_dataclass (value )
651
661
652
662
@classmethod
653
663
def get_model_fields (cls ) -> list [FieldMeta ]:
654
- if not is_pydantic_dataclass (cls .__model__ ):
664
+ if _is_pydantic_v1_dataclass (cls .__model__ ):
665
+ pydantic_model = cls .__model__ .__pydantic_model__
666
+ cls ._fields_metadata = [
667
+ PydanticFieldMeta .from_model_field (
668
+ field ,
669
+ use_alias = not pydantic_model .__config__ .allow_population_by_field_name , # type: ignore[attr-defined]
670
+ random = cls .__random__ ,
671
+ )
672
+ for field in pydantic_model .__fields__ .values ()
673
+ ]
674
+ elif _is_pydantic_v2_dataclass (cls .__model__ ):
675
+ pydantic_fields = cls .__model__ .__pydantic_fields__
676
+ pydantic_config = cls .__model__ .__pydantic_config__
677
+ cls ._fields_metadata = [
678
+ PydanticFieldMeta .from_field_info (
679
+ field_info = field_info ,
680
+ field_name = field_name ,
681
+ random = cls .__random__ ,
682
+ use_alias = not pydantic_config .get (
683
+ "populate_by_name" ,
684
+ False ,
685
+ ),
686
+ )
687
+ for field_name , field_info in pydantic_fields .items ()
688
+ ]
689
+ else :
655
690
# This should be unreachable
656
691
return []
657
692
658
- pydantic_fields = cls .__model__ .__pydantic_fields__
659
- pydantic_config = cls .__model__ .__pydantic_config__
660
- cls ._fields_metadata = [
661
- PydanticFieldMeta .from_field_info (
662
- field_info = field_info ,
663
- field_name = field_name ,
664
- random = cls .__random__ ,
665
- use_alias = not pydantic_config .get (
666
- "populate_by_name" ,
667
- False ,
668
- ),
669
- )
670
- for field_name , field_info in pydantic_fields .items ()
671
- ]
672
-
673
693
return cls ._fields_metadata
0 commit comments