Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for PEP 0681 - dataclass_transform #12

Merged
merged 1 commit into from
Nov 26, 2022
Merged
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
17 changes: 15 additions & 2 deletions runtype/dataclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import random
from copy import copy
import dataclasses
from typing import Union
from typing import Union, Any, Tuple, Callable, TypeVar
from abc import ABC, abstractmethod
import inspect

Expand Down Expand Up @@ -319,9 +319,22 @@ def _add_slots(cls, is_frozen):
return cls


# This is a super-ugly hack called PEP-0681. https://peps.python.org/pep-0681/
_T = TypeVar("_T")
def __dataclass_transform__(
*,
eq_default: bool = True,
order_default: bool = False,
kw_only_default: bool = False,
field_descriptors: Tuple[Union[type, Callable[..., Any]], ...] = (()),
) -> Callable[[_T], _T]:
return lambda a: a


@__dataclass_transform__(eq_default=True, order_default=True)
def dataclass(cls=None, *, check_types: Union[bool, str] = CHECK_TYPES,
config: Configuration = PythonConfiguration(),
init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=True, slots=False):
init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=True, slots=False) -> Any:
"""Runtype's dataclass is a drop-in replacement to Python's built-in dataclass, with added functionality.

**Differences from builtin dataclass:**
Expand Down