Skip to content

Commit

Permalink
working on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tlambert03 committed Jun 28, 2023
1 parent df72bd9 commit 430fce7
Show file tree
Hide file tree
Showing 16 changed files with 7,178 additions and 112 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,8 @@ venv.bak/

.DS_Store
_test_data
src/ome_types/model/
src/ome_types/model/ome_*
src/ome_types/_version.py
src/ome_types2/model/ome*
docs/source/_autosummary
.benchmarks/
_build/
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,11 @@ exclude = '''
minversion = "6.0"
addopts = "--benchmark-disable"
testpaths = ["tests"]
filterwarnings = ["error", "ignore:Casting invalid AnnotationID:UserWarning"]
filterwarnings = [
"error",
"ignore:Casting invalid AnnotationID:UserWarning",
"ignore::ResourceWarning", # FIXME: i think this might be an xsdata issue?
]

# https://mypy.readthedocs.io/en/stable/config_file.html
[tool.mypy]
Expand Down
4 changes: 2 additions & 2 deletions src/ome_types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
except PackageNotFoundError:
__version__ = "unknown"

from ome_types2._conversion import from_tiff, from_xml, to_dict, to_xml
from ome_types2.model import OME
from ome_types._conversion import from_tiff, from_xml, to_dict, to_xml
from ome_types.model import OME

__all__ = ["__version__", "OME", "from_xml", "from_tiff", "to_dict", "to_xml"]
11 changes: 6 additions & 5 deletions src/ome_types/_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from xsdata.formats.dataclass.parsers.mixins import XmlHandler
from xsdata_pydantic_basemodel.bindings import XmlContext

from ome_types2.model.ome_2016_06 import OME
from ome_types.model.ome_2016_06 import OME

class ParserKwargs(TypedDict, total=False):
config: ParserConfig
Expand All @@ -36,7 +36,7 @@ def _get_ome(xml: str | bytes) -> type[OME]:
root = ET.fromstring(xml) # noqa: S314

if root.tag == OME_2016_06_NS:
from ome_types2.model import OME
from ome_types.model import OME

return OME
raise NotImplementedError(f"Unknown root tag: {root.tag}")
Expand All @@ -56,11 +56,12 @@ def to_dict(source: OME | Path | str | bytes) -> dict[str, Any]:
def from_xml(
xml: Path | str | bytes,
*,
validate: bool | None = None,
validate: bool | None = None, # TODO implement
parser: Any = None, # TODO deprecate
parser_kwargs: ParserKwargs | None = None,
) -> OME:
if validate:
raise NotImplementedError("validate=True is not supported yet")
# if validate:
# raise NotImplementedError("validate=True is not supported yet")

if isinstance(xml, Path):
xml = str(xml)
Expand Down
21 changes: 18 additions & 3 deletions src/ome_types/_mixins/_base_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
from textwrap import indent
from typing import TYPE_CHECKING, Any, ClassVar, Optional, Sequence, Set, Type, cast

from pydantic import BaseModel, validator
from pydantic import BaseModel, ValidationError, validator

from ome_types2.units import ureg
from ome_types.units import ureg
import pydantic

if TYPE_CHECKING:
import pint
Expand Down Expand Up @@ -55,7 +56,20 @@ class Config:
def __init__(__pydantic_self__, **data: Any) -> None:
if "id" in __pydantic_self__.__fields__:
data.setdefault("id", _AUTO_SEQUENCE)
super().__init__(**data)
try:
super().__init__(**data)
except ValidationError as e:
for err in e.raw_errors:
if isinstance(err, pydantic.error_wrappers.ErrorWrapper):
loc = err.loc_tuple()
if loc:
key = loc[0]
if key == "id":
data[key] = _AUTO_SEQUENCE
else:
data.pop(key, None)
print(data)
super().__init__(**data)

def __init_subclass__(cls) -> None:
"""Add `*_quantity` property for fields that have both a value and a unit.
Expand Down Expand Up @@ -118,6 +132,7 @@ def validate_id(cls, value: Any) -> Any:
if value is _AUTO_SEQUENCE:
# just increment the counter
_COUNTERS[cls] += 1
breakpoint()
return f"{cls.__name__}:{_COUNTERS[cls]}"

raise ValueError(f"Invalid ID value: {value!r}")
Expand Down
2 changes: 1 addition & 1 deletion src/ome_types/_mixins/_instrument.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import TYPE_CHECKING, Protocol, TypeAlias, TypeVar, Union

if TYPE_CHECKING:
from ome_types2.model.ome_2016_06 import (
from ome_types.model import (
Arc,
Filament,
GenericExcitationSource,
Expand Down
12 changes: 6 additions & 6 deletions src/ome_types/_mixins/_ome.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from ._base_type import OMEType

if TYPE_CHECKING:
from ome_types2.model.ome_2016_06 import OME, Reference
from ome_types.model import OME, Reference


class OMEMixin:
Expand All @@ -29,18 +29,18 @@ def __setstate__(self, state: dict[str, Any]) -> None:

@classmethod
def from_xml(cls, xml: Path | str) -> OME:
from ome_types2._conversion import from_xml
from ome_types._conversion import from_xml

return from_xml(xml)

@classmethod
def from_tiff(cls, path: Path | str) -> OME:
from ome_types2._conversion import from_tiff
from ome_types._conversion import from_tiff

return from_tiff(path)

# def to_xml(self) -> str:
# from ome_types2._conversion import to_xml
# from ome_types._conversion import to_xml

# return to_xml(self)

Expand All @@ -51,7 +51,7 @@ def collect_ids(value: Any) -> dict[str, OMEType]:
Recursively walks all dataclass fields and iterates over lists. The base
case is when value is neither a dataclass nor a list.
"""
from ome_types2.model.ome_2016_06 import Reference
from ome_types.model import Reference

ids: dict[str, OMEType] = {}
if isinstance(value, list):
Expand All @@ -77,7 +77,7 @@ def collect_references(value: Any) -> list[Reference]:
that we don't need to inspect further.
"""
from ome_types2.model.ome_2016_06 import Reference
from ome_types.model import Reference

references: list[Reference] = []
if isinstance(value, Reference):
Expand Down
1 change: 1 addition & 0 deletions src/ome_types/model/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from ome_types.model.ome_2016_06 import * # noqa
Empty file.
Loading

0 comments on commit 430fce7

Please sign in to comment.