Skip to content

Commit b0aa890

Browse files
committed
Add dataclass behavior hint for type checker using dataclass_transform
1 parent 31e34ea commit b0aa890

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

dataclassabc/__init__.py

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
import sys
2-
import typing
32
from dataclasses import (_FIELD, _FIELD_INITVAR, _POST_INIT_NAME, MISSING,
43
_fields_in_init_order, _init_fn, _process_class,
5-
_set_new_attribute)
6-
from typing import Any, Generator, Tuple
4+
_set_new_attribute, Field, field)
5+
from typing import (Any, Generic, TypeVar, Type, Callable, Generator, Tuple,
6+
dataclass_transform)
7+
try:
8+
from typing import overload
9+
except ImportError:
10+
from typing_extensions import overload
11+
12+
13+
__all__ = ['dataclassabc']
14+
_T = TypeVar("_T")
715

816

917
def resolve_abc_prop(cls):
@@ -54,7 +62,7 @@ def set_func(self, val, key=key):
5462
get_set_properties = dict(gen_get_set_properties())
5563

5664
mro_filtered = tuple(
57-
mro for mro in cls.__mro__ if mro is not typing.Generic)
65+
mro for mro in cls.__mro__ if mro is not Generic)
5866

5967
new_cls = type(
6068
cls.__name__,
@@ -65,6 +73,43 @@ def set_func(self, val, key=key):
6573
return new_cls
6674

6775

76+
@overload
77+
@dataclass_transform(field_specifiers=(Field, field))
78+
def dataclassabc(
79+
_cls: None = None, /, *,
80+
init: bool = True,
81+
repr: bool = True,
82+
eq: bool = True,
83+
order: bool = False,
84+
unsafe_hash: bool = False,
85+
frozen: bool = False,
86+
match_args: bool = True,
87+
kw_only: bool = False,
88+
slots: bool = False,
89+
weakref_slot: bool = False
90+
) -> Callable[[Type[_T]], Type[_T]]:
91+
...
92+
93+
94+
@overload
95+
@dataclass_transform(field_specifiers=(Field, field))
96+
def dataclassabc(
97+
_cls: Type[_T], /, *,
98+
init: bool = True,
99+
repr: bool = True,
100+
eq: bool = True,
101+
order: bool = False,
102+
unsafe_hash: bool = False,
103+
frozen: bool = False,
104+
match_args: bool = True,
105+
kw_only: bool = False,
106+
slots: bool = False,
107+
weakref_slot: bool = False
108+
) -> Type[_T]:
109+
...
110+
111+
112+
@dataclass_transform(field_specifiers=(Field, field))
68113
def dataclassabc(_cls=None, /, *, init=True, repr=True, eq=True, order=False,
69114
unsafe_hash=False, frozen=False, match_args=True,
70115
kw_only=False, slots=False, weakref_slot=False):

dataclassabc/py.typed

Whitespace-only changes.

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
'Programming Language :: Python :: 3.11',
2323
],
2424
keywords='dataclass-abc abstract abc property',
25+
package_data={'dataclassabc': ['py.typed']},
2526
packages=['dataclassabc'],
2627
python_requires='>=3.10',
2728
)

0 commit comments

Comments
 (0)