Skip to main content

dataslots

PyPI - Python Version PyPI - Status license build status coverage

SLSA

Decorator for adding slots

In python 3.7 dataclasses module was introduced for faster class creation (PEP 557). Unfortunately, there's no support for __slots__ (basic support was added in 3.10). If you want to create more memory efficient instances, you need to do it by yourself or use @dataslots decorator.

Usage

Simple example

@dataslots
@dataclass
class Point2D:
    x: int
    y: int

Inheritance

As described in docs, in derived class __dict__ is created, because base class does not have __slots__. Slots are created from all defined properties (returned by dataclasses.fields() function).

@dataclass
class Base:
    a: int


@dataslots
@dataclass
class Derived(Base):
    c: int
    d: int

Dynamic assignment of new variables

@dataslots(add_dict=True)
@dataclass
class Point2D:
    x: int
    y: int
    
point = Point2D(10, 20)
point.length = math.sqrt(point.x ** 2 + point.y ** 2)

Weakref

@dataslots(add_weakref=True)
@dataclass
class Point2D:
    x: int
    y: int
    
point = Point2D(10, 20)
r = weakref.ref(point)

Read-only class variables

With __slots__ it's possible to define read-only class variables. When using dataclasses you cannot provide type for attribute or use typing.ClassVar to declare one.

@dataslots
@dataclass
class A:
    x = 5
    y: ClassVar[set] = set()

Pickling frozen dataclass

Because of an issue 36424 you need custom __setstate__ method. In dataslots there is implemented default version, and it is used if decorated class has no __getstate__ and __setstate__ function declared.

Added in 1.0.2

Data descriptors

Data descriptors are supported by inheritance from DataDescriptor (base class with required interface) or DataslotsDescriptor (class with additional features to simplify descriptor definition).

Check example directory for basic usage.

Added in 1.1.0

Typing support (PEP 561)

The package is PEP 561 compliant, so you can easily use it with mypy1 and pyright.

1 Due to some issues in mypy not all features are supported correctly (like dataclass alike interface or descriptors).

Added in 1.2.0

Backport

If you prefer using the newest dataclasses.dataclass interface you can use dataslots.dataclass wrapper to provide a consistent interface regardless of the python version.

Notice: Wrapper always uses dataslots to make all additional features available and slots=True is obligatory.

Added in 1.2.0

SLSA support

All packages from version 1.2.0 can be verified using SLSA provenance (dataslots package is compliant with SLSA Level 3).

If you want to verify dataslots before installing, you need to download SLSA verifier and run:

slsa-verifier verify-artifact \
--provenance-path dataslots.intoto.jsonl \
--source-uri github.com/starhel/dataslots \
--source-tag v${VER} \
${PATH_TO_PACKAGE}

VER is version of package download from PYPI or GH release. Provenance is only available in GH release as PYPI does not accept jsonl files.

More about __slots__

Release files for dataslots 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dataslots 1.2.0
File Size Uploaded
dataslots-1.2.0.tar.gz 12.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dataslots 1.2.0
File Interpreter ABI Platform
dataslots-1.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 18.8 kB

Release files / dataslots-1.2.0.tar.gz

Download URL dataslots-1.2.0.tar.gz
Size 12.7 kB
Tags Source
SHA-256 checksum
How to use checksums
08a6597094c8bf821eea68bb9f3c5f188867efa62206166678bbb5f5167dbb43
BLAKE2b-256 checksum
How to use checksums
6035ddad1ab94f5d4471f064d203e5d363b536f270c4d82ef89d5044a4c8fd25
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.1

Release files / dataslots-1.2.0-py3-none-any.whl

Download URL dataslots-1.2.0-py3-none-any.whl
Size 6.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
93322471730fbcc148b9458629567fa0fa9d4e9b664862860ab9910293c609b5
BLAKE2b-256 checksum
How to use checksums
afde4bc13dc9737e6a4e9a6fda8f8a20f2d295b6ea036210be4291ee2ddefded
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.1

Release history Release notifications | RSS feed

This release

1.2.0 This release

2 release files

1.1.0

2 release files

1.0.2

2 release files

1.0.1

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page