Running pydantic type validation in generated code string raises KeyError: '__name__' #8448
Closed
1 task done
Labels
bug V2
Bug related to Pydantic V2
Initial Checks
Description
When running type validation of non-pydantic_validator objects in a string execution (e.g. generated code) context, a
KeyError: '__name__'
is raised byTypeAdapter.__init__
unless{'__name__': '__main__'}
is passed as part of global variables toexec
.While this issue is trivial to fix for a user if they understand the execution context requirements, it seems this
__name__
requirement was only recently added to the type adapter and the raised error is fairly confusing and/or unexpected behavior.The simplest fix here in my mind is to check if
__name__
is present onf.f_globals
and if not, either raise a more informative error or set it to__main__
by default.Example Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: