@@ -124,6 +124,7 @@ def __init__(
124124 default : Any = ...,
125125 children : list [FieldMeta ] | None = None ,
126126 constraints : PydanticConstraints | None = None ,
127+ examples : list [Any ] | None = None ,
127128 ) -> None :
128129 super ().__init__ (
129130 name = name ,
@@ -133,6 +134,7 @@ def __init__(
133134 children = children ,
134135 constraints = constraints ,
135136 )
137+ self .examples = examples
136138
137139 @classmethod
138140 def from_field_info (
@@ -212,13 +214,15 @@ def from_field_info(
212214 if is_json :
213215 constraints ["json" ] = True
214216
215- return PydanticFieldMeta .from_type (
217+ result = PydanticFieldMeta .from_type (
216218 annotation = annotation ,
217219 children = children ,
218220 constraints = cast ("Constraints" , {k : v for k , v in constraints .items () if v is not None }) or None ,
219221 default = default_value ,
220222 name = name ,
221223 )
224+ result .examples = field_info .examples
225+ return result
222226
223227 @classmethod
224228 def from_model_field ( # pragma: no cover
@@ -336,12 +340,15 @@ def from_model_field( # pragma: no cover
336340 for arg in fields_to_iterate
337341 )
338342
343+ examples = None
344+
339345 return PydanticFieldMeta (
340346 name = name ,
341347 annotation = annotation , # pyright: ignore[reportArgumentType]
342348 children = children or None ,
343349 default = default_value ,
344350 constraints = cast ("PydanticConstraints" , {k : v for k , v in constraints .items () if v is not None }) or None ,
351+ examples = examples ,
345352 )
346353
347354 if not _IS_PYDANTIC_V1 :
@@ -363,6 +370,28 @@ class ModelFactory(Generic[T], BaseFactory[T]):
363370
364371 __forward_ref_resolution_type_mapping__ : ClassVar [Mapping [str , type ]] = {}
365372 __is_base_factory__ = True
373+ __use_examples__ : ClassVar [bool ] = False # for backwards compatibility
374+ """
375+ Flag indicating whether to use a random example, if provided (Pydantic >=V2)
376+
377+ Example code::
378+
379+ class Payment(BaseModel):
380+ amount: int = Field(0)
381+ currency: str = Field(examples=['USD', 'EUR', 'INR'])
382+
383+ class PaymentFactory(ModelFactory[Payment]):
384+ __use_examples__ = True
385+
386+ >>> payment = PaymentFactory.build()
387+ >>> payment
388+ Payment(amount=120, currency="EUR")
389+ """
390+
391+ __config_keys__ = (
392+ * BaseFactory .__config_keys__ ,
393+ "__use_examples__" ,
394+ )
366395
367396 def __init_subclass__ (cls , * args : Any , ** kwargs : Any ) -> None :
368397 super ().__init_subclass__ (* args , ** kwargs )
@@ -425,6 +454,7 @@ def get_constrained_field_value(
425454 build_context : BuildContext | None = None ,
426455 ) -> Any :
427456 constraints = cast ("PydanticConstraints" , field_meta .constraints )
457+
428458 if constraints .pop ("json" , None ):
429459 value = cls .get_field_value (
430460 field_meta , field_build_parameters = field_build_parameters , build_context = build_context
@@ -435,6 +465,34 @@ def get_constrained_field_value(
435465 annotation , field_meta , field_build_parameters = field_build_parameters , build_context = build_context
436466 )
437467
468+ @classmethod
469+ def get_field_value (
470+ cls ,
471+ field_meta : FieldMeta ,
472+ field_build_parameters : Any | None = None ,
473+ build_context : BuildContext | None = None ,
474+ ) -> Any :
475+ """Return a value from examples if exists, else random value.
476+
477+ :param field_meta: FieldMeta instance.
478+ :param field_build_parameters: Any build parameters passed to the factory as kwarg values.
479+ :param build_context: BuildContext data for current build.
480+
481+ :returns: An arbitrary value.
482+
483+ """
484+ result : Any
485+
486+ field_meta = cast ("PydanticFieldMeta" , field_meta )
487+
488+ if cls .__use_examples__ and field_meta .examples :
489+ result = cls .__random__ .choice (field_meta .examples )
490+ else :
491+ result = super ().get_field_value (
492+ field_meta = field_meta , field_build_parameters = field_build_parameters , build_context = build_context
493+ )
494+ return result
495+
438496 @classmethod
439497 def build (
440498 cls ,
0 commit comments