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
16 changes: 13 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,20 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu, macos, windows]
os: [ubuntu-latest, macos-13, macos-latest, windows-latest]
sydney-runkle marked this conversation as resolved.
Show resolved Hide resolved
python-version: ['3.7', '3.8', '3.9', '3.10']
include:
- os: ubuntu
exclude:
# Python 3.7, 3.8, and 3.9 are not available on macOS 14
- os: macos-13
python-version: '3.10'
- os: macos-latest
python-version: '3.7'
- os: macos-latest
python-version: '3.8'
- os: macos-latest
python-version: '3.9'

runs-on: ${{ matrix.os }}

env:
PYTHON: ${{ matrix.python-version }}
Expand Down
6 changes: 6 additions & 0 deletions pydantic/v1.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# NOTE This file aliases the pydantic namespace as pydantic.v1 for smoother v1 -> v2 transition
# flake8: noqa
import os

from pydantic import *

# 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'
):
sydney-runkle marked this conversation as resolved.
Show resolved Hide resolved
continue
module_name = module_fname[:-3]

_ = importlib.import_module(f'pydantic.v1.{module_name}')