Typing Stubs for Dataclasses
This package provides PEP 561 compliant type stubs for the standard library's `dataclasses` module. It enables static type checkers like MyPy, PyCharm, or pytype to correctly analyze code that utilizes dataclasses by providing type definitions. The current version is 0.6.6. It is important to note that this specific `types-dataclasses` package is unmaintained and will not receive further updates; all fixes and contributions for `dataclasses` stubs are now managed directly within the `typeshed` project.
Warnings
- deprecated The `types-dataclasses` PyPI package is explicitly marked as unmaintained and will not receive further updates. Stubs for `dataclasses` are now managed directly within the `typeshed` project and are typically bundled with or accessed by modern type checkers (e.g., MyPy, Pyright) automatically, making this separate package redundant.
- breaking Python 3.13 introduces a breaking change in the comparison semantics for `dataclasses.dataclass` instances, specifically concerning the `__eq__` method's implementation. This change may affect code that relies on the exact comparison behavior of dataclasses, particularly when objects are identical in memory.
- gotcha When using `from __future__ import annotations` (which became default in Python 3.10) with earlier Python versions (pre-3.7.6 or pre-3.8.1), inspecting type parameters of `dataclasses`' `__init__` methods could yield unexpected string representations instead of resolved types, hindering introspection-based tools.
- gotcha In Python 3.9, using unparameterized generic types from the `typing` module (e.g., `List` instead of `List[str]`) within `dataclasses` could lead to runtime errors when processed by certain libraries that introspect these types. While `types-dataclasses` provides stubs, the runtime behavior can still be problematic.
Install
-
pip install types-dataclasses
Imports
- dataclass
from dataclasses import dataclass
- field
from dataclasses import field
Quickstart
from dataclasses import dataclass
from typing import List
@dataclass
class Point:
x: int
y: int
@dataclass
class TaggedItem:
name: str
tags: List[str]
def process_point(p: Point):
print(f"Processing point: ({p.x}, {p.y})")
p = Point(10, 20)
process_point(p)
item = TaggedItem(name="Book", tags=["fiction", "novel"])
print(f"Item: {item.name}, Tags: {', '.join(item.tags)}")