starfield
starfield is a Python package that allows you to create attrs classes with a single field that can be initialized using variadic positional arguments (i.e. the star *). This makes it easier to initialise list-like structures with attrs without having to explicitly pass a list to the initializer.
Installation
To install starfield, run the following command in your terminal:
pip install starfield
Examples
The following example shows how to use starfield to create a class that behaves like a list with some extra fields:
from attrs import define, field
from starfield import starfield
@define(field_transformer=starfield)
class ShoppingList:
items: list = field(init="*")
store: str = field()
grocery_list = ShoppingList("Milk", "Bread", "Eggs", store="Supermarket")
Without starfield, you would have to explicitly pass a list to the initializer:
from attrs import define, field
@define
class ShoppingList:
items: list = field()
store: str = field()
grocery_list = ShoppingList(["Milk", "Bread", "Eggs"], store="Supermarket")
To illustrate the power of starfield, let's look at a more complex example involving nested fields. Suppose we want to create a data structure to represent a simple grammatical expression:
"I" ( "love" | "hate" ) ( "cats" | "dogs" )
We can define a class to represent this expression with attrs and starfield:
from attrs import define, field
from starfield import starfield
@define(field_transformer=starfield)
class And:
children: list = field(init="*")
@define(field_transformer=starfield)
class Or:
children: list = field(init="*")
expr = And("I", Or("love", "hate"), Or("cats", "dogs"))
Without starfield, you would have to explicitly pass a list to the initializer:
from attrs import define, field
@define
class And:
children: list = field()
@define
class Or:
children: list = field()
expr = And(["I", Or(["love", "hate"]), Or(["cats", "dogs"])])
Why Use starfield?
Nested fields can quickly become unwieldy when initializing objects with attrs. attrs's documentation explains why it's usually better to use a classmethod than to modify the initializer. But this can make initialization even more verbose.
starfield provides an alternative to using a classmethod by allowing you to define a single field that can be initialized using variadic positional arguments (i.e. the star *).
Features
-
starfieldwill make all non-star fields keyword-only. -
You can still set the star field using a keyword argument (e.g.
expr = And("I", items=[Or("love", "hate"), Or("cats", "dogs")])). -
To make the string representation of the class more readable,
starfieldadds a__rich_repr__method to the class. However, this only works if you're using rich to stringify your objects. To add a__repr__method as well, you can passrepr=Truetostarfield.
Limitations and Caveats
-
starfieldonly works with classes that useattrs. -
You can only have one star field per class.
-
The behaviour of
starfield's__repr__method may be inconsistent with theattrs-generated__repr__methods. -
starfieldis not yetmypy-compatible.- In particular,
mypywill complain about the following:[arg-type]: static type checkers won't recognize variadic positional arguments as valid arguments to the initializer.[call-overload]and[misc]onfield(init="*"):attrsexpectsinitto be abool.
- As a workaround, you can suppress these errors using:
# type: ignore[arg-type]upon initialization with variadic positional arguments, and# type: ignore[call-overload, misc]afterfield(init="*")in the class definition.
- In particular,
Related Projects
-
This feature has been requested and discussed here. The use of
init="*"is also proposed. -
pydantic's root types serve a similar purpose. Notable, however, a class with a root type cannot have any other fields.
Please let me know if I've missed any.
Dependencies
starfield requires Python 3.9 or later and attrs >= 22.2.0.
Authors
Metadata
Release files for starfield 0.3.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| starfield-0.3.3.tar.gz | 4.6 kB | Without provenance | Details for starfield-0.3.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| starfield-0.3.3-py3-none-any.whl | Python 3 | none | any | Details for starfield-0.3.3-py3-none-any.whl |
Total release size: 9.3 kB
Release files / starfield-0.3.3.tar.gz
| Download URL | starfield-0.3.3.tar.gz |
|---|---|
| Size | 4.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1bd4acc52af4e55414b15f0ef27b3aee8569add676c788e7608aa971ff9785a9
|
|
BLAKE2b-256 checksum How to use checksums |
6217a6abb1f232a613a576fe6b19909073cedbca0ed8ebcdf9c9b5725bdf4619
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.3.1 CPython/3.11.1 Darwin/22.1.0
|
Release files / starfield-0.3.3-py3-none-any.whl
| Download URL | starfield-0.3.3-py3-none-any.whl |
|---|---|
| Size | 4.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
96e1a84cb3119a62a177de8bafc0b26946065b2e316322083008ebd86a22763c
|
|
BLAKE2b-256 checksum How to use checksums |
96092d7b2373ab2a15b48058135a928607b39e7b3c3c86ae9b49363a672f363e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.3.1 CPython/3.11.1 Darwin/22.1.0
|
Security for starfield 0.3.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.