{"id":13738344,"url":"https://github.com/starhel/dataslots","last_synced_at":"2025-05-08T16:33:24.346Z","repository":{"id":30811442,"uuid":"125579768","full_name":"starhel/dataslots","owner":"starhel","description":"__slots__ for dataclasses","archived":false,"fork":false,"pushed_at":"2024-02-12T10:33:31.000Z","size":66,"stargazers_count":22,"open_issues_count":8,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-02T04:07:15.843Z","etag":null,"topics":["dataclasses","python3","slots"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/starhel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2018-03-16T23:22:11.000Z","updated_at":"2023-04-27T19:42:33.000Z","dependencies_parsed_at":"2023-02-12T10:00:48.820Z","dependency_job_id":"dba347fd-b1fd-43c6-a7ef-2e54f9c88b76","html_url":"https://github.com/starhel/dataslots","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/starhel%2Fdataslots","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/starhel%2Fdataslots/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/starhel%2Fdataslots/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/starhel%2Fdataslots/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/starhel","download_url":"https://codeload.github.com/starhel/dataslots/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224746590,"owners_count":17363076,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dataclasses","python3","slots"],"created_at":"2024-08-03T03:02:19.426Z","updated_at":"2024-11-15T07:30:49.461Z","avatar_url":"https://github.com/starhel.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# 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[![OpenSSF Scorecard](https://api.securityscorecards.dev/projects/github.com/starhel/dataslots/badge)](https://api.securityscorecards.dev/projects/github.com/starhel/dataslots)\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\u003e=1.1.1`\u003csup\u003e1\u003c/sup\u003e and `pyright`.\n\n\u003csup\u003e1\u003c/sup\u003e Due to some issues in mypy not all features are supported correctly (known bugs: [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","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstarhel%2Fdataslots","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstarhel%2Fdataslots","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstarhel%2Fdataslots/lists"}