Skip to content

Commit

Permalink
linting for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sneakers-the-rat committed Oct 4, 2024
1 parent f291afb commit 8f1eb6e
Show file tree
Hide file tree
Showing 20 changed files with 92 additions and 98 deletions.
7 changes: 5 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ markers = [

[tool.ruff]
target-version = "py39"
include = ["src/numpydantic/**/*.py", "pyproject.toml"]
exclude = ["tests"]
include = ["src/numpydantic/**/*.py", "tests/**/*.py", "pyproject.toml"]

[tool.ruff.lint]
select = [
Expand Down Expand Up @@ -177,6 +176,10 @@ ignore = [

fixable = ["ALL"]

[tool.ruff.lint.per-file-ignores]
"src/numpydantic/testing/*" = ["D", "F722"]
"tests/*" = ["D", "F403", "F722", "ANN", ]

[tool.mypy]
plugins = [
"pydantic.mypy"
Expand Down
2 changes: 0 additions & 2 deletions src/numpydantic/testing/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
YES_PIPE = False



class BasicModel(BaseModel):
x: int

Expand Down Expand Up @@ -129,4 +128,3 @@ class SubClass(BasicModel):
"union-pipe-str",
]
)

1 change: 1 addition & 0 deletions src/numpydantic/testing/helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Any, Tuple, Type, Union

import numpy as np
from pydantic import BaseModel, ConfigDict, computed_field

from numpydantic import NDArray, Shape
Expand Down
12 changes: 6 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import sys

from numpydantic.testing.cases import YES_PIPE, RGB_UNION, UNION_PIPE, DTYPE_CASES, DTYPE_IDS
import pytest

from numpydantic.testing.cases import (
DTYPE_CASES,
DTYPE_IDS,
RGB_UNION,
)
from numpydantic.testing.helpers import ValidationCase
from tests.fixtures import *

Expand All @@ -14,9 +17,6 @@ def pytest_addoption(parser):
)





@pytest.fixture(
scope="module",
params=[
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .paths import *
from .generation import *
from .models import *
from .models import *
from .paths import *
9 changes: 5 additions & 4 deletions tests/fixtures/generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ def _hdf5_array(
compound: bool = False,
) -> H5ArrayPath:
array_path = "/" + "_".join([str(s) for s in shape]) + "__" + dtype.__name__

generator = np.random.default_rng()

if not compound:
if dtype is str:
data = np.random.random(shape).astype(bytes)
data = generator.random(shape).astype(bytes)
elif dtype is datetime:
data = np.empty(shape, dtype="S32")
data.fill(datetime.now(timezone.utc).isoformat().encode("utf-8"))
else:
data = np.random.random(shape).astype(dtype)
data = generator.random(shape).astype(dtype)

h5path = H5ArrayPath(hdf5_file, array_path)
else:
Expand Down Expand Up @@ -64,7 +65,7 @@ def zarr_nested_array(tmp_output_dir_func) -> ZarrArrayPath:
file = tmp_output_dir_func / "nested.zarr"
path = "a/b/c"
root = zarr.open(str(file), mode="w")
array = root.zeros(path, shape=(100, 100), chunks=(10, 10))
_ = root.zeros(path, shape=(100, 100), chunks=(10, 10))
return ZarrArrayPath(file=file, path=path)


Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Callable, Tuple, Union, Type, Optional, Any
from typing import Any, Callable, Optional, Tuple, Type, Union

import numpy as np
import pytest
Expand Down
3 changes: 2 additions & 1 deletion tests/fixtures/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def tmp_output_dir(request: pytest.FixtureRequest) -> Path:
except PermissionError as e:
# sporadic error on windows machines...
warn(
f"Temporary directory could not be removed due to a permissions error: \n{str(e)}"
"Temporary directory could not be removed due to a permissions error: "
f"\n{str(e)}"
)


Expand Down
8 changes: 4 additions & 4 deletions tests/test_interface/conftest.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import pytest

from typing import Callable, Tuple, Type
import numpy as np

import dask.array as da
import numpy as np
import pytest
import zarr
from pydantic import BaseModel

from numpydantic import interface, NDArray
from numpydantic import NDArray, interface


@pytest.fixture(
Expand Down
5 changes: 2 additions & 3 deletions tests/test_interface/test_dask.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import pytest
import json

import dask.array as da
import pytest
from pydantic import BaseModel, ValidationError

from numpydantic.interface import DaskInterface
from numpydantic.exceptions import DtypeError, ShapeError

from numpydantic.interface import DaskInterface
from numpydantic.testing.helpers import ValidationCase

pytestmark = pytest.mark.dask
Expand Down
12 changes: 4 additions & 8 deletions tests/test_interface/test_hdf5.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import json
from datetime import datetime, timezone
from datetime import datetime
from typing import Any

import h5py
import numpy as np
import pytest
from pydantic import BaseModel, ValidationError

import numpy as np
from numpydantic import NDArray, Shape
from numpydantic.exceptions import DtypeError, ShapeError
from numpydantic.interface import H5Interface
from numpydantic.interface.hdf5 import H5ArrayPath, H5Proxy
from numpydantic.exceptions import DtypeError, ShapeError

from numpydantic.testing.helpers import ValidationCase

pytestmark = pytest.mark.hdf5
Expand Down Expand Up @@ -221,10 +220,7 @@ def test_empty_dataset(dtype, tmp_path):
Empty datasets shouldn't choke us during validation
"""
array_path = tmp_path / "test.h5"
if dtype in (str, datetime):
np_dtype = "S32"
else:
np_dtype = dtype
np_dtype = "S32" if dtype in (str, datetime) else dtype

with h5py.File(array_path, "w") as h5f:
_ = h5f.create_dataset(name="/data", dtype=np_dtype)
Expand Down
16 changes: 7 additions & 9 deletions tests/test_interface/test_interface_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@
import gc
from typing import Literal

import pytest
import numpy as np
import pytest
from pydantic import ValidationError

from numpydantic.interface import (
Interface,
JsonDict,
InterfaceMark,
NumpyInterface,
JsonDict,
MarkedJson,
NumpyInterface,
)
from pydantic import ValidationError

from numpydantic.interface.interface import V


Expand Down Expand Up @@ -46,9 +45,7 @@ class Interface1(Interface):
@classmethod
def check(cls, array):
cls.checked = True
if isinstance(array, list):
return True
return False
return isinstance(array, list)

@classmethod
def enabled(cls) -> bool:
Expand Down Expand Up @@ -94,7 +91,8 @@ class Interfaces:

def test_interface_match_error(interfaces):
"""
Test that `match` and `match_output` raises errors when no or multiple matches are found
Test that `match` and `match_output` raises errors when no or multiple matches
are found
"""
with pytest.raises(ValueError) as e:
Interface.match([1, 2, 3])
Expand Down
10 changes: 5 additions & 5 deletions tests/test_interface/test_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
Tests that should be applied to all interfaces
"""

import pytest
from typing import Callable
from importlib.metadata import version
import json
from importlib.metadata import version
from typing import Callable

import numpy as np
import dask.array as da
from zarr.core import Array as ZarrArray
import numpy as np
import pytest
from pydantic import BaseModel
from zarr.core import Array as ZarrArray

from numpydantic.interface import Interface, InterfaceMark, MarkedJson

Expand Down
4 changes: 2 additions & 2 deletions tests/test_interface/test_numpy.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import numpy as np
import pytest
from pydantic import ValidationError, BaseModel
from numpydantic.exceptions import DtypeError, ShapeError
from pydantic import BaseModel, ValidationError

from numpydantic.exceptions import DtypeError, ShapeError
from numpydantic.testing.helpers import ValidationCase

pytestmark = pytest.mark.numpy
Expand Down
8 changes: 3 additions & 5 deletions tests/test_interface/test_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
Needs to be refactored to DRY, but works for now
"""

import numpy as np
import pytest

from pathlib import Path
import cv2

import cv2
import pytest
from pydantic import BaseModel, ValidationError

from numpydantic import NDArray, Shape
Expand Down Expand Up @@ -65,7 +63,7 @@ class MyModel(BaseModel):

# should correctly validate :)
with pytest.raises(ValidationError):
instance = MyModel(array=vid)
_ = MyModel(array=vid)


@pytest.mark.proxy
Expand Down
13 changes: 5 additions & 8 deletions tests/test_interface/test_zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

import pytest
import zarr

from pydantic import BaseModel, ValidationError
from numcodecs import Pickle

from numpydantic.exceptions import DtypeError, ShapeError
from numpydantic.interface import ZarrInterface
from numpydantic.interface.zarr import ZarrArrayPath
from numpydantic.exceptions import DtypeError, ShapeError

from numpydantic.testing.helpers import ValidationCase

pytestmark = pytest.mark.zarr
Expand All @@ -36,7 +33,7 @@ def nested_dir_array(tmp_output_dir_func) -> zarr.NestedDirectoryStore:
def _zarr_array(case: ValidationCase, store) -> zarr.core.Array:
if issubclass(case.dtype, BaseModel):
pytest.skip(
f"Zarr can't handle objects properly at the moment, "
"Zarr can't handle objects properly at the moment, "
"see https://github.com/zarr-developers/zarr-python/issues/2081"
)
# return zarr.full(
Expand Down Expand Up @@ -103,14 +100,14 @@ def test_zarr_from_tuple(array, model_blank, request):
"""Should be able to do the same validation logic from tuples as an input"""
array = request.getfixturevalue(array)
if isinstance(array, ZarrArrayPath):
instance = model_blank(array=(array.file, array.path))
_ = model_blank(array=(array.file, array.path))
else:
instance = model_blank(array=(array,))
_ = model_blank(array=(array,))


def test_zarr_from_path(zarr_array, model_blank):
"""Should be able to just pass a path"""
instance = model_blank(array=zarr_array)
_ = model_blank(array=zarr_array)


def test_zarr_array_path_from_iterable(zarr_array):
Expand Down
3 changes: 2 additions & 1 deletion tests/test_meta.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys

import pytest

from numpydantic import NDArray
Expand Down Expand Up @@ -40,4 +41,4 @@ def test_stub_revealed_type():
"""
Check that the revealed type matches the stub
"""
type = reveal_type(NDArray)
_ = reveal_type(NDArray)
Loading

0 comments on commit 8f1eb6e

Please sign in to comment.