Skip to content

Commit

Permalink
Fix module path_type creation when globals does not contain `__name…
Browse files Browse the repository at this point in the history
…__` (#8470)
  • Loading branch information
hramezani committed Jan 5, 2024
1 parent 2213f3c commit 383290f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pydantic/type_adapter.py
Expand Up @@ -204,7 +204,7 @@ def __init__(
except AttributeError:
if module is None:
f = sys._getframe(1)
module = cast(str, f.f_globals['__name__'])
module = cast(str, f.f_globals.get('__name__', ''))
validator = create_schema_validator(
core_schema, type, module, str(type), 'TypeAdapter', core_config, config_wrapper.plugin_settings
) # type: ignore
Expand Down
22 changes: 22 additions & 0 deletions tests/test_plugins.py
Expand Up @@ -360,6 +360,28 @@ def new_schema_validator(self, schema, schema_type, schema_type_path, schema_kin
TypeAdapter(List[str], module='provided_module_by_type_adapter')


def test_plugin_path_type_adapter_without_name_in_globals() -> None:
class CustomOnValidatePython(ValidatePythonHandlerProtocol):
pass

class Plugin:
def new_schema_validator(self, schema, schema_type, schema_type_path, schema_kind, config, plugin_settings):
assert str(schema_type) == 'typing.List[str]'
assert schema_type_path == SchemaTypePath('', 'typing.List[str]')
assert schema_kind == 'TypeAdapter'
return CustomOnValidatePython(), None, None

plugin = Plugin()
with install_plugin(plugin):
code = """
from typing import List
import pydantic
pydantic.TypeAdapter(List[str])
"""
exec(code, {'bar': 'baz'})


def test_plugin_path_validate_call() -> None:
class CustomOnValidatePython(ValidatePythonHandlerProtocol):
pass
Expand Down

0 comments on commit 383290f

Please sign in to comment.