Skip to content

Commit 155d56a

Browse files
canikoCan H. Tartanoglu
authored and
Can H. Tartanoglu
committed
docs: Add info about custom serializer and types; contributing guide
chore(version): Poetry update; patch fix(ci): Outdated github action - Updated: - checkout action - python action - codecov action
1 parent 2dd105d commit 155d56a

File tree

6 files changed

+222
-105
lines changed

6 files changed

+222
-105
lines changed

Diff for: .github/workflows/test.yaml

+26-13
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,24 @@ defaults:
1313
jobs:
1414
unit_tests:
1515
strategy:
16+
fail-fast: false
1617
matrix:
1718
python-version: [ "3.9", "3.10", "3.11", "3.12" ]
1819
os: [ ubuntu-latest, windows-latest, macos-latest ]
1920

2021
runs-on: ${{ matrix.os }}
2122

2223
steps:
23-
- uses: actions/checkout@v3
24+
- uses: actions/checkout@v4
25+
26+
- name: Install poetry
27+
run: pipx install poetry
2428

2529
- name: Setup the Python Environment ${{ matrix.python-version }}
26-
uses: Qwerty-133/python-setup@v1
30+
uses: actions/setup-python@v5
2731
with:
2832
python-version: ${{ matrix.python-version }}
29-
skip-pre-commit: true
33+
cache: "poetry"
3034

3135
- name: Install dependencies
3236
run: |
@@ -37,7 +41,7 @@ jobs:
3741
poetry run pytest .
3842
3943
- name: Upload coverage reports to Codecov
40-
uses: codecov/codecov-action@v3
44+
uses: codecov/codecov-action@v4
4145
env:
4246
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
4347
mypy:
@@ -48,13 +52,16 @@ jobs:
4852
python-version: [ "3.9", "3.10", "3.11", "3.12" ]
4953

5054
steps:
51-
- uses: actions/checkout@v3
55+
- uses: actions/checkout@v4
56+
57+
- name: Install poetry
58+
run: pipx install poetry
5259

5360
- name: Setup the Python Environment ${{ matrix.python-version }}
54-
uses: Qwerty-133/python-setup@v1
61+
uses: actions/setup-python@v5
5562
with:
5663
python-version: ${{ matrix.python-version }}
57-
skip-pre-commit: true
64+
cache: "poetry"
5865

5966
- name: Install dependencies
6067
run: |
@@ -75,13 +82,16 @@ jobs:
7582
python-version: [ "3.9", "3.10", "3.11", "3.12" ]
7683

7784
steps:
78-
- uses: actions/checkout@v3
85+
- uses: actions/checkout@v4
86+
87+
- name: Install poetry
88+
run: pipx install poetry
7989

8090
- name: Setup the Python Environment ${{ matrix.python-version }}
81-
uses: Qwerty-133/python-setup@v1
91+
uses: actions/setup-python@v5
8292
with:
8393
python-version: ${{ matrix.python-version }}
84-
skip-pre-commit: true
94+
cache: "poetry"
8595

8696
- name: Install dependencies
8797
run: |
@@ -99,13 +109,16 @@ jobs:
99109
python-version: [ "3.12" ]
100110

101111
steps:
102-
- uses: actions/checkout@v3
112+
- uses: actions/checkout@v4
113+
114+
- name: Install poetry
115+
run: pipx install poetry
103116

104117
- name: Setup the Python Environment ${{ matrix.python-version }}
105-
uses: Qwerty-133/python-setup@v1
118+
uses: actions/setup-python@v5
106119
with:
107120
python-version: ${{ matrix.python-version }}
108-
skip-pre-commit: true
121+
cache: "poetry"
109122

110123
- name: Install dependencies
111124
run: |

Diff for: .github/workflows/upload.yaml

+6-3
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@ jobs:
2222
python-version: [ "3.12" ]
2323

2424
steps:
25-
- uses: actions/checkout@v3
25+
- uses: actions/checkout@v4
26+
27+
- name: Install poetry
28+
run: pipx install poetry
2629

2730
- name: Setup the Python Environment ${{ matrix.python-version }}
28-
uses: Qwerty-133/python-setup@v1
31+
uses: actions/setup-python@v5
2932
with:
3033
python-version: ${{ matrix.python-version }}
31-
skip-pre-commit: true
34+
cache: "poetry"
3235

3336
- name: Install dependencies
3437
run: poetry install

