Skip to content

Commit 45adbe2

Browse files
authored
💥 boom: replace DataclassInstance with optype.HasDataclassFields (#78)
* 💥 boom: replace DataclassInstance with opt.dataclasses.HasDataclassFields * ⬆️ dep-bump: optype v0.17.0 Signed-off-by: nstarman <nstarman@users.noreply.github.com>
1 parent 841444a commit 45adbe2

6 files changed

Lines changed: 20 additions & 54 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ classifiers = [
1818
"Typing :: Typed",
1919
]
2020
dependencies = [
21-
"optype>=0.16.0",
21+
"optype>=0.17.0",
2222
"plum-dispatch>=2.5.1",
2323
"typing_extensions>=4.12.2",
2424
]

src/dataclassish/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
"field_values",
1919
"field_items",
2020
# Classes
21-
"DataclassInstance",
2221
"F",
2322
)
2423

@@ -33,7 +32,7 @@
3332
get_field,
3433
replace,
3534
)
36-
from ._src.types import DataclassInstance, F
35+
from ._src.types import F
3736
from ._version import version as __version__
3837

3938
# Register dispatches by importing the submodules

src/dataclassish/_src/converters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ def field(
224224

225225

226226
class DataclassWithConvertersInstance(Protocol):
227-
"""DataclassInstance Protocol with additional ``__converter_init__``."""
227+
"""Dataclass instance Protocol with additional ``__converter_init__``."""
228228

229229
__dataclass_fields__: ClassVar[dict[str, dataclasses.Field[Any]]]
230230
__dataclass_init__: Callable[..., None]

src/dataclassish/_src/register_dataclass.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@
1313
)
1414
from typing import Any
1515

16+
import optype as opt
1617
from plum import dispatch
1718

1819
from .register_base import _recursive_replace_helper
19-
from .types import DataclassInstance
2020

2121
# ===================================================================
2222

2323
if sys.version_info < (3, 13):
2424

2525
@dispatch
26-
def get_field(obj: DataclassInstance, k: str, /) -> Any:
26+
def get_field(obj: opt.dataclasses.HasDataclassFields, k: str, /) -> Any:
2727
"""Get a field of a dataclass instance by name.
2828
2929
Examples:
@@ -49,7 +49,9 @@ def get_field(obj: DataclassInstance, k: str, /) -> Any:
4949
if sys.version_info < (3, 13):
5050

5151
@dispatch
52-
def replace(obj: DataclassInstance, /, **kwargs: Any) -> DataclassInstance:
52+
def replace(
53+
obj: opt.dataclasses.HasDataclassFields, /, **kwargs: Any
54+
) -> opt.dataclasses.HasDataclassFields:
5355
"""Replace the fields of a dataclass instance.
5456
5557
Examples:
@@ -72,7 +74,9 @@ def replace(obj: DataclassInstance, /, **kwargs: Any) -> DataclassInstance:
7274
return _dataclass_replace(obj, **kwargs)
7375

7476
@dispatch # type: ignore[no-redef]
75-
def replace(obj: DataclassInstance, fs: Mapping[str, Any], /) -> DataclassInstance:
77+
def replace(
78+
obj: opt.dataclasses.HasDataclassFields, fs: Mapping[str, Any], /
79+
) -> opt.dataclasses.HasDataclassFields:
7680
"""Replace the fields of a dataclass instance.
7781
7882
Examples:
@@ -127,7 +131,7 @@ def replace(obj: DataclassInstance, fs: Mapping[str, Any], /) -> DataclassInstan
127131

128132

129133
@dispatch
130-
def fields(obj: DataclassInstance, /) -> tuple[Field, ...]: # type: ignore[type-arg] # TODO: raise issue in beartype
134+
def fields(obj: opt.dataclasses.HasDataclassFields, /) -> tuple[Field, ...]: # type: ignore[type-arg] # TODO: raise issue in beartype
131135
"""Return the fields of a dataclass instance.
132136
133137
Examples:
@@ -154,7 +158,7 @@ def fields(obj: DataclassInstance, /) -> tuple[Field, ...]: # type: ignore[type
154158

155159
@dispatch
156160
def asdict(
157-
obj: DataclassInstance,
161+
obj: opt.dataclasses.HasDataclassFields,
158162
/,
159163
*,
160164
dict_factory: Callable[[list[tuple[str, Any]]], dict[str, Any]] = dict,
@@ -184,7 +188,7 @@ def asdict(
184188

185189
@dispatch
186190
def astuple(
187-
obj: DataclassInstance,
191+
obj: opt.dataclasses.HasDataclassFields,
188192
/,
189193
*,
190194
tuple_factory: Callable[[Any], tuple[Any, ...]] = tuple,

src/dataclassish/_src/types.py

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,9 @@
11
"""Data types for ``dataclassish``."""
22

3-
__all__ = ("DataclassInstance", "F")
3+
__all__ = ("F",)
44

55
from dataclasses import dataclass
6-
from typing import Any, ClassVar, Generic, Protocol, TypeVar, runtime_checkable
7-
8-
9-
@runtime_checkable
10-
class DataclassInstance(Protocol):
11-
"""Protocol for dataclass instances.
12-
13-
Examples
14-
--------
15-
>>> from dataclasses import dataclass
16-
>>> from dataclassish import DataclassInstance
17-
18-
>>> @dataclass
19-
... class Point:
20-
... x: float
21-
... y: float
22-
23-
>>> issubclass(Point, DataclassInstance)
24-
True
25-
26-
>>> point = Point(1.0, 2.0)
27-
28-
>>> isinstance(point, DataclassInstance)
29-
True
30-
31-
"""
32-
33-
__dataclass_fields__: ClassVar[dict[str, Any]]
34-
35-
# B/c of https://github.com/python/mypy/issues/3939 just having
36-
# `__dataclass_fields__` is insufficient for `issubclass` checks.
37-
@classmethod
38-
def __subclasshook__(cls: type, c: type) -> bool:
39-
"""Customize the subclass check."""
40-
return hasattr(c, "__dataclass_fields__")
41-
42-
43-
# ===================================================================
6+
from typing import Generic, TypeVar
447

458
V = TypeVar("V")
469

uv.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)