Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add CI and test changing a parameter #5

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

# GitHub recommends pinning actions to a commit SHA.
# To get a newer version, you will need to update the SHA.
# You can also reference a tag or branch, but the action may change without warning.

name: Publish

on:
release:
types: [published]

jobs:
publish-pypi:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install Poetry
uses: snok/install-poetry@v1
- name: Publish
env:
PYPI_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
run: |
poetry config pypi-token.pypi $PYPI_TOKEN
poetry publish --build
47 changes: 47 additions & 0 deletions .github/workflows/pytest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: pytest

on:
pull_request:
push:
branches:
- main
schedule:
- cron: '14 3 * * 1' # at 03:14 on Monday.

jobs:
pytest:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
python-version:
- "3.12"
- "3.11"
- "3.10"
os:
- ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install Poetry
uses: snok/install-poetry@v1
with:
virtualenvs-create: true
virtualenvs-in-project: true
- name: Install package
run: |
poetry install --no-interaction
- name: Run pytest
run: |
poetry run python --version
poetry run coverage run -m pytest
poetry run coverage lcov
- name: Coveralls
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
path-to-lcov: coverage.lcov
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[![zincware](https://img.shields.io/badge/Powered%20by-zincware-darkcyan)](https://github.com/zincware)
[![PyPI version](https://badge.fury.io/py/znfields.svg)](https://badge.fury.io/py/znfields)
[![Coverage Status](https://coveralls.io/repos/github/zincware/znfields/badge.svg?branch=main)](https://coveralls.io/github/zincware/znfields?branch=main)

# znfields

Provide a `getter` for `dataclasses.fields` to allow e.g. for lazy evaluation.
Expand Down
859 changes: 501 additions & 358 deletions poetry.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "znfields"
version = "0.1.0"
version = "0.1.1"
description = "Lazy evaluation of dataclass fields"
authors = ["Fabian Zills <[email protected]>"]
license = "Apache-2.0"
Expand All @@ -12,6 +12,7 @@ python = "^3.10"
[tool.poetry.group.dev.dependencies]
pre-commit = "^3.8.0"
pytest = "^8.3.2"
coverage = "^7.6.1"



Expand Down
9 changes: 8 additions & 1 deletion tests/test_znfields.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ def test_example1():
assert example.parameter == "parameter:1"


def test_example1_with_update():
example = Example1(parameter=1)
assert example.parameter == "parameter:1"
example.parameter = 2
assert example.parameter == "parameter:2"


@dataclasses.dataclass
class Example2(znfields.Base):
parameter: float = znfields.field()
Expand Down Expand Up @@ -46,7 +53,7 @@ def test_example3():
field = dataclasses.fields(example)[0]
assert field.metadata == {
"category": "test",
znfields._GETTER: example1_parameter_getter,
znfields.ZNFIELDS_GETTER_TYPE: example1_parameter_getter,
}


Expand Down
13 changes: 9 additions & 4 deletions znfields/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
import functools
from typing import Any, Callable, Optional

_GETTER = "znfields.getter"

class _ZNFIELDS_GETTER_TYPE:
pass


ZNFIELDS_GETTER_TYPE = _ZNFIELDS_GETTER_TYPE()


class Base:
Expand All @@ -17,7 +22,7 @@ def __getattribute__(self, name: str) -> Any:
)
except StopIteration:
return super().__getattribute__(name)
lazy = field.metadata.get(_GETTER)
lazy = field.metadata.get(ZNFIELDS_GETTER_TYPE)
if lazy:
return lazy(self, name)
return super().__getattribute__(name)
Expand All @@ -31,7 +36,7 @@ def field(*, getter: Optional[Callable[[Any, str], Any]] = None, **kwargs):
raise TypeError(
f"metadata must be a dict, not {type(kwargs['metadata'])}"
)
kwargs["metadata"][_GETTER] = getter
kwargs["metadata"][ZNFIELDS_GETTER_TYPE] = getter
else:
kwargs["metadata"] = {_GETTER: getter}
kwargs["metadata"] = {ZNFIELDS_GETTER_TYPE: getter}
return dataclasses.field(**kwargs)
Loading