Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

dataclasses.is_dataclass

Tests whether an object is a data class or an instance of one.

Version: Python 3.14; repository image, production runtime not attested

Syntax

dataclasses.is_dataclass(obj)

Tests whether an object is a data class or an instance of one.

The predicate accepts both the class and its instances. An ordinary class sharing similar annotations fails the test.

Use this predicate before working with field metadata. A dataclass, its subclass and instances of either qualify, while a parameterized generic alias itself does not. To demand an instance also exclude type objects. Point and Point(4) both pass in the example; int fails.

from dataclasses import dataclass, is_dataclass
@dataclass
class Box[T]:
    value: T
class Child(Box):
    pass
assert is_dataclass(Box) and is_dataclass(Child) and is_dataclass(Child(1))
assert not is_dataclass(Box[int])
assert not isinstance(Box(1), type)

Examples

import dataclasses
@dataclasses.dataclass
class Point:
    x: int

print(dataclasses.is_dataclass(Point), dataclasses.is_dataclass(Point(4)))
print(dataclasses.is_dataclass(int))

Result

True True
False

Use this predicate before working with field metadata. A dataclass, its subclass and instances of either qualify, while a parameterized generic alias itself does not. To demand an instance also exclude type objects. Point and Point(4) both pass in the example; int fails.

Sources