summaryrefslogtreecommitdiffstats
path: root/Lib/dataclasses.py
diff options
context:
space:
mode:
authorEric V. Smith <ericvsmith@users.noreply.github.com>2018-01-06 17:41:53 (GMT)
committerGitHub <noreply@github.com>2018-01-06 17:41:53 (GMT)
commite7ba013d870012157f695ead7e3645c2828a7fc5 (patch)
treeda2f4f57fc040b4587544423090bc6082a2636f7 /Lib/dataclasses.py
parent3cd7c6e6eb43dbd7d7180503265772a67953e682 (diff)
downloadcpython-e7ba013d870012157f695ead7e3645c2828a7fc5.zip
cpython-e7ba013d870012157f695ead7e3645c2828a7fc5.tar.gz
cpython-e7ba013d870012157f695ead7e3645c2828a7fc5.tar.bz2
bpo-32499: Add dataclasses.is_dataclass(obj), which returns True if obj is a dataclass or an instance of one. (#5113)
Diffstat (limited to 'Lib/dataclasses.py')
-rw-r--r--Lib/dataclasses.py19
1 files changed, 13 insertions, 6 deletions
diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py
index eaaed63..b4786bf 100644
--- a/Lib/dataclasses.py
+++ b/Lib/dataclasses.py
@@ -16,6 +16,7 @@ __all__ = ['dataclass',
'astuple',
'make_dataclass',
'replace',
+ 'is_dataclass',
]
# Raised when an attempt is made to modify a frozen class.
@@ -615,11 +616,17 @@ def fields(class_or_instance):
return tuple(f for f in fields.values() if f._field_type is _FIELD)
-def _isdataclass(obj):
+def _is_dataclass_instance(obj):
"""Returns True if obj is an instance of a dataclass."""
return not isinstance(obj, type) and hasattr(obj, _MARKER)
+def is_dataclass(obj):
+ """Returns True if obj is a dataclass or an instance of a
+ dataclass."""
+ return hasattr(obj, _MARKER)
+
+
def asdict(obj, *, dict_factory=dict):
"""Return the fields of a dataclass instance as a new dictionary mapping
field names to field values.
@@ -639,12 +646,12 @@ def asdict(obj, *, dict_factory=dict):
dataclass instances. This will also look into built-in containers:
tuples, lists, and dicts.
"""
- if not _isdataclass(obj):
+ if not _is_dataclass_instance(obj):
raise TypeError("asdict() should be called on dataclass instances")
return _asdict_inner(obj, dict_factory)
def _asdict_inner(obj, dict_factory):
- if _isdataclass(obj):
+ if _is_dataclass_instance(obj):
result = []
for f in fields(obj):
value = _asdict_inner(getattr(obj, f.name), dict_factory)
@@ -678,12 +685,12 @@ def astuple(obj, *, tuple_factory=tuple):
tuples, lists, and dicts.
"""
- if not _isdataclass(obj):
+ if not _is_dataclass_instance(obj):
raise TypeError("astuple() should be called on dataclass instances")
return _astuple_inner(obj, tuple_factory)
def _astuple_inner(obj, tuple_factory):
- if _isdataclass(obj):
+ if _is_dataclass_instance(obj):
result = []
for f in fields(obj):
value = _astuple_inner(getattr(obj, f.name), tuple_factory)
@@ -751,7 +758,7 @@ def replace(obj, **changes):
# We're going to mutate 'changes', but that's okay because it's a new
# dict, even if called with 'replace(obj, **my_changes)'.
- if not _isdataclass(obj):
+ if not _is_dataclass_instance(obj):
raise TypeError("replace() should be called on dataclass instances")
# It's an error to have init=False fields in 'changes'.