Skip to content

Commit

Permalink
build(deps): bump mypy from 0.920 to 0.930 (pydantic#3573)
Browse files Browse the repository at this point in the history
* build(deps): bump mypy from 0.920 to 0.930

* fix: avoid mypy plugin crash

Due to python/mypy#11332, mypy would crash
because `__builtins__` is not part of `ctx.api` modules, `builtins` is

* fix tests
  • Loading branch information
PrettyWood authored and Nash Taylor committed Jun 24, 2022
1 parent 3a6ea7a commit 8db880a
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 23 deletions.
16 changes: 8 additions & 8 deletions pydantic/class_validators.py
Expand Up @@ -54,7 +54,7 @@ def validator(
check_fields: bool = True,
whole: bool = None,
allow_reuse: bool = False,
) -> Callable[[AnyCallable], classmethod]:
) -> Callable[[AnyCallable], classmethod]: # type: ignore[type-arg]
"""
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 +81,7 @@ def validator(
assert each_item is False, '"each_item" and "whole" conflict, remove "whole"'
each_item = not whole

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


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


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


def root_validator(
_func: Optional[AnyCallable] = None, *, pre: bool = False, allow_reuse: bool = False, skip_on_failure: bool = False
) -> Union[classmethod, Callable[[AnyCallable], classmethod]]:
) -> Union[classmethod, Callable[[AnyCallable], classmethod]]: # type: ignore[type-arg]
"""
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 +122,7 @@ def root_validator(
)
return f_cls

def dec(f: AnyCallable) -> classmethod:
def dec(f: AnyCallable) -> classmethod: # type: ignore[type-arg]
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 +132,7 @@ def dec(f: AnyCallable) -> classmethod:
return dec


