python 39 compat

This commit is contained in:
sneakers-the-rat 2024-09-23 23:42:00 -07:00
parent e334232ac4
commit 402bf09cf1
Signed by untrusted user who does not match committer: jonny
GPG key ID: 6DCB96EF1E4D232D

View file

@ -3,10 +3,6 @@ import sys
import pytest import pytest
from typing import Any, Tuple, Union, Type from typing import Any, Tuple, Union, Type
if sys.version_info.minor >= 10:
from typing import TypeAlias
else:
from typing_extensions import TypeAlias
from pydantic import BaseModel, computed_field, ConfigDict from pydantic import BaseModel, computed_field, ConfigDict
from numpydantic import NDArray, Shape from numpydantic import NDArray, Shape
from numpydantic.ndarray import NDArrayMeta from numpydantic.ndarray import NDArrayMeta
@ -15,6 +11,15 @@ import numpy as np
from tests.fixtures import * from tests.fixtures import *
if sys.version_info.minor >= 10:
from typing import TypeAlias
YES_PIPE = True
else:
from typing_extensions import TypeAlias
YES_PIPE = False
def pytest_addoption(parser): def pytest_addoption(parser):
parser.addoption( parser.addoption(
@ -80,8 +85,9 @@ INTEGER: TypeAlias = NDArray[Shape["*, *, *"], Integer]
FLOAT: TypeAlias = NDArray[Shape["*, *, *"], Float] FLOAT: TypeAlias = NDArray[Shape["*, *, *"], Float]
STRING: TypeAlias = NDArray[Shape["*, *, *"], str] STRING: TypeAlias = NDArray[Shape["*, *, *"], str]
MODEL: TypeAlias = NDArray[Shape["*, *, *"], BasicModel] MODEL: TypeAlias = NDArray[Shape["*, *, *"], BasicModel]
UNION_PIPE: TypeAlias = NDArray[Shape["*, *, *"], np.uint32 | np.float32]
UNION_TYPE: TypeAlias = NDArray[Shape["*, *, *"], Union[np.uint32, np.float32]] UNION_TYPE: TypeAlias = NDArray[Shape["*, *, *"], Union[np.uint32, np.float32]]
if YES_PIPE:
UNION_PIPE: TypeAlias = NDArray[Shape["*, *, *"], np.uint32 | np.float32]
@pytest.fixture( @pytest.fixture(
@ -121,82 +127,93 @@ def shape_cases(request) -> ValidationCase:
return request.param return request.param
@pytest.fixture( DTYPE_CASES = [
scope="module", ValidationCase(dtype=float, passes=True),
params=[ ValidationCase(dtype=int, passes=False),
ValidationCase(dtype=float, passes=True), ValidationCase(dtype=np.uint8, passes=False),
ValidationCase(dtype=int, passes=False), ValidationCase(annotation=NUMBER, dtype=int, passes=True),
ValidationCase(dtype=np.uint8, passes=False), ValidationCase(annotation=NUMBER, dtype=float, passes=True),
ValidationCase(annotation=NUMBER, dtype=int, passes=True), ValidationCase(annotation=NUMBER, dtype=np.uint8, passes=True),
ValidationCase(annotation=NUMBER, dtype=float, passes=True), ValidationCase(annotation=NUMBER, dtype=np.float16, passes=True),
ValidationCase(annotation=NUMBER, dtype=np.uint8, passes=True), ValidationCase(annotation=NUMBER, dtype=str, passes=False),
ValidationCase(annotation=NUMBER, dtype=np.float16, passes=True), ValidationCase(annotation=INTEGER, dtype=int, passes=True),
ValidationCase(annotation=NUMBER, dtype=str, passes=False), ValidationCase(annotation=INTEGER, dtype=np.uint8, passes=True),
ValidationCase(annotation=INTEGER, dtype=int, passes=True), ValidationCase(annotation=INTEGER, dtype=float, passes=False),
ValidationCase(annotation=INTEGER, dtype=np.uint8, passes=True), ValidationCase(annotation=INTEGER, dtype=np.float32, passes=False),
ValidationCase(annotation=INTEGER, dtype=float, passes=False), ValidationCase(annotation=INTEGER, dtype=str, passes=False),
ValidationCase(annotation=INTEGER, dtype=np.float32, passes=False), ValidationCase(annotation=FLOAT, dtype=float, passes=True),
ValidationCase(annotation=INTEGER, dtype=str, passes=False), ValidationCase(annotation=FLOAT, dtype=np.float32, passes=True),
ValidationCase(annotation=FLOAT, dtype=float, passes=True), ValidationCase(annotation=FLOAT, dtype=int, passes=False),
ValidationCase(annotation=FLOAT, dtype=np.float32, passes=True), ValidationCase(annotation=FLOAT, dtype=np.uint8, passes=False),
ValidationCase(annotation=FLOAT, dtype=int, passes=False), ValidationCase(annotation=FLOAT, dtype=str, passes=False),
ValidationCase(annotation=FLOAT, dtype=np.uint8, passes=False), ValidationCase(annotation=STRING, dtype=str, passes=True),
ValidationCase(annotation=FLOAT, dtype=str, passes=False), ValidationCase(annotation=STRING, dtype=int, passes=False),
ValidationCase(annotation=STRING, dtype=str, passes=True), ValidationCase(annotation=STRING, dtype=float, passes=False),
ValidationCase(annotation=STRING, dtype=int, passes=False), ValidationCase(annotation=MODEL, dtype=BasicModel, passes=True),
ValidationCase(annotation=STRING, dtype=float, passes=False), ValidationCase(annotation=MODEL, dtype=BadModel, passes=False),
ValidationCase(annotation=MODEL, dtype=BasicModel, passes=True), ValidationCase(annotation=MODEL, dtype=int, passes=False),
ValidationCase(annotation=MODEL, dtype=BadModel, passes=False), ValidationCase(annotation=MODEL, dtype=SubClass, passes=True),
ValidationCase(annotation=MODEL, dtype=int, passes=False), ValidationCase(annotation=UNION_TYPE, dtype=np.uint32, passes=True),
ValidationCase(annotation=MODEL, dtype=SubClass, passes=True), ValidationCase(annotation=UNION_TYPE, dtype=np.float32, passes=True),
ValidationCase(annotation=UNION_PIPE, dtype=np.uint32, passes=True), ValidationCase(annotation=UNION_TYPE, dtype=np.uint64, passes=False),
ValidationCase(annotation=UNION_PIPE, dtype=np.float32, passes=True), ValidationCase(annotation=UNION_TYPE, dtype=np.float64, passes=False),
ValidationCase(annotation=UNION_PIPE, dtype=np.uint64, passes=False), ValidationCase(annotation=UNION_TYPE, dtype=str, passes=False),
ValidationCase(annotation=UNION_PIPE, dtype=np.float64, passes=False), ]
ValidationCase(annotation=UNION_PIPE, dtype=str, passes=False),
ValidationCase(annotation=UNION_TYPE, dtype=np.uint32, passes=True), DTYPE_IDS = [
ValidationCase(annotation=UNION_TYPE, dtype=np.float32, passes=True), "float",
ValidationCase(annotation=UNION_TYPE, dtype=np.uint64, passes=False), "int",
ValidationCase(annotation=UNION_TYPE, dtype=np.float64, passes=False), "uint8",
ValidationCase(annotation=UNION_TYPE, dtype=str, passes=False), "number-int",
], "number-float",
ids=[ "number-uint8",
"float", "number-float16",
"int", "number-str",
"uint8", "integer-int",
"number-int", "integer-uint8",
"number-float", "integer-float",
"number-uint8", "integer-float32",
"number-float16", "integer-str",
"number-str", "float-float",
"integer-int", "float-float32",
"integer-uint8", "float-int",
"integer-float", "float-uint8",
"integer-float32", "float-str",
"integer-str", "str-str",
"float-float", "str-int",
"float-float32", "str-float",
"float-int", "model-model",
"float-uint8", "model-badmodel",
"float-str", "model-int",
"str-str", "model-subclass",
"str-int", "union-type-uint32",
"str-float", "union-type-float32",
"model-model", "union-type-uint64",
"model-badmodel", "union-type-float64",
"model-int", "union-type-str",
"model-subclass", ]
"union-pipe-uint32",
"union-pipe-float32", if YES_PIPE:
"union-pipe-uint64", DTYPE_CASES.extend(
"union-pipe-float64", [
"union-pipe-str", ValidationCase(annotation=UNION_PIPE, dtype=np.uint32, passes=True),
"union-type-uint32", ValidationCase(annotation=UNION_PIPE, dtype=np.float32, passes=True),
"union-type-float32", ValidationCase(annotation=UNION_PIPE, dtype=np.uint64, passes=False),
"union-type-uint64", ValidationCase(annotation=UNION_PIPE, dtype=np.float64, passes=False),
"union-type-float64", ValidationCase(annotation=UNION_PIPE, dtype=str, passes=False),
"union-type-str", ]
], )
) DTYPE_IDS.extend(
[
"union-pipe-uint32",
"union-pipe-float32",
"union-pipe-uint64",
"union-pipe-float64",
"union-pipe-str",
]
)
@pytest.fixture(scope="module", params=DTYPE_CASES, ids=DTYPE_IDS)
def dtype_cases(request) -> ValidationCase: def dtype_cases(request) -> ValidationCase:
return request.param return request.param