Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import modules/objects directly from v1 namespace #9162

Merged
merged 13 commits into from
Jun 5, 2024
7 changes: 7 additions & 0 deletions pydantic/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
# flake8: noqa
from pydantic import *


import os
sydney-runkle marked this conversation as resolved.
Show resolved Hide resolved

# allows importing of objects from modules directly
# i.e. from pydantic.v1.fields import ModelField
__path__ = [os.path.dirname(os.path.abspath(__file__))]

# WARNING __all__ from .errors is not included here, it will be removed as an export here in v2
# please use "from pydantic.errors import ..." instead
__all__ = [
Expand Down
29 changes: 29 additions & 0 deletions tests/test_v1.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,31 @@
import importlib
import os

import pydantic


def test_imports() -> None:
from pydantic.v1 import BaseModel, dataclasses # noqa: F401


def test_imports_from_modules() -> None:
"""That specific objects can be imported from modules directly through the
``v1`` namespace."""
from pydantic.v1.fields import ModelField # noqa: F401
from pydantic.v1.generics import GenericModel # noqa: F401
from pydantic.v1.validators import bool_validator # noqa: F401


def test_can_import_modules_from_v1() -> None:
"""That imports from any module in pydantic can be imported through
``pydantic.v1.<module>``"""
for module_fname in os.listdir(pydantic.__path__[0]):
if (
module_fname.startswith("_")
or not module_fname.endswith(".py")
or module_fname == "v1.py"
):
continue
module_name = module_fname[:-3]

_ = importlib.import_module(f"pydantic.v1.{module_name}")
sydney-runkle marked this conversation as resolved.
Show resolved Hide resolved