-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Fixed incorrect methodology of getting Config from model instanc…
…e, self.Config -> self.__config__ Test: Improved coverage for NumpyModel Chore: Version bump, PATCH
- Loading branch information
Showing
5 changed files
with
57 additions
and
21 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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
# Project | ||
|
||
delete_me_test_dump*/ | ||
|
||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
|
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
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "pydantic_numpy" | ||
version = "2.0.1" | ||
version = "2.0.2" | ||
description = "Seamlessly integrate numpy arrays into pydantic models" | ||
authors = ["Can H. Tartanoglu <[email protected]>", "Christoph Heindl"] | ||
license = "MIT" | ||
|
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 |
---|---|---|
@@ -1,50 +1,84 @@ | ||
import shutil | ||
from pathlib import Path | ||
from typing import Union | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
from pydantic_numpy import NDArray | ||
from pydantic_numpy import NDArray, NDArrayBool | ||
from pydantic_numpy.model import NumpyModel | ||
|
||
TEST_DUMP_PATH: Path = Path("./test_dump").resolve() | ||
TEST_DUMP_PATH: Path = Path("../delete_me_test_dump").resolve() | ||
|
||
|
||
class TestNumpyModel(NumpyModel): | ||
class NumpyModelForTest(NumpyModel): | ||
array: NDArray | ||
non_array: int | ||
|
||
|
||
class TestWithArbitraryForTest(NumpyModelForTest): | ||
my_arbitrary_slice: slice | ||
|
||
class Config: | ||
arbitrary_types_allowed = True | ||
|
||
|
||
numpy_bool_array: NDArrayBool = np.array([True, True, True, True, True], dtype=bool) | ||
|
||
|
||
def _numpy_model(): | ||
return NumpyModelForTest(array=numpy_bool_array, non_array=5) | ||
|
||
|
||
@pytest.fixture | ||
def test_numpy_model(): | ||
return TestNumpyModel(array=np.array([True, False, True, True, True], dtype=bool), non_array=5) | ||
def numpy_model(): | ||
return _numpy_model() | ||
|
||
|
||
@pytest.fixture(params=[ | ||
_numpy_model(), | ||
TestWithArbitraryForTest(array=numpy_bool_array, non_array=5, my_arbitrary_slice=slice(0, 10)) | ||
]) | ||
def numpy_model_with_arbitrary(request): | ||
return request.param | ||
|
||
def test_io_yaml(test_numpy_model): | ||
|
||
def test_io_yaml(numpy_model): | ||
try: | ||
test_numpy_model.dump(TEST_DUMP_PATH) | ||
test_numpy_model.load(TEST_DUMP_PATH) | ||
numpy_model.dump(TEST_DUMP_PATH) | ||
_test_loaded_numpy_model(numpy_model.load(TEST_DUMP_PATH)) | ||
finally: | ||
_delete_leftovers() | ||
|
||
|
||
def test_io_compressed_pickle(test_numpy_model): | ||
def test_io_compressed_pickle(numpy_model_with_arbitrary): | ||
try: | ||
test_numpy_model.dump(TEST_DUMP_PATH, pickle=True) | ||
test_numpy_model.load(TEST_DUMP_PATH) | ||
numpy_model_with_arbitrary.dump(TEST_DUMP_PATH, pickle=True) | ||
_test_loaded_numpy_model(numpy_model_with_arbitrary.load(TEST_DUMP_PATH)) | ||
|
||
finally: | ||
_delete_leftovers() | ||
|
||
|
||
def test_io_pickle(test_numpy_model): | ||
def test_io_pickle(numpy_model_with_arbitrary): | ||
try: | ||
test_numpy_model.dump(TEST_DUMP_PATH, pickle=True, compress=False) | ||
test_numpy_model.load(TEST_DUMP_PATH) | ||
numpy_model_with_arbitrary.dump(TEST_DUMP_PATH, pickle=True, compress=False) | ||
_test_loaded_numpy_model(numpy_model_with_arbitrary.load(TEST_DUMP_PATH)) | ||
finally: | ||
_delete_leftovers() | ||
|
||
|
||
def _test_loaded_numpy_model(model: Union[NumpyModelForTest, TestWithArbitraryForTest]): | ||
assert np.all(model.array) and len(model.array) == 5 | ||
if isinstance(model, TestWithArbitraryForTest): | ||
assert isinstance(model.my_arbitrary_slice, slice) | ||
|
||
|
||
def _delete_leftovers(): | ||
dump_path = TestNumpyModel.model_directory_path(TEST_DUMP_PATH) | ||
dump_path = NumpyModelForTest.model_directory_path(TEST_DUMP_PATH) | ||
if dump_path.exists(): | ||
shutil.rmtree(dump_path) | ||
|
||
dump_path = TestWithArbitraryForTest.model_directory_path(TEST_DUMP_PATH) | ||
if dump_path.exists(): | ||
shutil.rmtree(dump_path) |