def _prepare_validator(function: AnyCallable, allow_reuse: bool) -> classmethod:
def _prepare_validator(function: AnyCallable, allow_reuse: bool) -> classmethod: # type: ignore[type-arg]
"""
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 +325,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]:
def gather_all_validators(type_: 'ModelOrDc') -> Dict[str, classmethod]: # type: ignore[type-arg]
all_attributes = ChainMap(*[cls.__dict__ for cls in type_.__mro__])
return {
k: v
Expand Down
6 changes: 3 additions & 3 deletions pydantic/main.py
Expand Up @@ -890,7 +890,7 @@ def create_model(
__config__: Optional[Type[BaseConfig]] = None,
__base__: None = None,
__module__: str = __name__,
__validators__: Dict[str, classmethod] = None,
__validators__: Dict[str, classmethod] = None, # type: ignore[type-arg]
**field_definitions: Any,
) -> Type['BaseModel']:
...
Expand All @@ -903,7 +903,7 @@ def create_model(
__config__: Optional[Type[BaseConfig]] = None,
__base__: Union[Type['Model'], Tuple[Type['Model'], ...]],
__module__: str = __name__,
__validators__: Dict[str, classmethod] = None,
__validators__: Dict[str, classmethod] = None, # type: ignore[type-arg]
**field_definitions: Any,
) -> Type['Model']:
...
Expand All @@ -915,7 +915,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,
__validators__: Dict[str, classmethod] = None, # type: ignore[type-arg]
**field_definitions: Any,
) -> Type['Model']:
"""
Expand Down
6 changes: 3 additions & 3 deletions pydantic/mypy.py
Expand Up @@ -348,13 +348,13 @@ def add_construct_method(self, fields: List['PydanticModelField']) -> None:
and does not treat settings fields as optional.
"""
ctx = self._ctx
set_str = ctx.api.named_type('__builtins__.set', [ctx.api.named_type('__builtins__.str')])
set_str = ctx.api.named_type('builtins.set', [ctx.api.named_type('builtins.str')])
optional_set_str = UnionType([set_str, NoneType()])
fields_set_argument = Argument(Var('_fields_set', optional_set_str), optional_set_str, None, ARG_OPT)
construct_arguments = self.get_field_arguments(fields, typed=True, force_all_optional=False, use_alias=False)
construct_arguments = [fields_set_argument] + construct_arguments

obj_type = ctx.api.named_type('__builtins__.object')
obj_type = ctx.api.named_type('builtins.object')
self_tvar_name = '_PydanticBaseModel' # Make sure it does not conflict with other names in the class
tvar_fullname = ctx.cls.fullname + '.' + self_tvar_name
self_type = TypeVarType(self_tvar_name, tvar_fullname, -1, [], obj_type)
Expand Down Expand Up @@ -654,7 +654,7 @@ def add_method(
arg_names.append(get_name(arg.variable))
arg_kinds.append(arg.kind)

function_type = ctx.api.named_type('__builtins__.function')
function_type = ctx.api.named_type('builtins.function')
signature = CallableType(arg_types, arg_kinds, arg_names, return_type, function_type)
if tvar_like_type:
signature.variables = [tvar_like_type]
Expand Down
2 changes: 1 addition & 1 deletion pydantic/utils.py
Expand Up @@ -574,7 +574,7 @@ 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__}')
raise TypeError(f'Unexpected type of exclude value {items.__class__}') # type: ignore[attr-defined]
return items

@classmethod
Expand Down
4 changes: 2 additions & 2 deletions tests/mypy/outputs/plugin-fail-strict.txt
Expand Up @@ -33,7 +33,7 @@
186: error: Unexpected keyword argument "z" for "AliasGeneratorModel2" [call-arg]
189: error: Name "Missing" is not defined [name-defined]
197: error: No overload variant of "dataclass" matches argument type "Dict[<nothing>, <nothing>]" [call-overload]
197: note: Possible overload variant:
197: note: Possible overload variants:
197: note: def dataclass(*, init: bool = ..., repr: bool = ..., eq: bool = ..., order: bool = ..., unsafe_hash: bool = ..., frozen: bool = ..., config: Optional[Type[Any]] = ...) -> Callable[[Type[Any]], Type[Dataclass]]
197: note: <1 more non-matching overload not shown>
197: note: def dataclass(_cls: Type[Any], *, init: bool = ..., repr: bool = ..., eq: bool = ..., order: bool = ..., unsafe_hash: bool = ..., frozen: bool = ..., config: Optional[Type[Any]] = ...) -> Type[Dataclass]
219: error: Property "y" defined in "FrozenModel" is read-only [misc]
8 changes: 4 additions & 4 deletions tests/mypy/outputs/plugin-fail.txt
Expand Up @@ -18,11 +18,11 @@
101: error: Missing named argument "y" for "construct" of "Model" [call-arg]
103: error: Argument "x" to "construct" of "Model" has incompatible type "str"; expected "int" [arg-type]
156: error: Missing named argument "x" for "DynamicAliasModel2" [call-arg]
175: error: unused "type: ignore" comment
182: error: unused "type: ignore" comment
175: error: Unused "type: ignore" comment
182: error: Unused "type: ignore" comment
189: error: Name "Missing" is not defined [name-defined]
197: error: No overload variant of "dataclass" matches argument type "Dict[<nothing>, <nothing>]" [call-overload]
197: note: Possible overload variant:
197: note: Possible overload variants:
197: note: def dataclass(*, init: bool = ..., repr: bool = ..., eq: bool = ..., order: bool = ..., unsafe_hash: bool = ..., frozen: bool = ..., config: Optional[Type[Any]] = ...) -> Callable[[Type[Any]], Type[Dataclass]]
197: note: <1 more non-matching overload not shown>
197: note: def dataclass(_cls: Type[Any], *, init: bool = ..., repr: bool = ..., eq: bool = ..., order: bool = ..., unsafe_hash: bool = ..., frozen: bool = ..., config: Optional[Type[Any]] = ...) -> Type[Dataclass]
219: error: Property "y" defined in "FrozenModel" is read-only [misc]
2 changes: 1 addition & 1 deletion tests/requirements-linting.txt
Expand Up @@ -3,7 +3,7 @@ flake8==4.0.1
flake8-quotes==3.3.1
hypothesis==6.31.6
isort==5.10.1
mypy==0.920
mypy==0.930
pre-commit==2.16.0
pycodestyle==2.8.0
pyflakes==2.4.0
Expand Down
2 changes: 1 addition & 1 deletion tests/requirements-testing.txt
Expand Up @@ -2,7 +2,7 @@ coverage==6.2
hypothesis==6.31.6
# pin importlib-metadata as upper versions need typing-extensions to work if on python < 3.8
importlib-metadata==3.1.0;python_version<"3.8"
mypy==0.920
mypy==0.930
pytest==6.2.5
pytest-cov==3.0.0
pytest-mock==3.6.1
Expand Down

0 comments on commit 8db880a

Please sign in to comment.