Skip to content

Commit

Permalink
cleanup bumping mypy to 0.930, #3573
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Dec 29, 2021
1 parent 8ef492b commit 128cf48
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
19 changes: 11 additions & 8 deletions pydantic/class_validators.py
Expand Up @@ -9,6 +9,9 @@
from .typing import AnyCallable
from .utils import ROOT_KEY, in_ipython

if TYPE_CHECKING:
from .typing import AnyClassMethod


class Validator:
__slots__ = 'func', 'pre', 'each_item', 'always', 'check_fields', 'skip_on_failure'
Expand Down Expand Up @@ -54,7 +57,7 @@ def validator(
check_fields: bool = True,
whole: bool = None,
allow_reuse: bool = False,
) -> Callable[[AnyCallable], classmethod]: # type: ignore[type-arg]
) -> Callable[[AnyCallable], 'AnyClassMethod']:
"""
Decorate methods on the class indicating that they should be used to validate fields
:param fields: which field(s) the method should be called on
Expand All @@ -81,7 +84,7 @@ def validator(
assert each_item is False, '"each_item" and "whole" conflict, remove "whole"'
each_item = not whole

def dec(f: AnyCallable) -> classmethod: # type: ignore[type-arg]
def dec(f: AnyCallable) -> 'AnyClassMethod':
f_cls = _prepare_validator(f, allow_reuse)
setattr(
f_cls,
Expand All @@ -97,20 +100,20 @@ def dec(f: AnyCallable) -> classmethod: # type: ignore[type-arg]


@overload
def root_validator(_func: AnyCallable) -> classmethod: # type: ignore[type-arg]
def root_validator(_func: AnyCallable) -> 'AnyClassMethod':
...


@overload
def root_validator(
*, pre: bool = False, allow_reuse: bool = False, skip_on_failure: bool = False
) -> Callable[[AnyCallable], classmethod]: # type: ignore[type-arg]
) -> Callable[[AnyCallable], 'AnyClassMethod']:
...


def root_validator(
_func: Optional[AnyCallable] = None, *, pre: bool = False, allow_reuse: bool = False, skip_on_failure: bool = False
) -> Union[classmethod, Callable[[AnyCallable], classmethod]]: # type: ignore[type-arg]
) -> Union['AnyClassMethod', Callable[[AnyCallable], 'AnyClassMethod']]:
"""
Decorate methods on a model indicating that they should be used to validate (and perhaps modify) data either
before or after standard model parsing/validation is performed.
Expand All @@ -122,7 +125,7 @@ def root_validator(
)
return f_cls

def dec(f: AnyCallable) -> classmethod: # type: ignore[type-arg]
def dec(f: AnyCallable) -> 'AnyClassMethod':
f_cls = _prepare_validator(f, allow_reuse)
setattr(
f_cls, ROOT_VALIDATOR_CONFIG_KEY, Validator(func=f_cls.__func__, pre=pre, skip_on_failure=skip_on_failure)
Expand All @@ -132,7 +135,7 @@ def dec(f: AnyCallable) -> classmethod: # type: ignore[type-arg]
return dec


def _prepare_validator(function: AnyCallable, allow_reuse: bool) -> classmethod: # type: ignore[type-arg]
def _prepare_validator(function: AnyCallable, allow_reuse: bool) -> 'AnyClassMethod':
"""
Avoid validators with duplicated names since without this, validators can be overwritten silently
which generally isn't the intended behaviour, don't run in ipython (see #312) or if allow_reuse is False.
Expand Down Expand Up @@ -325,7 +328,7 @@ def _generic_validator_basic(validator: AnyCallable, sig: 'Signature', args: Set
return lambda cls, v, values, field, config: validator(v, values=values, field=field, config=config)


def gather_all_validators(type_: 'ModelOrDc') -> Dict[str, classmethod]: # type: ignore[type-arg]
def gather_all_validators(type_: 'ModelOrDc') -> Dict[str, 'AnyClassMethod']:
all_attributes = ChainMap(*[cls.__dict__ for cls in type_.__mro__])
return {
k: v
Expand Down
7 changes: 4 additions & 3 deletions pydantic/main.py
Expand Up @@ -66,6 +66,7 @@
from .types import ModelOrDc
from .typing import (
AbstractSetIntStr,
AnyClassMethod,
CallableGenerator,
DictAny,
DictStrAny,
Expand Down Expand Up @@ -890,7 +891,7 @@ def create_model(
__config__: Optional[Type[BaseConfig]] = None,
__base__: None = None,
__module__: str = __name__,
__validators__: Dict[str, classmethod] = None, # type: ignore[type-arg]
__validators__: Dict[str, 'AnyClassMethod'] = None,
**field_definitions: Any,
) -> Type['BaseModel']:
...
Expand All @@ -903,7 +904,7 @@ def create_model(
__config__: Optional[Type[BaseConfig]] = None,
__base__: Union[Type['Model'], Tuple[Type['Model'], ...]],
__module__: str = __name__,
__validators__: Dict[str, classmethod] = None, # type: ignore[type-arg]
__validators__: Dict[str, 'AnyClassMethod'] = None,
**field_definitions: Any,
) -> Type['Model']:
...
Expand All @@ -915,7 +916,7 @@ def create_model(
__config__: Optional[Type[BaseConfig]] = None,
__base__: Union[None, Type['Model'], Tuple[Type['Model'], ...]] = None,
__module__: str = __name__,
__validators__: Dict[str, classmethod] = None, # type: ignore[type-arg]
__validators__: Dict[str, 'AnyClassMethod'] = None,
**field_definitions: Any,
) -> Type['Model']:
"""
Expand Down
2 changes: 2 additions & 0 deletions pydantic/typing.py
Expand Up @@ -228,6 +228,7 @@ def is_union(tp: Optional[Type[Any]]) -> bool:
MappingIntStrAny = Mapping[IntStr, Any]
CallableGenerator = Generator[AnyCallable, None, None]
ReprArgs = Sequence[Tuple[Optional[str], Any]]
AnyClassMethod = classmethod[Any]

__all__ = (
'ForwardRef',
Expand Down Expand Up @@ -258,6 +259,7 @@ def is_union(tp: Optional[Type[Any]]) -> bool:
'DictIntStrAny',
'CallableGenerator',
'ReprArgs',
'AnyClassMethod',
'CallableGenerator',
'WithArgsTypes',
'get_args',
Expand Down
3 changes: 2 additions & 1 deletion pydantic/utils.py
Expand Up @@ -574,7 +574,8 @@ def _coerce_items(items: Union['AbstractSetIntStr', 'MappingIntStrAny']) -> 'Map
elif isinstance(items, AbstractSet):
items = dict.fromkeys(items, ...)
else:
raise TypeError(f'Unexpected type of exclude value {items.__class__}') # type: ignore[attr-defined]
class_name = getattr(items, '__class__', '???')
raise TypeError(f'Unexpected type of exclude value {class_name}')
return items

@classmethod
Expand Down

0 comments on commit 128cf48

Please sign in to comment.