8000 Use `Any` for field type in `make_dataclass` by alexei · Pull Request #11657 · python/typeshed · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions stdlib/dataclasses.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
15 changes: 13 additions & 2 deletions test_cases/stdlib/check_dataclasses.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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, ...])
0