Use Any for field type in make_dataclass (#11657)

Co-authored-by: Alex Waygood <alex.waygood@gmail.com>
This commit is contained in:
Alexandru Mărășteanu
2024-03-25 23:05:14 +00:00
committed by GitHub
parent db8e620e3d
commit 4df0725b48
2 changed files with 17 additions and 6 deletions

View File

@@ -243,7 +243,7 @@ class InitVar(Generic[_T], metaclass=_InitVarMeta):
if sys.version_info >= (3, 12):
def make_dataclass(
cls_name: str,
fields: Iterable[str | tuple[str, type] | tuple[str, type, Any]],
fields: Iterable[str | tuple[str, Any] | tuple[str, Any, Any]],
*,
bases: tuple[type, ...] = (),
namespace: dict[str, Any] | None = None,
@@ -263,7 +263,7 @@ if sys.version_info >= (3, 12):
elif sys.version_info >= (3, 11):
def make_dataclass(
cls_name: str,
fields: Iterable[str | tuple[str, type] | tuple[str, type, Any]],
fields: Iterable[str | tuple[str, Any] | tuple[str, Any, Any]],
*,
bases: tuple[type, ...] = (),
namespace: dict[str, Any] | None = None,
@@ -282,7 +282,7 @@ elif sys.version_info >= (3, 11):
elif sys.version_info >= (3, 10):
def make_dataclass(
cls_name: str,
fields: Iterable[str | tuple[str, type] | tuple[str, type, Any]],
fields: Iterable[str | tuple[str, Any] | tuple[str, Any, Any]],
*,
bases: tuple[type, ...] = (),
namespace: dict[str, Any] | None = None,
@@ -300,7 +300,7 @@ elif sys.version_info >= (3, 10):
else:
def make_dataclass(
cls_name: str,
fields: Iterable[str | tuple[str, type] | tuple[str, type, Any]],
fields: Iterable[str | tuple[str, Any] | tuple[str, Any, Any]],
*,
bases: tuple[type, ...] = (),
namespace: dict[str, Any] | None = None,

View File

@@ -1,8 +1,8 @@
from __future__ import annotations
import dataclasses as dc
from typing import TYPE_CHECKING, Any, Dict, Tuple, Type, Union
from typing_extensions import assert_type
from typing import TYPE_CHECKING, Any, Dict, FrozenSet, Tuple, Type, Union
from typing_extensions import Annotated, assert_type
if TYPE_CHECKING:
from _typeshed import DataclassInstance
@@ -88,3 +88,14 @@ def check_other_isdataclass_overloads(x: type, y: object) -> None:
assert_type(dc.asdict(y), Dict[str, Any])
assert_type(dc.astuple(y), Tuple[Any, ...])
dc.replace(y)
# Regression test for #11653
D = dc.make_dataclass(
"D", [("a", Union[int, None]), "y", ("z", Annotated[FrozenSet[bytes], "metadata"], dc.field(default=frozenset({b"foo"})))]
)
# Check that it's inferred by the type checker as a class object of some kind
# (but don't assert the exact type that `D` is inferred as,
# in case a type checker decides to add some special-casing for
# `make_dataclass` in the future)
assert_type(D.__mro__, Tuple[type, ...])