From 0ebacbe9ba7dfe4a2ff2c8541127fe317e83d19a Mon Sep 17 00:00:00 2001 From: hauntsaninja <> Date: Wed, 30 Jun 2021 19:34:39 -0700 Subject: [PATCH] dataclasses: work around default factory issues --- stdlib/dataclasses.pyi | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/stdlib/dataclasses.pyi b/stdlib/dataclasses.pyi index 8d4ab1b1efba..519da6d31dbe 100644 --- a/stdlib/dataclasses.pyi +++ b/stdlib/dataclasses.pyi @@ -1,10 +1,12 @@ import sys from typing import Any, Callable, Dict, Generic, Iterable, List, Mapping, Optional, Tuple, Type, TypeVar, Union, overload +from typing_extensions import Protocol if sys.version_info >= (3, 9): from types import GenericAlias _T = TypeVar("_T") +_T_co = TypeVar("_T_co", covariant=True) class _MISSING_TYPE: ... @@ -63,11 +65,15 @@ else: *, init: bool = ..., repr: bool = ..., eq: bool = ..., order: bool = ..., unsafe_hash: bool = ..., frozen: bool = ... ) -> Callable[[Type[_T]], Type[_T]]: ... +# See https://github.com/python/mypy/issues/10750 +class _DefaultFactory(Protocol[_T_co]): + def __call__(self) -> _T_co: ... + class Field(Generic[_T]): name: str type: Type[_T] default: _T - default_factory: Callable[[], _T] + default_factory: _DefaultFactory[_T] repr: bool hash: Optional[bool] init: bool