# dataslots
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/dataslots.svg)](https://pypi.org/project/dataslots/)
[![PyPI - Status](https://img.shields.io/pypi/status/dataslots.svg)](https://pypi.org/project/dataslots/)
![license](https://img.shields.io/github/license/starhel/dataslots.svg)
[![build status](https://github.com/starhel/dataslots/actions/workflows/tests.yml/badge.svg)](https://github.com/starhel/dataslots/actions)
[![coverage](https://img.shields.io/badge/coverage-100%25-success)](https://github.com/starhel/dataslots/actions)
[![SLSA](https://slsa.dev/images/gh-badge-level3.svg)](https://slsa.dev)
## Decorator for adding __slots__
In python 3.7 dataclasses module was introduced for faster class creation ([PEP 557](https://www.python.org/dev/peps/pep-0557/)).
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
```python
@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).
```python
@dataclass
class Base:
a: int
@dataslots
@dataclass
class Derived(Base):
c: int
d: int
```
### Dynamic assignment of new variables
```python
@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
```python
@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.
```python
@dataslots
@dataclass
class A:
x = 5
y: ClassVar[set] = set()
```
### Pickling frozen dataclass
Because of an [issue 36424](https://bugs.python.org/issue36424) 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](https://docs.python.org/3.7/howto/descriptor.html#descriptor-protocol) 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 mypy<sup>1</sup> and pyright.
<sup>1</sup> Due to some issues in mypy not all features are supported correctly (like [dataclass alike
interface](https://github.com/python/mypy/issues/14293) or [descriptors](https://github.com/python/mypy/issues/13856)).
_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](https://slsa.dev/provenance/v0.2)
(dataslots package is compliant with [SLSA Level 3](https://slsa.dev/spec/v0.1/levels)).
If you want to verify dataslots before installing, you need to download
[SLSA verifier](https://github.com/slsa-framework/slsa-verifier) and run:
```bash
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__
* https://docs.python.org/3/reference/datamodel.html#slots
* https://github.com/ericvsmith/dataclasses/issues/28
[dataclasses_issue]: https://github.com/ericvsmith/dataclasses/issues/28
Raw data
{
"_id": null,
"home_page": "https://github.com/starhel/dataslots",
"name": "dataslots",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "",
"keywords": "dataslots slots dataclasses",
"author": "Adrian Stachlewski",
"author_email": "starhel.github@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/60/35/ddad1ab94f5d4471f064d203e5d363b536f270c4d82ef89d5044a4c8fd25/dataslots-1.2.0.tar.gz",
"platform": null,
"description": "# dataslots\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/dataslots.svg)](https://pypi.org/project/dataslots/)\n[![PyPI - Status](https://img.shields.io/pypi/status/dataslots.svg)](https://pypi.org/project/dataslots/)\n![license](https://img.shields.io/github/license/starhel/dataslots.svg)\n[![build status](https://github.com/starhel/dataslots/actions/workflows/tests.yml/badge.svg)](https://github.com/starhel/dataslots/actions)\n[![coverage](https://img.shields.io/badge/coverage-100%25-success)](https://github.com/starhel/dataslots/actions)\n\n[![SLSA](https://slsa.dev/images/gh-badge-level3.svg)](https://slsa.dev)\n\n## Decorator for adding __slots__\nIn python 3.7 dataclasses module was introduced for faster class creation ([PEP 557](https://www.python.org/dev/peps/pep-0557/)).\nUnfortunately, there's no support for `__slots__` (basic support was added in 3.10). If you want to create more memory \nefficient instances, you need to do it by yourself or use `@dataslots` decorator.\n\n## Usage\n### Simple example\n```python\n@dataslots\n@dataclass\nclass Point2D:\n x: int\n y: int\n```\n### Inheritance\nAs described in docs, in derived class `__dict__` is created, because base class does not have `__slots__`. \nSlots are created from all defined properties (returned by `dataclasses.fields()` function).\n```python\n@dataclass\nclass Base:\n a: int\n\n\n@dataslots\n@dataclass\nclass Derived(Base):\n c: int\n d: int\n```\n\n### Dynamic assignment of new variables\n```python\n@dataslots(add_dict=True)\n@dataclass\nclass Point2D:\n x: int\n y: int\n \npoint = Point2D(10, 20)\npoint.length = math.sqrt(point.x ** 2 + point.y ** 2)\n```\n\n### Weakref\n```python\n@dataslots(add_weakref=True)\n@dataclass\nclass Point2D:\n x: int\n y: int\n \npoint = Point2D(10, 20)\nr = weakref.ref(point)\n```\n\n### Read-only class variables\nWith `__slots__` it's possible to define read-only class variables. When using dataclasses you cannot provide type \nfor attribute or use `typing.ClassVar` to declare one. \n```python\n@dataslots\n@dataclass\nclass A:\n x = 5\n y: ClassVar[set] = set()\n```\n\n### Pickling frozen dataclass\nBecause of an [issue 36424](https://bugs.python.org/issue36424) you need custom `__setstate__` method. In dataslots \nthere is implemented default version, and it is used if decorated class has no `__getstate__` and `__setstate__` \nfunction declared.\n\n_Added in 1.0.2_\n\n### Data descriptors\n[Data descriptors](https://docs.python.org/3.7/howto/descriptor.html#descriptor-protocol) are supported by \ninheritance from `DataDescriptor` (base class with required interface) or `DataslotsDescriptor` (class with \nadditional features to simplify descriptor definition). \n\nCheck example directory for basic usage. \n\n_Added in 1.1.0_\n\n### Typing support (PEP 561)\nThe package is PEP 561 compliant, so you can easily use it with mypy<sup>1</sup> and pyright.\n\n<sup>1</sup> Due to some issues in mypy not all features are supported correctly (like [dataclass alike \ninterface](https://github.com/python/mypy/issues/14293) or [descriptors](https://github.com/python/mypy/issues/13856)). \n\n_Added in 1.2.0_\n\n### Backport\nIf you prefer using the newest `dataclasses.dataclass` interface you can use `dataslots.dataclass` wrapper \nto provide a consistent interface regardless of the python version.\n\nNotice: Wrapper always uses `dataslots` to make all additional features available and `slots=True` is obligatory. \n\n_Added in 1.2.0_\n\n## SLSA support\nAll packages from version 1.2.0 can be verified using [SLSA provenance](https://slsa.dev/provenance/v0.2) \n(dataslots package is compliant with [SLSA Level 3](https://slsa.dev/spec/v0.1/levels)).\n\nIf you want to verify dataslots before installing, you need to download \n[SLSA verifier](https://github.com/slsa-framework/slsa-verifier) and run:\n```bash\nslsa-verifier verify-artifact \\\n--provenance-path dataslots.intoto.jsonl \\\n--source-uri github.com/starhel/dataslots \\\n--source-tag v${VER} \\\n${PATH_TO_PACKAGE}\n```\n\n`VER` is version of package download from PYPI or GH release. Provenance is only available in GH release as PYPI\ndoes not accept jsonl files. \n\n## More about \\_\\_slots__\n* https://docs.python.org/3/reference/datamodel.html#slots\n* https://github.com/ericvsmith/dataclasses/issues/28\n\n[dataclasses_issue]: https://github.com/ericvsmith/dataclasses/issues/28\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Decorator to add __slots__ in dataclasses",
"version": "1.2.0",
"split_keywords": [
"dataslots",
"slots",
"dataclasses"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "afde4bc13dc9737e6a4e9a6fda8f8a20f2d295b6ea036210be4291ee2ddefded",
"md5": "d6b8fb070af5d6158513130e28281f38",
"sha256": "93322471730fbcc148b9458629567fa0fa9d4e9b664862860ab9910293c609b5"
},
"downloads": -1,
"filename": "dataslots-1.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "d6b8fb070af5d6158513130e28281f38",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 6100,
"upload_time": "2023-01-21T13:11:31",
"upload_time_iso_8601": "2023-01-21T13:11:31.913851Z",
"url": "https://files.pythonhosted.org/packages/af/de/4bc13dc9737e6a4e9a6fda8f8a20f2d295b6ea036210be4291ee2ddefded/dataslots-1.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "6035ddad1ab94f5d4471f064d203e5d363b536f270c4d82ef89d5044a4c8fd25",
"md5": "cdea559fafd567e289c220c23aecce08",
"sha256": "08a6597094c8bf821eea68bb9f3c5f188867efa62206166678bbb5f5167dbb43"
},
"downloads": -1,
"filename": "dataslots-1.2.0.tar.gz",
"has_sig": false,
"md5_digest": "cdea559fafd567e289c220c23aecce08",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 12737,
"upload_time": "2023-01-21T13:11:33",
"upload_time_iso_8601": "2023-01-21T13:11:33.152864Z",
"url": "https://files.pythonhosted.org/packages/60/35/ddad1ab94f5d4471f064d203e5d363b536f270c4d82ef89d5044a4c8fd25/dataslots-1.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-01-21 13:11:33",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "starhel",
"github_project": "dataslots",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "dataslots"
}