-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add setter support * some more tests * add some random chatgpt tests * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add type hints * test against python 3.13 * test non-dataclass behaviour * test setter only * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
732b365
commit d0de0d3
Showing
5 changed files
with
257 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ jobs: | |
fail-fast: false | ||
matrix: | ||
python-version: | ||
- "3.13" | ||
- "3.12" | ||
- "3.11" | ||
- "3.10" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import dataclasses | ||
|
||
import pytest | ||
|
||
import znfields | ||
|
||
|
||
def getter(self, name): | ||
return f"{name}:{self.__dict__[name]}" | ||
|
||
|
||
def setter(self, name, value): | ||
if not isinstance(value, float): | ||
raise ValueError(f"Value {value} is not a float") | ||
self.__dict__[name] = value | ||
|
||
|
||
@dataclasses.dataclass | ||
class MyModel(znfields.Base): | ||
parameter: float = znfields.field(getter=getter, setter=setter) | ||
|
||
|
||
def test_readme(): | ||
model = MyModel(parameter=3.14) | ||
assert model.parameter == "parameter:3.14" | ||
with pytest.raises(ValueError): | ||
model.parameter = 42 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters