Skip to content

Commit f177c09

Browse files
committed
feat: add make_immutable to be on par with make_dataclass
1 parent 8f5d3bc commit f177c09

File tree

6 files changed

+105
-51
lines changed

6 files changed

+105
-51
lines changed

.github/workflows/integration_delivery.yml

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ on:
66
workflow_dispatch:
77

88
env:
9-
PYTHON_VERSION: "3.11"
9+
PYTHON_VERSION: '3.11'
1010

1111
jobs:
1212
dependencies:
@@ -16,7 +16,7 @@ jobs:
1616
- uses: actions/checkout@v4
1717
name: Checkout
1818

19-
- name: Save Cached Poetry
19+
- name: Load Cached Poetry
2020
id: cached-poetry
2121
uses: actions/cache@v4
2222
with:
@@ -99,8 +99,8 @@ jobs:
9999
- dependencies
100100
runs-on: ubuntu-latest
101101
outputs:
102-
version: ${{ steps.extract_version.outputs.version }}
103-
name: ${{ steps.extract_version.outputs.name }}
102+
version: ${{ steps.extract_version.outputs.VERSION }}
103+
name: ${{ steps.extract_version.outputs.NAME }}
104104
steps:
105105
- uses: actions/checkout@v4
106106
name: Checkout
@@ -120,14 +120,41 @@ jobs:
120120
~/.local
121121
key: poetry-${{ hashFiles('poetry.lock') }}
122122

123-
- name: Build
124-
run: poetry build
125-
126123
- name: Extract Version
127124
id: extract_version
128125
run: |
129-
echo "version=$(poetry version --short)" >> "$GITHUB_OUTPUT"
130-
echo "name=$(poetry version | cut -d' ' -f1)" >> "$GITHUB_OUTPUT"
126+
echo "VERSION=$(poetry version --short)" >> "$GITHUB_OUTPUT"
127+
echo "NAME=$(poetry version | cut -d' ' -f1)" >> "$GITHUB_OUTPUT"
128+
echo "VERSION=$(poetry version --short)"
129+
echo "NAME=$(poetry version | cut -d' ' -f1)"
130+
131+
- name: Extract Version from CHANGELOG.md
132+
id: extract_changelog_version
133+
run: |
134+
VERSION_CHANGELOG=$(sed -n '3 s/## Version //p' CHANGELOG.md)
135+
echo "VERSION_CHANGELOG=$VERSION_CHANGELOG"
136+
if [ "${{ steps.extract_version.outputs.VERSION }}" != "$VERSION_CHANGELOG" ]; then
137+
echo "Error: Version extracted from CHANGELOG.md does not match the version in pyproject.toml"
138+
exit 1
139+
else
140+
echo "Versions are consistent."
141+
fi
142+
143+
- name: Extract Version from Tag
144+
if: startsWith(github.ref, 'refs/tags/v')
145+
id: extract_tag_version
146+
run: |
147+
VERSION_TAG=$(sed 's/^v//' <<< ${{ github.ref_name }})
148+
echo "VERSION_TAG=$VERSION_TAG"
149+
if [ "${{ steps.extract_version.outputs.VERSION }}" != "$VERSION_TAG" ]; then
150+
echo "Error: Version extracted from tag does not match the version in pyproject.toml"
151+
exit 1
152+
else
153+
echo "Versions are consistent."
154+
fi
155+
156+
- name: Build
157+
run: poetry build
131158

132159
- name: Upload wheel
133160
uses: actions/upload-artifact@v4
@@ -175,19 +202,19 @@ jobs:
175202
verbose: true
176203

177204
release:
205+
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
178206
name: Release
179207
needs:
180208
- type-check
181209
- lint
182210
- build
183211
- pypi-publish
212+
runs-on: ubuntu-latest
184213
environment:
185214
name: release
186215
url: https://pypi.org/p/${{ needs.build.outputs.name }}
187-
runs-on: ubuntu-latest
188216
permissions:
189217
contents: write
190-
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
191218
steps:
192219
- name: Procure Wheel
193220
uses: actions/download-artifact@v4
@@ -201,13 +228,28 @@ jobs:
201228
name: binary
202229
path: artifacts
203230

231+
- uses: actions/checkout@v4
232+
name: Checkout
233+
234+
- name: Extract Changelog
235+
id: changelog
236+
run: |
237+
perl -0777 -ne 'while (/## Version ${{ needs.build.outputs.version }}\n(\s*\n)*(.*?)(\s*\n)*## Version \d+\.\d+\.\d+\n/sg) {print "$2\n"}' CHANGELOG.md > CURRENT_CHANGELOG.md
238+
{
239+
echo "CONTENT<<EOF"
240+
cat CURRENT_CHANGELOG.md
241+
echo "EOF"
242+
} >> "$GITHUB_OUTPUT"
243+
204244
- name: Release
205-
uses: softprops/action-gh-release@v1
245+
uses: softprops/action-gh-release@v2
206246
with:
207247
files: artifacts/*
208248
tag_name: v${{ needs.build.outputs.version }}
209249
body: |
210-
Release of version ${{ needs.build.outputs.version }}
211250
PyPI package: https://pypi.org/project/${{ needs.build.outputs.name }}/${{ needs.build.outputs.version }}
251+
252+
# Changes:
253+
${{ steps.changelog.outputs.CONTENT }}
212254
prerelease: false
213255
draft: false

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Version 1.0.6
4+
5+
- feat: add `make_immutable` to be on par with `make_dataclass`
6+
37
## Version 1.0.5
48

59
- feat: add `is_immutable` to check whether an object is an instance of `Immutable`

immutable/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Immutable data structures for Python."""
22

3-
from .main import Immutable, immutable, is_immutable
3+
from .main import Immutable, immutable, is_immutable, make_immutable
44

5-
__all__ = ['Immutable', 'immutable', 'is_immutable']
5+
__all__ = ['Immutable', 'immutable', 'is_immutable', 'make_immutable']

immutable/main.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
from __future__ import annotations
33

44
import sys
5-
from dataclasses import dataclass
6-
from typing import Any, Mapping, TypeGuard, TypeVar
5+
from dataclasses import dataclass, make_dataclass
6+
from typing import Any, Iterable, Mapping, TypeGuard, TypeVar
77

88
from typing_extensions import dataclass_transform
99

@@ -34,3 +34,11 @@ def is_immutable(obj: object) -> TypeGuard[Immutable]:
3434
and hasattr(obj, '__dataclass_params__')
3535
and getattr(getattr(obj, '__dataclass_params__', None), 'frozen', False)
3636
)
37+
38+
39+
@dataclass_transform(kw_only_default=True, frozen_default=True)
40+
def make_immutable(
41+
cls_name: str,
42+
fields: Iterable[str | tuple[str, Any] | tuple[str, Any, Any]],
43+
) -> type:
44+
return make_dataclass(cls_name, fields, frozen=True, kw_only=True)

poetry.lock

Lines changed: 31 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "python-immutable"
3-
version = "1.0.5"
3+
version = "1.0.6"
44
description = "Immutable implementation for Python using dataclasses"
55
authors = ["Sassan Haradji <[email protected]>"]
66
license = "Apache-2.0"
@@ -17,8 +17,8 @@ optional = true
1717

1818
[tool.poetry.group.dev.dependencies]
1919
poethepoet = "^0.24.3"
20-
pyright = "^1.1.352"
21-
ruff = "^0.3.0"
20+
pyright = "^1.1.361"
21+
ruff = "^0.4.3"
2222

2323
[build-system]
2424
requires = ["poetry-core"]

0 commit comments

Comments
 (0)