From 853a9cd337e160dee348aa21c11406f5fceb9e09 Mon Sep 17 00:00:00 2001 From: Alex Hadley Date: Wed, 30 Aug 2023 13:21:23 -0700 Subject: [PATCH] #114 Remove kw_onl default to True --- .github/workflows/ci.yml | 7 ++----- .github/workflows/publish.yml | 3 +-- CHANGELOG.md | 9 +++++++++ README.md | 1 + docs/parameter-data.md | 18 ++++++++++++------ paramdb/_param_data/_dataclasses.py | 6 +++--- 6 files changed, 28 insertions(+), 16 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d412b5e..7e7c8b4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,14 +11,14 @@ on: - develop env: + POETRY_VERSION: "1.6.1" MAIN_PYTHON_VERSION: "3.9" - POETRY_VERSION: "1.5.1" jobs: ci: strategy: matrix: - python_version: ["3.9", "3.10"] + python_version: ["3.9", "3.10", "3.11"] runs-on: ubuntu-latest steps: - name: Checkout @@ -33,9 +33,6 @@ jobs: python-version: ${{ matrix.python_version }} cache: poetry - - name: Check Python version - run: poetry run python --version - - name: Install dependencies run: poetry install --all-extras --without docs diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 9bec610..e5fa78c 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -5,8 +5,7 @@ on: types: [published] env: - PYTHON_VERSION: "3.10" - POETRY_VERSION: "1.5.1" + POETRY_VERSION: "1.6.1" jobs: build: diff --git a/CHANGELOG.md b/CHANGELOG.md index c29c6e2..dc85005 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,15 @@ project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Changed + +- Support for Python 3.9 + +### Removed + +- Parameter dataclass bases (`Param` and `Struct`) no longer set `kw_only` to True by + default (since this feature does not exist in Python 3.9). + ## [0.9.1] (Aug 9 2023) ### Changed diff --git a/README.md b/README.md index e0539e1..484f038 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # ParamDB [![PyPI Latest Release](https://img.shields.io/pypi/v/paramdb)](https://pypi.org/project/paramdb/) +![PyPI Python Versions](https://img.shields.io/pypi/pyversions/paramdb) [![License](https://img.shields.io/pypi/l/paramdb)](https://github.com/PainterQubits/paramdb/blob/main/LICENSE) [![CI](https://github.com/PainterQubits/paramdb/actions/workflows/ci.yml/badge.svg)](https://github.com/PainterQubits/paramdb/actions/workflows/ci.yml) [![Codecov](https://codecov.io/github/PainterQubits/paramdb/branch/main/graph/badge.svg?token=PQEJWLBTBK)](https://codecov.io/github/PainterQubits/paramdb) diff --git a/docs/parameter-data.md b/docs/parameter-data.md index c23ae89..9111975 100644 --- a/docs/parameter-data.md +++ b/docs/parameter-data.md @@ -54,18 +54,24 @@ 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. 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. +same default values as in [`@dataclass`]. An example of dataclass customization is shown +below. + +```{note} +The `kw_only` setting below only works in Python 3.10, but is useful for defining +non-default arguments after those with default values (like in the example), especially +when building up dataclasses through inheritance. +``` ```{jupyter-execute} from dataclasses import field -class CustomizedDataclassParam(Param, kw_only=False, repr=False): +class CustomizedDataclassParam(Param, kw_only=True): values: list[int] = field(default_factory=list) + count: int -customized_dataclass_param = CustomizedDataclassParam([1, 2, 3]) -customized_dataclass_param.values +customized_dataclass_param = CustomizedDataclassParam(count=123) +customized_dataclass_param ``` ```{warning} diff --git a/paramdb/_param_data/_dataclasses.py b/paramdb/_param_data/_dataclasses.py index 6b4787e..77636e4 100644 --- a/paramdb/_param_data/_dataclasses.py +++ b/paramdb/_param_data/_dataclasses.py @@ -10,7 +10,7 @@ from paramdb._param_data._param_data import ParamData -@dataclass_transform(kw_only_default=True) +@dataclass_transform() class _ParamDataclass(ParamData): """ Base class for parameter dataclasses. @@ -19,10 +19,10 @@ class _ParamDataclass(ParamData): constructor. """ - def __init_subclass__(cls, /, kw_only: bool = True, **kwargs: Any) -> None: + def __init_subclass__(cls, /, **kwargs: Any) -> None: # Convert subclasses into dataclasses super().__init_subclass__() # kwargs are passed to dataclass constructor - dataclass(kw_only=kw_only, **kwargs)(cls) + dataclass(**kwargs)(cls) def __getitem__(self, name: str) -> Any: # Enable getting attributes via indexing