diff --git a/pyproject.toml b/pyproject.toml index 2bae93e0..d5d1a792 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -171,5 +171,11 @@ no_implicit_reexport = true ignore_missing_imports = true disallow_untyped_defs = true +[[tool.mypy.overrides]] +module = ['ome_types.model.ome_2016_06.structured_annotations'] +# Definition of "__iter__" in base class "BaseModel" +# is incompatible with definition in base class "Sequence" +disable_error_code = "misc" + [tool.coverage.run] source = ["src/ome_types"] diff --git a/src/ome_autogen/main.py b/src/ome_autogen/main.py index aeb2c02f..14b7d4dc 100644 --- a/src/ome_autogen/main.py +++ b/src/ome_autogen/main.py @@ -61,7 +61,11 @@ def build_model( _print_gray("Running mypy ...") mypy = ["mypy", str(package_dir), "--strict"] - subprocess.check_output(mypy) # noqa S + + try: + subprocess.check_output(mypy, stderr=subprocess.STDOUT) # noqa S + except subprocess.CalledProcessError as e: + raise RuntimeError(f"mypy errors:\n\n{e.output.decode()}") from e # print a bold green checkmark _print_green(f"\u2713 OME python model created at {OUTPUT_PACKAGE}") diff --git a/src/ome_types/_mixins/_structured_annotations.py b/src/ome_types/_mixins/_structured_annotations.py index 00b400ca..5117c908 100644 --- a/src/ome_types/_mixins/_structured_annotations.py +++ b/src/ome_types/_mixins/_structured_annotations.py @@ -25,5 +25,5 @@ def __getitem__(self, key: int | slice) -> Annotation | Sequence[Annotation]: def __len__(self) -> int: return len(list(self._iter_annotations())) - def __iter__(self) -> Iterator[Annotation]: + def __iter__(self) -> Iterator[Annotation]: # type: ignore[override] return self._iter_annotations() diff --git a/src/ome_types/model/_color.py b/src/ome_types/model/_color.py index a2540017..bcdae59e 100644 --- a/src/ome_types/model/_color.py +++ b/src/ome_types/model/_color.py @@ -1,20 +1,21 @@ -from typing import Any +from typing import Any, Tuple, Union from pydantic import color from xsdata.formats.converter import Converter, converter __all__ = ["Color"] +ColorTuple = Union[Tuple[int, int, int], Tuple[int, int, int, float]] +ColorType = Union[ColorTuple, str, int] + class Color(color.Color): - def __init__(self, val: color.ColorType = -1) -> None: + def __init__(self, val: ColorType = -1) -> None: try: - val_int = int(val) # type: ignore + val = self._int2tuple(int(val)) # type: ignore except ValueError: pass - else: - val = self._int2tuple(val_int) - super().__init__(val) + super().__init__(val) # type: ignore [arg-type] @classmethod def _int2tuple(cls, val: int) -> tuple[int, int, int, float]: diff --git a/tests/test_autogen.py b/tests/test_autogen.py index d6afcc0a..ef5d4e6a 100644 --- a/tests/test_autogen.py +++ b/tests/test_autogen.py @@ -2,19 +2,22 @@ import sys from pathlib import Path -import pytest +from _pytest.monkeypatch import MonkeyPatch -ome_autogen = pytest.importorskip("_ome_autogen") -XSD = Path(__file__).parent.parent.parent / "src" / "ome_types" / "ome-2016-06.xsd" +import ome_autogen.main -def test_autogen(tmp_path_factory): +def test_autogen(tmp_path: Path, monkeypatch: MonkeyPatch) -> None: """Test that autogen works without raising an exception. - This does *not* actually test the resulting model. + and that mypy has no issues with it. """ - target_dir = tmp_path_factory.mktemp("_ome_types_test_model") - ome_autogen.convert_schema(url=XSD, target_dir=target_dir) - sys.path.insert(0, str(target_dir.parent)) - assert importlib.import_module(target_dir.name) - sys.path.pop(0) + ome_autogen.main.build_model(output_dir=tmp_path, do_formatting=True, do_mypy=True) + + monkeypatch.delitem(sys.modules, "ome_types") + monkeypatch.delitem(sys.modules, "ome_types.model") + monkeypatch.delitem(sys.modules, "ome_types.model.ome_2016_06") + monkeypatch.syspath_prepend(str(tmp_path)) + mod = importlib.import_module("ome_types.model.ome_2016_06") + assert mod.__file__ and mod.__file__.startswith(str(tmp_path)) + assert mod.Channel(color="blue")