From 2a03356c936b184bfcd5838c001506e22747b193 Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Thu, 4 Jun 2020 06:17:48 +0000 Subject: [PATCH 1/7] Bump mypy from 0.770 to 0.780 Bumps [mypy](https://github.com/python/mypy) from 0.770 to 0.780. - [Release notes](https://github.com/python/mypy/releases) - [Commits](https://github.com/python/mypy/compare/v0.770...v0.780) Signed-off-by: dependabot-preview[bot] --- tests/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/requirements.txt b/tests/requirements.txt index fd2eae5a73..da1e3c5e07 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -4,7 +4,7 @@ Cython==0.29.19;sys_platform!='win32' flake8==3.8.2 flake8-quotes==3.2.0 isort==4.3.21 -mypy==0.770 +mypy==0.780 pycodestyle==2.6.0 pyflakes==2.2.0 pytest==5.4.2 From 293ba78577498e3e8a96256521b8c23a7e67a06c Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Thu, 11 Jun 2020 11:23:47 +0100 Subject: [PATCH 2/7] fix mypy errors and remove AnyType --- changes/1598-samuelcolvin.md | 1 + pydantic/dataclasses.py | 9 ++++----- pydantic/errors.py | 8 ++++---- pydantic/fields.py | 5 ++--- pydantic/main.py | 4 ++-- pydantic/schema.py | 4 ++-- pydantic/types.py | 3 +-- pydantic/typing.py | 32 +++++++++++++++----------------- pydantic/utils.py | 6 +++--- pydantic/validators.py | 5 ++--- 10 files changed, 36 insertions(+), 41 deletions(-) create mode 100644 changes/1598-samuelcolvin.md diff --git a/changes/1598-samuelcolvin.md b/changes/1598-samuelcolvin.md new file mode 100644 index 0000000000..60fd5015c6 --- /dev/null +++ b/changes/1598-samuelcolvin.md @@ -0,0 +1 @@ +Update mypy, remove `AnyType` alias for `Type[Any]` diff --git a/pydantic/dataclasses.py b/pydantic/dataclasses.py index 982b07008a..b5f9cb3bcf 100644 --- a/pydantic/dataclasses.py +++ b/pydantic/dataclasses.py @@ -5,7 +5,6 @@ from .errors import DataclassTypeError from .fields import Required from .main import create_model, validate_model -from .typing import AnyType if TYPE_CHECKING: from .main import BaseModel # noqa: F401 @@ -56,7 +55,7 @@ def setattr_validate_assignment(self: 'DataclassType', name: str, value: Any) -> def _process_class( - _cls: AnyType, + _cls: Type[Any], init: bool, repr: bool, eq: bool, @@ -120,7 +119,7 @@ def _pydantic_post_init(self: 'DataclassType', *initvars: Any) -> None: def dataclass( - _cls: Optional[AnyType] = None, + _cls: Optional[Type[Any]] = None, *, init: bool = True, repr: bool = True, @@ -129,7 +128,7 @@ def dataclass( unsafe_hash: bool = False, frozen: bool = False, config: Type[Any] = None, -) -> Union[Callable[[AnyType], 'DataclassType'], 'DataclassType']: +) -> Union[Callable[[Type[Any]], 'DataclassType'], 'DataclassType']: """ Like the python standard lib dataclasses but with type validation. @@ -137,7 +136,7 @@ def dataclass( as Config.validate_assignment. """ - def wrap(cls: AnyType) -> 'DataclassType': + def wrap(cls: Type[Any]) -> 'DataclassType': return _process_class(cls, init, repr, eq, order, unsafe_hash, frozen, config) if _cls is None: diff --git a/pydantic/errors.py b/pydantic/errors.py index e6029f4e55..c5e4ab927d 100644 --- a/pydantic/errors.py +++ b/pydantic/errors.py @@ -1,8 +1,8 @@ from decimal import Decimal from pathlib import Path -from typing import Any, Set, Union +from typing import Any, Set, Type, Union -from .typing import AnyType, display_as_type +from .typing import display_as_type # explicitly state exports to avoid "from .errors import *" also importing Decimal, Path etc. __all__ = ( @@ -413,7 +413,7 @@ class ArbitraryTypeError(PydanticTypeError): code = 'arbitrary_type' msg_template = 'instance of {expected_arbitrary_type} expected' - def __init__(self, *, expected_arbitrary_type: AnyType) -> None: + def __init__(self, *, expected_arbitrary_type: Type[Any]) -> None: super().__init__(expected_arbitrary_type=display_as_type(expected_arbitrary_type)) @@ -426,7 +426,7 @@ class SubclassError(PydanticTypeError): code = 'subclass' msg_template = 'subclass of {expected_class} expected' - def __init__(self, *, expected_class: AnyType) -> None: + def __init__(self, *, expected_class: Type[Any]) -> None: super().__init__(expected_class=display_as_type(expected_class)) diff --git a/pydantic/fields.py b/pydantic/fields.py index c49ad7f047..8967fcdf25 100644 --- a/pydantic/fields.py +++ b/pydantic/fields.py @@ -27,7 +27,6 @@ from .errors import NoneIsNotAllowedError from .types import Json, JsonWrapper from .typing import ( - AnyType, Callable, ForwardRef, NoArgAnyCallable, @@ -237,7 +236,7 @@ def __init__( self, *, name: str, - type_: AnyType, + type_: Type[Any], class_validators: Optional[Dict[str, Validator]], model_config: Type['BaseConfig'], default: Any = None, @@ -486,7 +485,7 @@ def _type_analysis(self) -> None: # noqa: C901 (ignore complexity) # type_ has been refined eg. as the type of a List and sub_fields needs to be populated self.sub_fields = [self._create_sub_type(self.type_, '_' + self.name)] - def _create_sub_type(self, type_: AnyType, name: str, *, for_keys: bool = False) -> 'ModelField': + def _create_sub_type(self, type_: Type[Any], name: str, *, for_keys: bool = False) -> 'ModelField': return self.__class__( type_=type_, name=name, diff --git a/pydantic/main.py b/pydantic/main.py index bb01d2e3cc..2e2243b2f1 100644 --- a/pydantic/main.py +++ b/pydantic/main.py @@ -33,7 +33,7 @@ from .parse import Protocol, load_file, load_str_bytes from .schema import model_schema from .types import PyObject, StrBytes -from .typing import AnyCallable, AnyType, ForwardRef, is_classvar, resolve_annotations, update_field_forward_refs +from .typing import AnyCallable, ForwardRef, is_classvar, resolve_annotations, update_field_forward_refs from .utils import ( ClassAttribute, GetterDict, @@ -106,7 +106,7 @@ class BaseConfig: schema_extra: Union[Dict[str, Any], 'SchemaExtraCallable'] = {} json_loads: Callable[[str], Any] = json.loads json_dumps: Callable[..., str] = json.dumps - json_encoders: Dict[AnyType, AnyCallable] = {} + json_encoders: Dict[Type[Any], AnyCallable] = {} @classmethod def get_field_info(cls, name: str) -> Dict[str, Any]: diff --git a/pydantic/schema.py b/pydantic/schema.py index 9562e74b46..6cd9ef7fa2 100644 --- a/pydantic/schema.py +++ b/pydantic/schema.py @@ -737,7 +737,7 @@ def field_singleton_schema( # noqa: C901 (ignore complexity) def multivalue_literal_field_for_schema(values: Tuple[Any, ...], field: ModelField) -> ModelField: return ModelField( name=field.name, - type_=Union[tuple(Literal[value] for value in values)], + type_=Union[tuple(Literal[value] for value in values)], # type: ignore class_validators=field.class_validators, model_config=field.model_config, default=field.default, @@ -801,7 +801,7 @@ def go(type_: Any) -> Type[Any]: return type_ if origin is Union: - return Union[tuple(go(a) for a in args)] + return Union[tuple(go(a) for a in args)] # type: ignore if issubclass(origin, List) and (field_info.min_items is not None or field_info.max_items is not None): used_constraints.update({'min_items', 'max_items'}) diff --git a/pydantic/types.py b/pydantic/types.py index 13e74c33cb..e1371de620 100644 --- a/pydantic/types.py +++ b/pydantic/types.py @@ -9,7 +9,6 @@ from uuid import UUID from . import errors -from .typing import AnyType from .utils import import_string, update_not_none from .validators import ( bytes_validator, @@ -527,7 +526,7 @@ class JsonWrapper: class JsonMeta(type): - def __getitem__(self, t: AnyType) -> Type[JsonWrapper]: + def __getitem__(self, t: Type[Any]) -> Type[JsonWrapper]: return type('JsonWrapperValue', (JsonWrapper,), {'inner_type': t}) diff --git a/pydantic/typing.py b/pydantic/typing.py index ed8c23c71f..b07befe178 100644 --- a/pydantic/typing.py +++ b/pydantic/typing.py @@ -25,10 +25,10 @@ from typing import _Final as typing_base # type: ignore try: - from typing import ForwardRef # type: ignore + from typing import ForwardRef def evaluate_forwardref(type_: ForwardRef, globalns: Any, localns: Any) -> Type[Any]: - return type_._evaluate(globalns, localns) + return type_._evaluate(globalns, localns) # type: ignore except ImportError: @@ -36,7 +36,7 @@ def evaluate_forwardref(type_: ForwardRef, globalns: Any, localns: Any) -> Type[ from typing import _ForwardRef as ForwardRef # type: ignore def evaluate_forwardref(type_: ForwardRef, globalns: Any, localns: Any) -> Type[Any]: - return type_._eval_type(globalns, localns) + return type_._eval_type(globalns, localns) # type: ignore if sys.version_info < (3, 7): @@ -82,7 +82,6 @@ def evaluate_forwardref(type_: ForwardRef, globalns: Any, localns: Any) -> Type[ 'Callable', 'AnyCallable', 'NoArgAnyCallable', - 'AnyType', 'NoneType', 'display_as_type', 'resolve_annotations', @@ -108,11 +107,10 @@ def evaluate_forwardref(type_: ForwardRef, globalns: Any, localns: Any) -> Type[ ) -AnyType = Type[Any] NoneType = None.__class__ -def display_as_type(v: AnyType) -> str: +def display_as_type(v: Type[Any]) -> str: if not isinstance(v, typing_base) and not isinstance(v, type): v = v.__class__ @@ -131,7 +129,7 @@ def display_as_type(v: AnyType) -> str: return str(v).replace('typing.', '') -def resolve_annotations(raw_annotations: Dict[str, AnyType], module_name: Optional[str]) -> Dict[str, AnyType]: +def resolve_annotations(raw_annotations: Dict[str, Type[Any]], module_name: Optional[str]) -> Dict[str, Type[Any]]: """ Partially taken from typing.get_type_hints. @@ -157,25 +155,25 @@ def resolve_annotations(raw_annotations: Dict[str, AnyType], module_name: Option return annotations -def is_callable_type(type_: AnyType) -> bool: +def is_callable_type(type_: Type[Any]) -> bool: return type_ is Callable or getattr(type_, '__origin__', None) is Callable if sys.version_info >= (3, 7): - def is_literal_type(type_: AnyType) -> bool: + def is_literal_type(type_: Type[Any]) -> bool: return Literal is not None and getattr(type_, '__origin__', None) is Literal - def literal_values(type_: AnyType) -> Tuple[Any, ...]: + def literal_values(type_: Type[Any]) -> Tuple[Any, ...]: return type_.__args__ else: - def is_literal_type(type_: AnyType) -> bool: + def is_literal_type(type_: Type[Any]) -> bool: return Literal is not None and hasattr(type_, '__values__') and type_ == Literal[type_.__values__] - def literal_values(type_: AnyType) -> Tuple[Any, ...]: + def literal_values(type_: Type[Any]) -> Tuple[Any, ...]: return type_.__values__ @@ -195,24 +193,24 @@ def all_literal_values(type_: Type[Any]) -> Tuple[Any, ...]: test_type = NewType('test_type', str) -def is_new_type(type_: AnyType) -> bool: +def is_new_type(type_: Type[Any]) -> bool: """ Check whether type_ was created using typing.NewType """ return isinstance(type_, test_type.__class__) and hasattr(type_, '__supertype__') # type: ignore -def new_type_supertype(type_: AnyType) -> AnyType: +def new_type_supertype(type_: Type[Any]) -> Type[Any]: while hasattr(type_, '__supertype__'): type_ = type_.__supertype__ return type_ -def _check_classvar(v: AnyType) -> bool: +def _check_classvar(v: Type[Any]) -> bool: return v.__class__ == ClassVar.__class__ and (sys.version_info < (3, 7) or getattr(v, '_name', None) == 'ClassVar') -def is_classvar(ann_type: AnyType) -> bool: +def is_classvar(ann_type: Type[Any]) -> bool: return _check_classvar(ann_type) or _check_classvar(getattr(ann_type, '__origin__', None)) @@ -228,7 +226,7 @@ def update_field_forward_refs(field: 'ModelField', globalns: Any, localns: Any) update_field_forward_refs(sub_f, globalns=globalns, localns=localns) -def get_class(type_: AnyType) -> Union[None, bool, AnyType]: +def get_class(type_: Type[Any]) -> Union[None, bool, Type[Any]]: """ Tries to get the class of a Type[T] annotation. Returns True if Type is used without brackets. Otherwise returns None. diff --git a/pydantic/utils.py b/pydantic/utils.py index fdd4f59a07..36a631efce 100644 --- a/pydantic/utils.py +++ b/pydantic/utils.py @@ -19,7 +19,7 @@ no_type_check, ) -from .typing import AnyType, display_as_type +from .typing import display_as_type from .version import version_info if TYPE_CHECKING: @@ -85,7 +85,7 @@ def truncate(v: Union[str], *, max_len: int = 80) -> str: return v -def sequence_like(v: AnyType) -> bool: +def sequence_like(v: Type[Any]) -> bool: return isinstance(v, (list, tuple, set, frozenset, GeneratorType)) @@ -101,7 +101,7 @@ def validate_field_name(bases: List[Type['BaseModel']], field_name: str) -> None ) -def lenient_issubclass(cls: Any, class_or_tuple: Union[AnyType, Tuple[AnyType, ...]]) -> bool: +def lenient_issubclass(cls: Any, class_or_tuple: Union[Type[Any], Tuple[Type[Any], ...]]) -> bool: return isinstance(cls, type) and issubclass(cls, class_or_tuple) diff --git a/pydantic/validators.py b/pydantic/validators.py index 3d05845305..a53c83119b 100644 --- a/pydantic/validators.py +++ b/pydantic/validators.py @@ -27,7 +27,6 @@ from .datetime_parse import parse_date, parse_datetime, parse_duration, parse_time from .typing import ( AnyCallable, - AnyType, ForwardRef, all_literal_values, display_as_type, @@ -502,7 +501,7 @@ def check(self, config: Type['BaseConfig']) -> bool: # order is important here, for example: bool is a subclass of int so has to come first, datetime before date same, # IPv4Interface before IPv4Address, etc -_VALIDATORS: List[Tuple[AnyType, List[Any]]] = [ +_VALIDATORS: List[Tuple[Type[Any], List[Any]]] = [ (IntEnum, [int_validator, enum_validator]), (Enum, [enum_validator]), ( @@ -547,7 +546,7 @@ def check(self, config: Type['BaseConfig']) -> bool: def find_validators( # noqa: C901 (ignore complexity) - type_: AnyType, config: Type['BaseConfig'] + type_: Type[Any], config: Type['BaseConfig'] ) -> Generator[AnyCallable, None, None]: if type_ is Any: return From 1f62628697048c4895121cffc7db185d636e5966 Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Thu, 11 Jun 2020 12:24:02 +0100 Subject: [PATCH 3/7] fix python 3.6 inconsistencies --- pydantic/typing.py | 16 ++++++++-------- setup.cfg | 1 + 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/pydantic/typing.py b/pydantic/typing.py index b07befe178..0085b38e1b 100644 --- a/pydantic/typing.py +++ b/pydantic/typing.py @@ -24,19 +24,19 @@ except ImportError: from typing import _Final as typing_base # type: ignore -try: - from typing import ForwardRef - def evaluate_forwardref(type_: ForwardRef, globalns: Any, localns: Any) -> Type[Any]: - return type_._evaluate(globalns, localns) # type: ignore +if sys.version_info < (3, 7): + # python 3.6 + from typing import _ForwardRef as ForwardRef + def evaluate_forwardref(type_: ForwardRef, globalns: Any, localns: Any) -> Type[Any]: + return type_._eval_type(globalns, localns) -except ImportError: - # python 3.6 - from typing import _ForwardRef as ForwardRef # type: ignore +else: + from typing import ForwardRef def evaluate_forwardref(type_: ForwardRef, globalns: Any, localns: Any) -> Type[Any]: - return type_._eval_type(globalns, localns) # type: ignore + return type_._evaluate(globalns, localns) # type: ignore if sys.version_info < (3, 7): diff --git a/setup.cfg b/setup.cfg index a2947c2fb9..83dc9b7b0c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -42,6 +42,7 @@ force_grid_wrap=0 combine_as_imports=True [mypy] +show_error_codes = True follow_imports = silent strict_optional = True warn_redundant_casts = True From 51220c94d9482524c98c3a11057ce6255c8f3211 Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Thu, 11 Jun 2020 12:33:03 +0100 Subject: [PATCH 4/7] linting --- pydantic/typing.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pydantic/typing.py b/pydantic/typing.py index 0085b38e1b..7aa91a7420 100644 --- a/pydantic/typing.py +++ b/pydantic/typing.py @@ -32,6 +32,7 @@ def evaluate_forwardref(type_: ForwardRef, globalns: Any, localns: Any) -> Type[Any]: return type_._eval_type(globalns, localns) + else: from typing import ForwardRef From b2aa6502d8e5f79894dccba1c2dc6fae7ca55981 Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Thu, 11 Jun 2020 14:26:15 +0100 Subject: [PATCH 5/7] tweak typing --- pydantic/typing.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/pydantic/typing.py b/pydantic/typing.py index 7aa91a7420..fde6d2b8dd 100644 --- a/pydantic/typing.py +++ b/pydantic/typing.py @@ -26,18 +26,21 @@ if sys.version_info < (3, 7): - # python 3.6 - from typing import _ForwardRef as ForwardRef + from typing import _ForwardRef as ForwardRef # type: ignore +else: + from typing import ForwardRef + - def evaluate_forwardref(type_: ForwardRef, globalns: Any, localns: Any) -> Type[Any]: +if sys.version_info < (3, 7): + + def evaluate_forwardref(type_: ForwardRef, globalns: Any, localns: Any) -> Any: return type_._eval_type(globalns, localns) else: - from typing import ForwardRef - def evaluate_forwardref(type_: ForwardRef, globalns: Any, localns: Any) -> Type[Any]: - return type_._evaluate(globalns, localns) # type: ignore + def evaluate_forwardref(type_: ForwardRef, globalns: Any, localns: Any) -> Any: + return type_._evaluate(globalns, localns) if sys.version_info < (3, 7): From 28f571ee6cf491f1d947d7d4b387c934a1d33a46 Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Thu, 25 Jun 2020 18:40:32 +0100 Subject: [PATCH 6/7] fix typing for 3.6 --- pydantic/typing.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pydantic/typing.py b/pydantic/typing.py index fde6d2b8dd..070691eebf 100644 --- a/pydantic/typing.py +++ b/pydantic/typing.py @@ -26,7 +26,14 @@ if sys.version_info < (3, 7): - from typing import _ForwardRef as ForwardRef # type: ignore + if TYPE_CHECKING: + + class ForwardRef: + def _eval_type(self, globalns: Any, localns: Any) -> Any: + pass + + else: + from typing import _ForwardRef as ForwardRef else: from typing import ForwardRef From f90d769c1105eff0f5e95d8bcf30062e48e8e416 Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Thu, 25 Jun 2020 19:13:12 +0100 Subject: [PATCH 7/7] bump