Skip to content

Commit

Permalink
fix export tests
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Sep 21, 2023
1 parent 1c5ea3b commit 8ad7752
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 25 deletions.
18 changes: 5 additions & 13 deletions pydantic/__init__.py
Expand Up @@ -196,14 +196,10 @@
'GenerateSchema',
]

# A mapping of {<member name>: (package, <module name>, deprecation_message)} defining dynamic imports
_dynamic_imports: 'dict[str, tuple[str, str, str | None]]' = {
'RootModel': (__package__, '.root_model', None),
'FieldValidationInfo': (
'pydantic_core',
'.core_schema',
'`FieldValidationInfo` is deprecated, use `ValidationInfo` instead',
),
# A mapping of {<member name>: (package, <module name>)} defining dynamic imports
_dynamic_imports: 'dict[str, tuple[str, str]]' = {
'RootModel': (__package__, '.root_model'),
'FieldValidationInfo': ('pydantic_core', '.core_schema'),
}
if typing.TYPE_CHECKING:
from .root_model import RootModel
Expand All @@ -216,11 +212,7 @@ def __getattr__(attr_name: str) -> object:
if dynamic_attr is None:
return _getattr_migration(attr_name)

package, module_name, deprecation_message = dynamic_attr
if deprecation_message is not None:
import warnings

warnings.warn(deprecation_message, DeprecationWarning, stacklevel=1)
package, module_name = dynamic_attr

from importlib import import_module

Expand Down
2 changes: 1 addition & 1 deletion pydantic/_internal/_decorators_v1.py
Expand Up @@ -55,7 +55,7 @@ def can_be_keyword(param: Parameter) -> bool:
return param.kind in (Parameter.POSITIONAL_OR_KEYWORD, Parameter.KEYWORD_ONLY)


def make_generic_v1_field_validator(validator: V1Validator) -> core_schema.FieldValidatorFunction:
def make_generic_v1_field_validator(validator: V1Validator) -> core_schema.WithInfoValidatorFunction:
"""Wrap a V1 style field validator for V2 compatibility.
Args:
Expand Down
1 change: 1 addition & 0 deletions pydantic/_internal/_generate_schema.py
Expand Up @@ -263,6 +263,7 @@ def _add_custom_serialization_from_json_encoders(

class GenerateSchema:
"""Generate core schema for a Pydantic model, dataclass and types like `str`, `datetime`, ... ."""

__slots__ = (
'_config_wrapper_stack',
'_types_namespace',
Expand Down
11 changes: 3 additions & 8 deletions pydantic/functional_validators.py
Expand Up @@ -200,11 +200,7 @@ class Model(BaseModel):
```
"""

func: (
core_schema.NoInfoWrapValidatorFunction
| core_schema.GeneralWrapValidatorFunction
| core_schema.FieldWrapValidatorFunction
)
func: core_schema.NoInfoWrapValidatorFunction | core_schema.WithInfoWrapValidatorFunction

def __get_pydantic_core_schema__(self, source_type: Any, handler: _GetCoreSchemaHandler) -> core_schema.CoreSchema:
schema = handler(source_type)
Expand Down Expand Up @@ -237,15 +233,14 @@ def __call__(

_V2Validator = Union[
_V2ValidatorClsMethod,
_core_schema.FieldValidatorFunction,
_core_schema.WithInfoValidatorFunction,
_OnlyValueValidatorClsMethod,
_core_schema.NoInfoValidatorFunction,
]

_V2WrapValidator = Union[
_V2WrapValidatorClsMethod,
_core_schema.GeneralWrapValidatorFunction,
_core_schema.FieldWrapValidatorFunction,
_core_schema.WithInfoWrapValidatorFunction,
]

_PartialClsOrStaticMethod: TypeAlias = Union[classmethod[Any, Any, Any], staticmethod[Any, Any], partialmethod[Any]]
Expand Down
7 changes: 4 additions & 3 deletions tests/test_exports.py
Expand Up @@ -24,15 +24,16 @@ def test_init_export():
continue
exported.add(name)

# add stuff from `pydantic._dynamic_imports` if `dep_message` is None
exported.update({k for k, v in pydantic._dynamic_imports.items() if v[2] is None})
# add stuff from `pydantic._dynamic_imports` if `package` is "pydantic"
exported.update({k for k, v in pydantic._dynamic_imports.items() if v[0] == 'pydantic'})

assert pydantic_all == exported, "pydantic.__all__ doesn't match actual exports"


@pytest.mark.filterwarnings('ignore::DeprecationWarning')
@pytest.mark.parametrize(('attr_name', 'value'), list(pydantic._dynamic_imports.items()))
def test_public_api_dynamic_imports(attr_name, value):
package, module_name, deprecation_message = value
package, module_name = value
imported_object = getattr(importlib.import_module(module_name, package=package), attr_name)
assert isinstance(imported_object, object)

Expand Down

0 comments on commit 8ad7752

Please sign in to comment.