@@ -63,13 +63,14 @@ def __get_pydantic_core_schema__(self, source_type: Any, handler: GetCoreSchemaH
63
63
The Pydantic core schema.
64
64
"""
65
65
schema = handler (source_type )
66
- globalns , localns = handler ._get_types_namespace ()
67
66
try :
67
+ # Do not pass in globals as the function could be defined in a different module.
68
+ # Instead, let `get_function_return_type` infer the globals to use, but still pass
69
+ # in locals that may contain a parent/rebuild namespace:
68
70
return_type = _decorators .get_function_return_type (
69
71
self .func ,
70
72
self .return_type ,
71
- globalns = globalns ,
72
- localns = localns ,
73
+ localns = handler ._get_types_namespace ().locals ,
73
74
)
74
75
except NameError as e :
75
76
raise PydanticUndefinedAnnotation .from_name_error (e ) from e
@@ -166,11 +167,13 @@ def __get_pydantic_core_schema__(self, source_type: Any, handler: GetCoreSchemaH
166
167
schema = handler (source_type )
167
168
globalns , localns = handler ._get_types_namespace ()
168
169
try :
170
+ # Do not pass in globals as the function could be defined in a different module.
171
+ # Instead, let `get_function_return_type` infer the globals to use, but still pass
172
+ # in locals that may contain a parent/rebuild namespace:
169
173
return_type = _decorators .get_function_return_type (
170
174
self .func ,
171
175
self .return_type ,
172
- globalns = globalns ,
173
- localns = localns ,
176
+ localns = handler ._get_types_namespace ().locals ,
174
177
)
175
178
except NameError as e :
176
179
raise PydanticUndefinedAnnotation .from_name_error (e ) from e
0 commit comments