@@ -61,6 +61,8 @@ def _pydantic_recursion_protector(
61
61
name = None ,
62
62
allow_cycles : bool = False ,
63
63
sort_alphabetically : Optional [bool ] = None ,
64
+ globalns : dict = None ,
65
+ localns : dict = None ,
64
66
) -> Optional [type [PydanticModel ]]:
65
67
"""
66
68
It is an inner function to protect pydantic model creator against cyclic recursion
@@ -93,6 +95,8 @@ def _pydantic_recursion_protector(
93
95
_stack = stack ,
94
96
allow_cycles = allow_cycles ,
95
97
sort_alphabetically = sort_alphabetically ,
98
+ globalns = globalns ,
99
+ localns = localns ,
96
100
_as_submodel = True ,
97
101
)
98
102
return pmc .create_pydantic_model ()
@@ -237,6 +241,8 @@ def __init__(
237
241
model_config : Optional [ConfigDict ] = None ,
238
242
validators : Optional [dict [str , Any ]] = None ,
239
243
module : str = __name__ ,
244
+ globalns : dict = None ,
245
+ localns : dict = None ,
240
246
_stack : tuple = (),
241
247
_as_submodel : bool = False ,
242
248
) -> None :
@@ -288,7 +294,7 @@ def __init__(
288
294
289
295
self ._as_submodel = _as_submodel
290
296
291
- self ._annotations = get_annotations (cls )
297
+ self ._annotations = get_annotations (cls , globalns = globalns , localns = localns )
292
298
293
299
self ._pconfig : ConfigDict
294
300
@@ -305,6 +311,9 @@ def __init__(
305
311
self ._validators = validators
306
312
self ._module = module
307
313
314
+ self .globalns = globalns
315
+ self .localns = localns
316
+
308
317
self ._stack = _stack
309
318
310
319
@property
@@ -523,7 +532,7 @@ def _process_computed_field(
523
532
field : ComputedFieldDescription ,
524
533
) -> Optional [Any ]:
525
534
func = field .function
526
- annotation = get_annotations (self ._cls , func ).get ("return" , None )
535
+ annotation = get_annotations (self ._cls , func , globalns = self . globalns , localns = self . localns ).get ("return" , None )
527
536
comment = _cleandoc (func )
528
537
if annotation is not None :
529
538
c_f = computed_field (return_type = annotation , description = comment )
@@ -555,6 +564,8 @@ def get_fields_to_carry_on(field_tuple: tuple[str, ...]) -> tuple[str, ...]:
555
564
stack = new_stack ,
556
565
allow_cycles = self .meta .allow_cycles ,
557
566
sort_alphabetically = self .meta .sort_alphabetically ,
567
+ globalns = self .globalns ,
568
+ localns = self .localns ,
558
569
)
559
570
else :
560
571
pmodel = None
@@ -581,6 +592,8 @@ def pydantic_model_creator(
581
592
model_config : Optional [ConfigDict ] = None ,
582
593
validators : Optional [dict [str , Any ]] = None ,
583
594
module : str = __name__ ,
595
+ globalns : dict = None ,
596
+ localns : dict = None ,
584
597
) -> type [PydanticModel ]:
585
598
"""
586
599
Function to build `Pydantic Model <https://docs.pydantic.dev/latest/concepts/models/>`__ off Tortoise Model.
@@ -607,6 +620,8 @@ def pydantic_model_creator(
607
620
:param model_config: A custom config to use as pydantic config.
608
621
:param validators: A dictionary of methods that validate fields.
609
622
:param module: The name of the module that the model belongs to.
623
+ :param globalns: If specified, use this dictionary as the globals map.
624
+ :param localns: If specified, use this dictionary as the locals map.
610
625
611
626
Note: Created pydantic model uses config_class parameter and PydanticMeta's
612
627
config_class as its Config class's bases(Only if provided!), but it
@@ -627,5 +642,7 @@ def pydantic_model_creator(
627
642
model_config = model_config ,
628
643
validators = validators ,
629
644
module = module ,
645
+ globalns = globalns ,
646
+ localns = localns ,
630
647
)
631
648
return pmc .create_pydantic_model ()
0 commit comments