Diff for: CONTRIBUTING.md

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Contributors Guide
2+
3+
Welcome to our project! We value your contributions and want to make it easy for you to get involved. This guide outlines our project's workflow, versioning strategy, and commit message conventions.
4+
5+
## Workflow: GitHub Flow
6+
7+
To get started with contributing, please follow the GitHub Flow:
8+
9+
1. **Fork** this repository to your own GitHub account.
10+
2. **Clone** the forked repository to your local machine:
11+
```bash
12+
git clone <your-fork-url>
13+
git checkout -b your-new-branch-name
14+
```
15+
3. **Create a branch** for your modifications:
16+
```bash
17+
git checkout -b feature-branch-name
18+
```
19+
4. **Make your changes** and commit them using the [Conventional Commits](#commit-messages) format.
20+
5. **Push** your branch to your fork:
21+
```bash
22+
git push origin feature-branch-name
23+
```
24+
6. **Create a pull request** against the original repository’s main branch.
25+
7. **Review** the changes with maintainers and **address any feedback**.
26+
27+
The project maintainers will merge the changes once they are approved. If you have any questions or need help, please feel free to reach out to us.
28+
29+
## Semantic Versioning
30+
31+
Our project adheres to Semantic Versioning (SemVer) principles, which helps us manage releases and dependencies systematically. The version number format is:
32+
33+
MAJOR.MINOR.PATCH
34+
35+
- **MAJOR**: Incompatible API changes,
36+
- **MINOR**: Additions that are backward compatible,
37+
- **PATCH**: Backward-compatible bug fixes.
38+
39+
## Commit Messages
40+
41+
We use the Conventional Commits format for our commit messages to facilitate consistency and changelog generation. Please structure your commits as follows:
42+
43+
<type>(<scope>): <description>
44+
45+
[optional body]
46+
47+
[optional footer]
48+
49+
markdown
50+
51+
52+
### Commit Types <type>
53+
54+
- `feat`: Introduces a new feature.
55+
- `fix`: Patches a bug.
56+
- `docs`: Documentation only changes.
57+
- `style`: Code style update (formatting, semi-colons, etc.).
58+
- `refactor`: Neither fixes a bug nor introduces a feature.
59+
- `perf`: Performance improvements.
60+
- `test`: Adding missing tests or correcting existing ones.
61+
- `chore`: Other changes that don't modify src or test files.
62+
63+
**Example Commit Message**:
64+
65+
```
66+
feat(authentication): add biometric login support
67+
68+
- Support fingerprint and face recognition login.
69+
- Ensure compatibility with mobile devices.
70+
```
71+
72+
Thank you for contributing to our project! We appreciate your effort to follow these guidelines.

Diff for: README.md

+36-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Package that integrates NumPy Arrays into Pydantic!
1414
- `pydantic_numpy.typing` provides many typings such as `NpNDArrayFp64`, `Np3DArrayFp64` (float64 that must be 3D)! Works with both `pydantic.BaseModel` and `pydantic.dataclass`
1515
- `NumpyModel` (derived from `pydantic.BaseModel`) make it possible to dump and load `np.ndarray` within model fields alongside other fields that are not instances of `np.ndarray`!
1616

17-
See the [`test.helper.groups`](https://github.com/caniko/pydantic-numpy/blob/trunk/tests/helper/groups.py) to see types that are defined explicitly. Define your own NumPy types with `pydantic_numpy.np_array_pydantic_annotated_typing`.
17+
See the [`test.helper.testing_groups`](https://github.com/caniko/pydantic-numpy/blob/trunk/tests/helper/testing_groups.py) to see types that are defined explicitly.
1818

1919
### Examples
2020

@@ -69,16 +69,45 @@ cfg.dump("path_to_dump_dir", "object_id")
6969
equals_cfg = model_agnostic_load("path_to_dump_dir", "object_id", models=[MyNumpyModel, MyDemoModel])
7070
```
7171

72+
### Custom type
73+
There are two ways to define. Function derived types with `pydantic_numpy.helper.annotation.np_array_pydantic_annotated_typing`.
74+
75+
Function derived types don't work with static type checkers like Pyright and MyPy. In case you need the support,
76+
just create the types yourself:
77+
78+
```python
79+
NpStrict1DArrayInt64 = Annotated[
80+
np.ndarray[tuple[int], np.dtype[np.int64]],
81+
NpArrayPydanticAnnotation.factory(data_type=np.int64, dimensions=1, strict_data_typing=True),
82+
]
83+
```
84+
85+
#### Custom serialization
86+
87+
If the default serialization of NumpyDataDict, as outlined in [typing.py](https://github.com/caniko/pydantic-numpy/blob/trunk/pydantic_numpy/helper/typing.py), doesn't meet your requirements, you have the option to define a custom type with its own serializer. This can be achieved using the NpArrayPydanticAnnotation.factory method, which accepts a custom serialization function through its serialize_numpy_array_to_json parameter. This parameter expects a function of the form `Callable[[npt.ArrayLike], Iterable]`, allowing you to tailor the serialization process to your specific needs.
88+
89+
Example below illustrates definition of 1d-array of `float32` type that serializes to flat Python list (without nested dict as in default `NumpyDataDict` case):
90+
91+
```python
92+
def _serialize_numpy_array_to_float_list(array_like: npt.ArrayLike) -> Iterable:
93+
return np.array(array_like).astype(float).tolist()
94+
95+
96+
Np1DArrayFp32 = Annotated[
97+
np.ndarray[tuple[int], np.dtype[np.float32]],
98+
NpArrayPydanticAnnotation.factory(
99+
data_type=np.float32,
100+
dimensions=1,
101+
strict_data_typing=False,
102+
serialize_numpy_array_to_json=_serialize_numpy_array_to_float_list,
103+
),
104+
]
105+
```
106+
72107
### Install
73108
```shell
74109
pip install pydantic-numpy
75110
```
76111

77-
## Considerations
78-
The package is designed to work with Pydantic V2 and not V1. You can install from [cheind's](https://github.com/cheind/pydantic-numpy) repository if you want Python `<=3.8` and Pydantic V1 support.
79-
80-
### Licensing notice
81-
As of version `3.0.0` the license has moved over to BSD-4. The versions prior are under the MIT license.
82-
83112
### History
84113
The original idea originates from [this discussion](https://gist.github.com/danielhfrank/00e6b8556eed73fb4053450e602d2434), and forked from [cheind's](https://github.com/cheind/pydantic-numpy) repository.

0 commit comments

Comments
 (0)