Skip to content

Commit

Permalink
#40 Make kw_only True by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Hadley committed Mar 14, 2023
1 parent 4ceaa9e commit 1ba5236
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

- Ability to specify start and end indices in `ParamDB.commit_history()`
- Support for scalar [`astropy.units.Quantity`] objects
- Parameter dataclass bases (`Param` and `Struct`) automatically convert subclasses into
dataclasses `kw_only` as True by default

### Changed

- `ParamDict` can be initialized from keyword arguments in addition to dictionaries
- Parameter dataclass bases (`Param` and `Struct`) automatically convert subclasses into
dataclasses, so the `@dataclass` decorator should no longer be used

## [0.2.0] (Mar 8 2023)

Expand Down
11 changes: 7 additions & 4 deletions docs/parameter-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,18 @@ param.value

The dataclass aspects of the subclass can be customized by passing keyword arguments when
defining the custom class (the same arguments that would be passed to the [`@dataclass`]
decorator), and by using the dataclass [`field`] function. For example,
decorator), and by using the dataclass [`field`] function. The class arguments have the
same default values as in [`@dataclass`], except `kw_only` is True by default for
ParamDB dataclasses to facilitate dataclass inheritance with default values. An example of
dataclass customization is shown below.

```{jupyter-execute}
from dataclasses import field
class CustomizedDataclassParam(Param, kw_only=True, repr=False):
class CustomizedDataclassParam(Param, kw_only=False, repr=False):
values: list[int] = field(default_factory=list)
customized_dataclass_param = CustomizedDataclassParam()
customized_dataclass_param = CustomizedDataclassParam([1, 2, 3])
customized_dataclass_param.values
```

Expand Down Expand Up @@ -176,7 +179,7 @@ example,
```{jupyter-execute}
from paramdb import ParamList
param_list = ParamList([CustomParam(1), CustomParam(2), CustomParam(3)])
param_list = ParamList([CustomParam(value=1), CustomParam(value=2), CustomParam(value=3)])
param_list[1].parent == param_list
```

Expand Down
6 changes: 3 additions & 3 deletions paramdb/_param_data/_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
from paramdb._param_data._param_data import ParamData


@dataclass_transform()
@dataclass_transform(kw_only_default=True)
class _ParamDataclass(ParamData):
"""Base class for parameter dataclasses."""

def __init_subclass__(cls, /, **kwargs: Any) -> None:
def __init_subclass__(cls, /, kw_only=True, **kwargs: Any) -> None:
# Convert subclasses into dataclasses
super().__init_subclass__()
dataclass(cls, **kwargs)
dataclass(cls, kw_only=kw_only, **kwargs)

def __getitem__(self, name: str) -> Any:
# Enable getting attributes via indexing
Expand Down

0 comments on commit 1ba5236

Please sign in to comment.