diff --git a/pydantic/dataclasses.py b/pydantic/dataclasses.py index f353de2..12d4c58 100644 --- a/pydantic/dataclasses.py +++ b/pydantic/dataclasses.py @@ -169,9 +169,8 @@ def _process_class( if field.default is not dataclasses.MISSING: default = field.default - # mypy issue 7020 and 708 - elif field.default_factory is not dataclasses.MISSING: # type: ignore - default_factory = field.default_factory # type: ignore + elif field.default_factory is not dataclasses.MISSING: + default_factory = field.default_factory else: default = Required diff --git a/pydantic/datetime_parse.py b/pydantic/datetime_parse.py index e4dff67..024a899 100644 --- a/pydantic/datetime_parse.py +++ b/pydantic/datetime_parse.py @@ -245,4 +245,4 @@ def parse_duration(value: StrBytesIntFloat) -> timedelta: kw_ = {k: float(v) for k, v in kw.items() if v is not None} - return sign * timedelta(**kw_) # type: ignore + return sign * timedelta(**kw_) diff --git a/pydantic/generics.py b/pydantic/generics.py index 1fee636..a712d26 100644 --- a/pydantic/generics.py +++ b/pydantic/generics.py @@ -258,6 +258,7 @@ def replace_types(type_: Any, type_map: Mapping[Any, Any]) -> Any: # `type` or `collections.abc.Callable` need to be translated. # See: https://www.python.org/dev/peps/pep-0585 origin_type = getattr(typing, type_._name) + assert origin_type is not None return origin_type[resolved_type_args] # We handle pydantic generic models separately as they don't have the same diff --git a/pydantic/mypy.py b/pydantic/mypy.py index c212099..d8ad93d 100644 --- a/pydantic/mypy.py +++ b/pydantic/mypy.py @@ -15,13 +15,6 @@ except ImportError: # pragma: no cover warnings.warn('No TOML parser installed, cannot read configuration from `pyproject.toml`.') toml = None # type: ignore -try: - from mypy.types import TypeVarDef -except ImportError: # pragma: no cover - # Backward-compatible with TypeVarDef from Mypy 0.910. - from mypy.types import TypeVarType as TypeVarDef # type: ignore[misc] - - from mypy.errorcodes import ErrorCode from mypy.nodes import ( ARG_NAMED, @@ -67,6 +60,7 @@ from mypy.types import ( Type, TypeOfAny, TypeType, + TypeVarLikeType, TypeVarType, UnionType, get_proper_type, @@ -363,23 +357,17 @@ class PydanticModelTransformer: 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 - tvd = TypeVarDef(self_tvar_name, tvar_fullname, -1, [], obj_type) + self_type = TypeVarType(self_tvar_name, tvar_fullname, -1, [], obj_type) self_tvar_expr = TypeVarExpr(self_tvar_name, tvar_fullname, [], obj_type) ctx.cls.info.names[self_tvar_name] = SymbolTableNode(MDEF, self_tvar_expr) - # Backward-compatible with TypeVarDef from Mypy 0.910. - if isinstance(tvd, TypeVarType): # pragma: no cover - self_type = tvd - else: - self_type = TypeVarType(tvd) - add_method( ctx, 'construct', construct_arguments, return_type=self_type, self_type=self_type, - tvar_def=tvd, + tvar_like_type=self_type, is_classmethod=True, ) @@ -631,7 +619,7 @@ def add_method( args: List[Argument], return_type: Type, self_type: Optional[Type] = None, - tvar_def: Optional[TypeVarDef] = None, + tvar_like_type: Optional[TypeVarLikeType] = None, is_classmethod: bool = False, is_new: bool = False, # is_staticmethod: bool = False, @@ -668,8 +656,8 @@ def add_method( function_type = ctx.api.named_type('__builtins__.function') signature = CallableType(arg_types, arg_kinds, arg_names, return_type, function_type) - if tvar_def: - signature.variables = [tvar_def] + if tvar_like_type: + signature.variables = [tvar_like_type] func = FuncDef(name, args, Block([PassStmt()])) func.info = info diff --git a/pydantic/networks.py b/pydantic/networks.py index 53b4643..18a042c 100644 --- a/pydantic/networks.py +++ b/pydantic/networks.py @@ -285,6 +285,7 @@ class AnyUrl(str): tld = d.group('tld') if tld is None and not is_international: d = int_domain_regex().fullmatch(host) + assert d is not None tld = d.group('tld') is_international = True diff --git a/pydantic/typing.py b/pydantic/typing.py index b2742bf..c00d504 100644 --- a/pydantic/typing.py +++ b/pydantic/typing.py @@ -110,7 +110,7 @@ if sys.version_info < (3, 8): else: from typing import get_origin as _typing_get_origin - def get_origin(tp: Type[Any]) -> Type[Any]: + def get_origin(tp: Type[Any]) -> Optional[Type[Any]]: """ We can't directly use `typing.get_origin` since we need a fallback to support custom generic classes like `ConstrainedList` @@ -188,7 +188,7 @@ else: if sys.version_info < (3, 10): - def is_union(tp: Type[Any]) -> bool: + def is_union(tp: Optional[Type[Any]]) -> bool: return tp is Union WithArgsTypes = (TypingGenericAlias,) @@ -197,7 +197,7 @@ else: import types import typing - def is_union(tp: Type[Any]) -> bool: + def is_union(tp: Optional[Type[Any]]) -> bool: return tp is Union or tp is types.UnionType # noqa: E721 WithArgsTypes = (typing._GenericAlias, types.GenericAlias, types.UnionType) diff --git a/pydantic/utils.py b/pydantic/utils.py index d4ca693..f0a594c 100644 --- a/pydantic/utils.py +++ b/pydantic/utils.py @@ -164,16 +164,16 @@ def validate_field_name(bases: List[Type['BaseModel']], field_name: str) -> None ) -def lenient_isinstance(o: Any, class_or_tuple: Union[Type[Any], Tuple[Type[Any], ...]]) -> bool: +def lenient_isinstance(o: Any, class_or_tuple: Union[Type[Any], Tuple[Type[Any], ...], None]) -> bool: try: - return isinstance(o, class_or_tuple) + return isinstance(o, class_or_tuple) # type: ignore[arg-type] except TypeError: return False -def lenient_issubclass(cls: Any, class_or_tuple: Union[Type[Any], Tuple[Type[Any], ...]]) -> bool: +def lenient_issubclass(cls: Any, class_or_tuple: Union[Type[Any], Tuple[Type[Any], ...], None]) -> bool: try: - return isinstance(cls, type) and issubclass(cls, class_or_tuple) + return isinstance(cls, type) and issubclass(cls, class_or_tuple) # type: ignore[arg-type] except TypeError: if isinstance(cls, WithArgsTypes): return False diff --git a/tests/requirements-linting.txt b/tests/requirements-linting.txt index 40b92ad..259fa76 100644 --- a/tests/requirements-linting.txt +++ b/tests/requirements-linting.txt @@ -3,7 +3,7 @@ flake8==4.0.1 flake8-quotes==3.3.1 hypothesis==6.31.6 isort==5.10.1 -mypy==0.910 +mypy==0.920 pre-commit==2.16.0 pycodestyle==2.8.0 pyflakes==2.4.0 diff --git a/tests/requirements-testing.txt b/tests/requirements-testing.txt index 2e49644..892dba1 100644 --- a/tests/requirements-testing.txt +++ b/tests/requirements-testing.txt @@ -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.910 +mypy==0.920 pytest==6.2.5 pytest-cov==3.0.0 pytest-mock==3.6.1