def test_public_internal():
    """
    check we don't make anything from _internal public
    """
    public_internal_attributes = []

    def _test_file(file: Path, module_name: str):
        if file.name != '__init__.py' and not file.name.startswith('_'):
            module = sys.modules.get(module_name)
            if module is None:
                spec = importlib.util.spec_from_file_location(module_name, str(file))
                module = importlib.util.module_from_spec(spec)
                sys.modules[module_name] = module
                try:
                    spec.loader.exec_module(module)
                except ImportError:
                    return

            for name, attr in vars(module).items():
                if not name.startswith('_'):
                    attr_module = getattr(attr, '__module__', '')
                    if attr_module.startswith('pydantic._internal'):
                        public_internal_attributes.append(f'{module.__name__}:{name} from {attr_module}')

    pydantic_files = (Path(__file__).parent.parent / 'pydantic').glob('*.py')
    experimental_files = (Path(__file__).parent.parent / 'pydantic' / 'experimental').glob('*.py')

    for file in pydantic_files:
        _test_file(file, f'pydantic.{file.stem}')
    for file in experimental_files:
        _test_file(file, f'pydantic.experimental.{file.stem}')

    if public_internal_attributes:
        pytest.fail('The following should not be publicly accessible:\n  ' + '\n  '.join(public_internal_attributes))

    check we don't make anything from _internal public
    
tests/test_exports.py