mirror of
https://github.com/davidhalter/typeshed.git
synced 2026-07-25 17:36:19 +08:00
Update dataclasses for 3.14 (#14016)
This commit is contained in:
+103
-3
@@ -5,7 +5,7 @@ from _typeshed import DataclassInstance
|
||||
from builtins import type as Type # alias to avoid name clashes with fields named "type"
|
||||
from collections.abc import Callable, Iterable, Mapping
|
||||
from types import GenericAlias
|
||||
from typing import Any, Generic, Literal, Protocol, TypeVar, overload
|
||||
from typing import Any, Generic, Literal, Protocol, TypeVar, overload, type_check_only
|
||||
from typing_extensions import Never, TypeIs
|
||||
|
||||
_T = TypeVar("_T")
|
||||
@@ -31,6 +31,25 @@ if sys.version_info >= (3, 10):
|
||||
|
||||
_DataclassT = TypeVar("_DataclassT", bound=DataclassInstance)
|
||||
|
||||
@type_check_only
|
||||
class _DataclassFactory(Protocol):
|
||||
def __call__(
|
||||
self,
|
||||
cls: type[_T],
|
||||
/,
|
||||
*,
|
||||
init: bool = True,
|
||||
repr: bool = True,
|
||||
eq: bool = True,
|
||||
order: bool = False,
|
||||
unsafe_hash: bool = False,
|
||||
frozen: bool = False,
|
||||
match_args: bool = True,
|
||||
kw_only: bool = False,
|
||||
slots: bool = False,
|
||||
weakref_slot: bool = False,
|
||||
) -> type[_T]: ...
|
||||
|
||||
# define _MISSING_TYPE as an enum within the type stubs,
|
||||
# even though that is not really its type at runtime
|
||||
# this allows us to use Literal[_MISSING_TYPE.MISSING]
|
||||
@@ -114,8 +133,27 @@ class Field(Generic[_T]):
|
||||
init: bool
|
||||
compare: bool
|
||||
metadata: types.MappingProxyType[Any, Any]
|
||||
|
||||
if sys.version_info >= (3, 14):
|
||||
doc: str | None
|
||||
|
||||
if sys.version_info >= (3, 10):
|
||||
kw_only: bool | Literal[_MISSING_TYPE.MISSING]
|
||||
|
||||
if sys.version_info >= (3, 14):
|
||||
def __init__(
|
||||
self,
|
||||
default: _T,
|
||||
default_factory: Callable[[], _T],
|
||||
init: bool,
|
||||
repr: bool,
|
||||
hash: bool | None,
|
||||
compare: bool,
|
||||
metadata: Mapping[Any, Any],
|
||||
kw_only: bool,
|
||||
doc: str | None,
|
||||
) -> None: ...
|
||||
elif sys.version_info >= (3, 10):
|
||||
def __init__(
|
||||
self,
|
||||
default: _T,
|
||||
@@ -144,7 +182,48 @@ class Field(Generic[_T]):
|
||||
|
||||
# NOTE: Actual return type is 'Field[_T]', but we want to help type checkers
|
||||
# to understand the magic that happens at runtime.
|
||||
if sys.version_info >= (3, 10):
|
||||
if sys.version_info >= (3, 14):
|
||||
@overload # `default` and `default_factory` are optional and mutually exclusive.
|
||||
def field(
|
||||
*,
|
||||
default: _T,
|
||||
default_factory: Literal[_MISSING_TYPE.MISSING] = ...,
|
||||
init: bool = True,
|
||||
repr: bool = True,
|
||||
hash: bool | None = None,
|
||||
compare: bool = True,
|
||||
metadata: Mapping[Any, Any] | None = None,
|
||||
kw_only: bool | Literal[_MISSING_TYPE.MISSING] = ...,
|
||||
doc: str | None = None,
|
||||
) -> _T: ...
|
||||
@overload
|
||||
def field(
|
||||
*,
|
||||
default: Literal[_MISSING_TYPE.MISSING] = ...,
|
||||
default_factory: Callable[[], _T],
|
||||
init: bool = True,
|
||||
repr: bool = True,
|
||||
hash: bool | None = None,
|
||||
compare: bool = True,
|
||||
metadata: Mapping[Any, Any] | None = None,
|
||||
kw_only: bool | Literal[_MISSING_TYPE.MISSING] = ...,
|
||||
doc: str | None = None,
|
||||
) -> _T: ...
|
||||
@overload
|
||||
def field(
|
||||
*,
|
||||
default: Literal[_MISSING_TYPE.MISSING] = ...,
|
||||
default_factory: Literal[_MISSING_TYPE.MISSING] = ...,
|
||||
init: bool = True,
|
||||
repr: bool = True,
|
||||
hash: bool | None = None,
|
||||
compare: bool = True,
|
||||
metadata: Mapping[Any, Any] | None = None,
|
||||
kw_only: bool | Literal[_MISSING_TYPE.MISSING] = ...,
|
||||
doc: str | None = None,
|
||||
) -> Any: ...
|
||||
|
||||
elif sys.version_info >= (3, 10):
|
||||
@overload # `default` and `default_factory` are optional and mutually exclusive.
|
||||
def field(
|
||||
*,
|
||||
@@ -237,7 +316,28 @@ class InitVar(Generic[_T], metaclass=type):
|
||||
@overload
|
||||
def __class_getitem__(cls, type: Any) -> InitVar[Any]: ... # pyright: ignore[reportInvalidTypeForm]
|
||||
|
||||
if sys.version_info >= (3, 12):
|
||||
if sys.version_info >= (3, 14):
|
||||
def make_dataclass(
|
||||
cls_name: str,
|
||||
fields: Iterable[str | tuple[str, Any] | tuple[str, Any, Any]],
|
||||
*,
|
||||
bases: tuple[type, ...] = (),
|
||||
namespace: dict[str, Any] | None = None,
|
||||
init: bool = True,
|
||||
repr: bool = True,
|
||||
eq: bool = True,
|
||||
order: bool = False,
|
||||
unsafe_hash: bool = False,
|
||||
frozen: bool = False,
|
||||
match_args: bool = True,
|
||||
kw_only: bool = False,
|
||||
slots: bool = False,
|
||||
weakref_slot: bool = False,
|
||||
module: str | None = None,
|
||||
decorator: _DataclassFactory = ...,
|
||||
) -> type: ...
|
||||
|
||||
elif sys.version_info >= (3, 12):
|
||||
def make_dataclass(
|
||||
cls_name: str,
|
||||
fields: Iterable[str | tuple[str, Any] | tuple[str, Any, Any]],
|
||||
|
||||
Reference in New Issue
Block a user