10000 Bump mypy from 0.770 to 0.780 (#1598) · ag-python/pydantic@70d531f · GitHub
[go: up one dir, main page]

Skip to content

Commit 70d531f

Browse files
Bump mypy from 0.770 to 0.780 (pydantic#1598)
* 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](python/mypy@v0.770...v0.780) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> * fix mypy errors and remove AnyType * fix python 3.6 inconsistencies * linting * tweak typing * fix typing for 3.6 * bump Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> Co-authored-by: Samuel Colvin <s@muelcolvin.com>
1 parent 97b6b46 commit 70d531f

File tree

12 files changed

+54
-47
lines changed

12 files changed

+54
-47
lines changed

changes/1598-samuelcolvin.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Update mypy, remove `AnyType` alias for `Type[Any]`

pydantic/dataclasses.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from .errors import DataclassTypeError
66
from .fields import Required
77
from .main import create_model, validate_model
8-
from .typing import AnyType
98

109
if TYPE_CHECKING:
1110
from .main import BaseModel # noqa: F401
@@ -56,7 +55,7 @@ def setattr_validate_assignment(self: 'DataclassType', name: str, value: Any) ->
5655

5756

5857
def _process_class(
59-
_cls: AnyType,
58+
_cls: Type[Any],
6059
init: bool,
6160
repr: bool,
6261
eq: bool,
@@ -120,7 +119,7 @@ def _pydantic_post_init(self: 'DataclassType', *initvars: Any) -> None:
120119

121120

122121
def dataclass(
123-
_cls: Optional[AnyType] = None,
122+
_cls: Optional[Type[Any]] = None,
124123
*,
125124
init: bool = True,
126125
repr: bool = True,
@@ -129,15 +128,15 @@ def dataclass(
129128
unsafe_hash: bool = False,
130129
frozen: bool = False,
131130
config: Type[Any] = None,
132-
) -> Union[Callable[[AnyType], 'DataclassType'], 'DataclassType']:
131+
) -> Union[Callable[[Type[Any]], 'DataclassType'], 'DataclassType']:
133132
"""
134133
Like the python standard lib dataclasses but with type validation.
135134
136135
Arguments are the same as for standard dataclasses, except for validate_assignment which has the same meaning
137136
as Config.validate_assignment.
138137
"""
139138

140-
def wrap(cls: AnyType) -> 'DataclassType':
139+
def wrap(cls: Type[Any]) -> 'DataclassType':
141140
return _process_class(cls, init, repr, eq, order, unsafe_hash, frozen, config)
142141

143142
if _cls is None:

pydantic/errors.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from decimal import Decimal
22
from pathlib import Path
3-
from typing import Any, Set, Union
3+
from typing import Any, Set, Type, Union
44

5-
from .typing import AnyType, display_as_type
5+
from .typing import display_as_type
66

77
# explicitly state exports to avoid "from .errors import *" also importing Decimal, Path etc.
88
__all__ = (
@@ -413,7 +413,7 @@ class ArbitraryTypeError(PydanticTypeError):
413413
code = 'arbitrary_type'
414414
msg_template = 'instance of {expected_arbitrary_type} expected'
415415

416-
def __init__(self, *, expected_arbitrary_type: AnyType) -> None:
416+
def __init__(self, *, expected_arbitrary_type: Type[Any]) -> None:
417417
super().__init__(expected_arbitrary_type=display_as_type(expected_arbitrary_type))
418418

419419

@@ -426,7 +426,7 @@ class SubclassError(PydanticTypeError):
426426
code = 'subclass'
427427
msg_template = 'subclass of {expected_class} expected'
428428

429-
def __init__(self, *, expected_class: AnyType) -> None:
429+
def __init__(self, *, expected_class: Type[Any]) -> None:
430430
super().__init__(expected_class=display_as_type(expected_class))
431431

432432

pydantic/fields.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
from .errors import NoneIsNotAllowedError
2828
from .types import Json, JsonWrapper
2929
from .typing import (
30-
AnyType,
3130
Callable,
3231
ForwardRef,
3332
NoArgAnyCallable,
@@ -237,7 +236,7 @@ def __init__(
237236
self,
238237
*,
239238
name: str,
240-
type_: AnyType,
239+
type_: Type[Any],
241240
class_validators: Optional[Dict[str, Validator]],
242241
model_config: Type['BaseConfig'],
243242
default: Any = None,
@@ -486,7 +485,7 @@ def _type_analysis(self) -> None: # noqa: C901 (ignore complexity)
486485
# type_ has been refined eg. as the type of a List and sub_fields needs to be populated
487486
self.sub_fields = [self._create_sub_type(self.type_, '_' + self.name)]
488487

489-
def _create_sub_type(self, type_: AnyType, name< 10000 /span>: str, *, for_keys: bool = False) -> 'ModelField':
488+
def _create_sub_type(self, type_: Type[Any], name: str, *, for_keys: bool = False) -> 'ModelField':
490489
return self.__class__(
491490
type_=type_,
492491
name=name,

pydantic/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from .parse import Protocol, load_file, load_str_bytes
3434
from .schema import model_schema
3535
from .types import PyObject, StrBytes
36-
from .typing import AnyCallable, AnyType, ForwardRef, is_classvar, resolve_annotations, update_field_forward_refs
36+
from .typing import AnyCallable, ForwardRef, is_classvar, resolve_annotations, update_field_forward_refs
3737
from .utils import (
3838
ClassAttribute,
3939
GetterDict,
@@ -110,7 +110,7 @@ class BaseConfig:
110110
schema_extra: Union[Dict[str, Any], 'SchemaExtraCallable'] = {}
111111
json_loads: Callable[[str], Any] = json.loads
112112
json_dumps: Callable[..., str] = json.dumps
113-
json_encoders: Dict[AnyType, AnyCallable] = {}
113+
json_encoders: Dict[Type[Any], AnyCallable] = {}
114114

115115
@classmethod
116116
def get_field_info(cls, name: str) -> Dict[str, Any]:

pydantic/schema.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ def field_singleton_schema( # noqa: C901 (ignore complexity)
737737
def multivalue_literal_field_for_schema(values: Tuple[Any, ...], field: ModelField) -> ModelField:
738738
return ModelField(
739739
name=field.name,
740-
type_=Union[tuple(Literal[value] for value in values)],
740+
type_=Union[tuple(Literal[value] for value in values)], # type: ignore
741741
class_validators=field.class_validators,
742742
model_config=field.model_config,
743743
default=field.default,
@@ -801,7 +801,7 @@ def go(type_: Any) -> Type[Any]:
801801
return type_
802802

803803
if origin is Union:
804-
return Union[tuple(go(a) for a in args)]
804+
return Union[tuple(go(a) for a in args)] # type: ignore
805805

806806
if issubclass(origin, List) and (field_info.min_items is not None or field_info.max_items is not None):
807807
used_constraints.update({'min_items', 'max_items'})

pydantic/types.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from uuid import UUID
1010

1111
from . import errors
12-
from .typing import AnyType
1312
from .utils import import_string, update_not_none
1413
from .validators import (
1514
bytes_validator,
@@ -527,7 +526,7 @@ class JsonWrapper:
527526

528527

529528
class JsonMeta(type):
530-
def __getitem__(self, t: AnyType) -> Type[JsonWrapper]:
529+
def __getitem__(self, t: Type[Any]) -> Type[JsonWrapper]:
531530
return type('JsonWrapperValue', (JsonWrapper,), {'inner_type': t})
532531

533532

pydantic/typing.py

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,32 @@
2424
except ImportError:
2525
from typing import _Final as typing_base # type: ignore
2626

27-
try:
28-
from typing import ForwardRef # type: ignore
2927

30-
def evaluate_forwardref(type_: ForwardRef, globalns: Any, localns: Any) -> Type[Any]:
31-
return type_._evaluate(globalns, localns)
28+
if sys.version_info < (3, 7):
29+
if TYPE_CHECKING:
3230

31+
class ForwardRef:
32+
def _eval_type(self, globalns: Any, localns: Any) -> Any:
33+
pass
34+
35+
else:
36+
from typing import _ForwardRef as ForwardRef
37+
else:
38+
from typing import ForwardRef
3339

34-
except ImportError:
35-
# python 3.6
36-
from typing import _ForwardRef as ForwardRef # type: ignore
3740

38-
def evaluate_forwardref(type_: ForwardRef, globalns: Any, localns: Any) -> Type[Any]:
41+
if sys.version_info < (3, 7):
42+
43+
def evaluate_forwardref(type_: ForwardRef, globalns: Any, localns: Any) -> Any:
3944
return type_._eval_type(globalns, localns)
4045

4146

47+
else:
48+
49+
def evaluate_forwardref(type_: ForwardRef, globalns: Any, localns: Any) -> Any:
50+
return type_._evaluate(globalns, localns)
51+
52+
4253
if sys.version_info < (3, 7):
4354
from typing import Callable as Callable
4455

@@ -82,7 +93,6 @@ def evaluate_forwardref(type_: ForwardRef, globalns: Any, localns: Any) -> Type[
8293
'Callable',
8394
'AnyCallable',
8495
'NoArgAnyCallable',
85-
'AnyType',
8696
'NoneType',
8797
'display_as_type',
8898
'resolve_annotations',
@@ -108,11 +118,10 @@ def evaluate_forwardref(type_: ForwardRef, globalns: Any, localns: Any) -> Type[
108118
)
109119

110120

111-
AnyType = Type[Any]
112121
NoneType = None.__class__
113122

114123

115-
def display_as_type(v: AnyType) -> str:
124+
def display_as_type(v: Type[Any]) -> str:
116125
if not isinstance(v, typing_base) and not isinstance(v, type):
117126
v = v.__class__
118127

@@ -131,7 +140,7 @@ def display_as_type(v: AnyType) -> str:
131140
return str(v).replace('typing.', '')
132141

133142

134-
def resolve_annotations(raw_annotations: Dict[str, AnyType], module_name: Optional[str]) -> Dict[str, AnyType]:
143+
def resolve_annotations(raw_annotations: Dict[str, Type[Any]], module_name: Optional[str]) -> Dict[str, Type[Any]]:
135144
"""
136145
Partially taken from typing.get_type_hints.
137146
@@ -157,25 +166,25 @@ def resolve_annotations(raw_annotations: Dict[str, AnyType], module_name: Option
157166
return annotations
158167

159168

160-
def is_callable_type(type_: AnyType) -> bool:
169+
def is_callable_type(type_: Type[Any]) -> bool:
161170
return type_ is Callable or getattr(type_, '__origin__', None) is Callable
162171

163172

164173
if sys.version_info >= (3, 7):
165174

166-
def is_literal_type(type_: AnyType) -> bool:
175+
def is_literal_type(type_: Type[Any]) -> bool:
167176
return Literal is not None and getattr(type_, '__origin__', None) is Literal
168177

169-
def literal_values(type_: AnyType) -> Tuple[Any, ...]:
178+
def literal_values(type_: Type[Any]) -> Tuple[Any, ...]:
170179
return type_.__args__
171180

172181

173182
else:
174183

175-
def is_literal_type(type_: AnyType) -> bool:
184+
def is_literal_type(type_: Type[Any]) -> bool:
176185
return Literal is not None and hasattr(type_, '__values__') and type_ == Literal[type_.__values__]
177186

178-
def literal_values(type_: AnyType) -> Tuple[Any, ...]:
187+
def literal_values(type_: Type[Any]) -> Tuple[Any, ...]:
179188
return type_.__values__
180189

181190

@@ -195,24 +204,24 @@ def all_literal_values(type_: Type[Any]) -> Tuple[Any, ...]:
195204
test_type = NewType('test_type', str)
196205

197206

198-
def is_new_type(type_: AnyType) -> bool:
207+
def is_new_type(type_: Type[Any]) -> bool:
199208
"""
200209
Check whether type_ was created using typing.NewType
201210
"""
202211
return isinstance(type_, test_type.__class__) and hasattr(type_, '__supertype__') # type: ignore
203212

204213

205-
def new_type_supertype(type_: AnyType) -> AnyType:
214+
def new_type_supertype(type_: Type[Any]) -> Type[Any]:
206215
while hasattr(type_, '__supertype__'):
207216
type_ = type_.__supertype__
208217
return type_
209218

210219

211-
def _check_classvar(v: AnyType) -> bool:
220+
def _check_classvar(v: Type[Any]) -> bool:
212221
return v.__class__ == ClassVar.__class__ and (sys.version_info < (3, 7) or getattr(v, '_name', None) == 'ClassVar')
213222

214223

215-
def is_classvar(ann_type: AnyType) -> bool:
224+
def is_classvar(ann_type: Type[Any]) -> bool:
216225
return _check_classvar(ann_type) or _check_classvar(getattr(ann_type, '__origin__', None))
217226

218227

@@ -228,7 +237,7 @@ def update_field_forward_refs(field: 'ModelField', globalns: Any, localns: Any)
228237
update_field_forward_refs(sub_f, globalns=globalns, localns=localns)
229238

230239

231-
def get_class(type_: AnyType) -> Union[None, bool, AnyType]:
240+
def get_class(type_: Type[Any]) -> Union[None, bool, Type[Any]]:
232241
"""
233242
Tries to get the class of a Type[T] annotation. Returns True if Type is used
234243
without brackets. Otherwise returns None.

pydantic/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
no_type_check,
2020
)
2121

22-
from .typing import AnyType, display_as_type
22+
from .typing import display_as_type
2323
from .version import version_info
2424

2525
if TYPE_CHECKING:
@@ -85,7 +85,7 @@ def truncate(v: Union[str], *, max_len: int = 80) -> str:
8585
return v
8686

8787

88-
def sequence_like(v: AnyType) -> bool:
88+
def sequence_like(v: Type[Any]) -> bool:
8989
return isinstance(v, (list, tuple, set, frozenset, GeneratorType))
9090

9191

@@ -101,7 +101,7 @@ def validate_field_name(bases: List[Type['BaseModel']], field_name: str) -> None
101101
)
102102

103103

104-
def lenient_issubclass(cls: Any, class_or_tuple: Union[AnyType, Tuple[AnyType, ...]]) -> bool:
104+
def lenient_issubclass(cls: Any, class_or_tuple: Union[Type[Any], Tuple[Type[Any], ...]]) -> bool:
105105
return isinstance(cls, type) and issubclass(cls, class_or_tuple)
106106

107107

pydantic/validators.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
from .datetime_parse import parse_date, parse_datetime, parse_duration, parse_time
2828
from .typing import (
2929
AnyCallable,
30-
AnyType,
3130
ForwardRef,
3231
all_literal_values,
3332
display_as_type,
@@ -502,7 +501,7 @@ def check(self, config: Type['BaseConfig']) -> bool:
502501

503502
# order is important here, for example: bool is a subclass of int so has to come first, datetime before date same,
504503
# IPv4Interface before IPv4Address, etc
505-
_VALIDATORS: List[Tuple[AnyType, List[Any]]] = [
504+
_VALIDATORS: List[Tuple[Type[Any], List[Any]]] = [
506505
(IntEnum, [int_validator, enum_validator]),
507506
(Enum, [enum_validator]),
508507
(
@@ -547,7 +546,7 @@ def check(self, config: Type['BaseConfig']) -> bool:
547546

548547

549548
def find_validators( # noqa: C901 (ignore complexity)
550-
type_: AnyType, config: Type['BaseConfig']
549+
type_: Type[Any], config: Type['BaseConfig']
551550
) -> Generator[AnyCallable, None, None]:
552551
if type_ is Any:
553552
return

0 commit comments

Comments
 (0)
0