Skip to content

Commit

Permalink
fix: use lax validation by default (#159)
Browse files Browse the repository at this point in the history
* fix: use lax validation

* style: [pre-commit.ci] auto fixes [...]

* fix autogen, fix tests

* xfail round trip

* test: update test

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
tlambert03 and pre-commit-ci[bot] authored Dec 15, 2022
1 parent 520b90f commit d14a7a0
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 3 deletions.
28 changes: 27 additions & 1 deletion src/ome_autogen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,30 @@ def fname(self) -> str:
_url = os.path.join(_this_dir, "ome_types", "ome-2016-06.xsd")
_target = os.path.join(_this_dir, "ome_types", "model")

AnnotationID_Override = r"""class AnnotationID(LSID):
regex = re.compile(
r"(urn:lsid:([\w\-\.]+\.[\w\-\.]+)+:Annotation:\S+)|(Annotation:\S+)"
)
@classmethod
def __get_validators__(cls):
yield cls.validate
@classmethod
def validate(cls, v) -> "AnnotationID":
if not cls.regex.match(v):
search = cls.regex.search(v)
if search:
import warnings
new_v = search.group()
warnings.warn(f"Casting invalid AnnotationID {v!r} to {new_v!r}")
v = new_v
return v
"""

_SIMPLE_OVERRIDES = {"AnnotationID": AnnotationID_Override}


def convert_schema(url: str = _url, target_dir: str = _target) -> None:
print("Inspecting XML schema ...")
Expand Down Expand Up @@ -1052,7 +1076,9 @@ def convert_schema(url: str = _url, target_dir: str = _target) -> None:
init_imports.append((converter.fname, elem.local_name))
converter.write(filename=targetfile)

text = "\n".join([s.format() for s in simples])
text = "\n".join(
[_SIMPLE_OVERRIDES.get(s.elem.local_name) or s.format() for s in simples]
)
text = black_format(sort_imports(text))
with open(os.path.join(target_dir, "simple_types.py"), "w", encoding="utf-8") as f:
f.write(text)
Expand Down
2 changes: 1 addition & 1 deletion src/ome_types/_xmlschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def xmlschema2dict(
xml: str,
schema: Optional[xmlschema.XMLSchema] = None,
converter: XMLSchemaConverter = OMEConverter,
validate: bool = True,
validate: bool = False,
**kwargs: Any,
) -> Dict[str, Any]:
if isinstance(xml, bytes):
Expand Down
78 changes: 78 additions & 0 deletions tests/data/invalid_xml_annotation.ome.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<OME xmlns="http://www.openmicroscopy.org/Schemas/OME/2016-06"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openmicroscopy.org/Schemas/OME/2016-06
http://www.openmicroscopy.org/Schemas/OME/2016-06/ome.xsd">
<Image ID="Image:0" Name="P_t00000,W_s00_0">
<Pixels BigEndian="false" DimensionOrder="XYZTC" ID="Pixels:0" Interleaved="false" PhysicalSizeX="0.08850000022125" PhysicalSizeXUnit="µm" PhysicalSizeY="0.08850000022125" PhysicalSizeYUnit="µm" PhysicalSizeZ="1.0" PhysicalSizeZUnit="µm" SignificantBits="16" SizeC="2" SizeT="11" SizeX="171" SizeY="196" SizeZ="5" Type="uint16">
<Channel ID="Channel:0:0" SamplesPerPixel="1">
<LightPath/>
</Channel>
<Channel ID="Channel:0:1" SamplesPerPixel="1">
<LightPath/>
</Channel>
<MetadataOnly/>
</Pixels>
</Image>
<StructuredAnnotations>
<XMLAnnotation ID="XMLAnnotation:0">
<Value>
<SpimData version="0.2">
<BasePath type="relative">.</BasePath>
<SequenceDescription>
<ImageLoader format="bdv.hdf5">
<hdf5 type="relative">xyzct_16bit__mitosis.h5</hdf5>
</ImageLoader>
<ViewSetups>
<ViewSetup>
<id>0</id>
<name>channel 1</name>
<size>171 196 5</size>
<voxelSize>
<unit>µm</unit>
<size>0.08850000022125 0.08850000022125 1.0</size>
</voxelSize>
<attributes>
<channel>1</channel>
</attributes>
</ViewSetup>
<ViewSetup>
<id>1</id>
<name>channel 2</name>
<size>171 196 5</size>
<voxelSize>
<unit>µm</unit>
<size>0.08850000022125 0.08850000022125 1.0</size>
</voxelSize>
<attributes>
<channel>2</channel>
</attributes>
</ViewSetup>
<Attributes name="channel">
<Channel>
<id>1</id>
<name>1</name>
</Channel>
<Channel>
<id>2</id>
<name>2</name>
</Channel>
</Attributes>
</ViewSetups>
<Timepoints type="range">
<first>0</first>
<last>10</last>
</Timepoints>
</SequenceDescription>
<ViewRegistrations>
<ViewRegistration setup="0" timepoint="0">
<ViewTransform type="affine">
<affine>0.08850000022125 0.0 0.0 0.0 0.0 0.08850000022125 0.0 0.0 0.0 0.0 1.0
0.0</affine>
</ViewTransform>
</ViewRegistration>
</ViewRegistrations>
</SpimData>
</Value>
</XMLAnnotation>
</StructuredAnnotations>
</OME>
15 changes: 15 additions & 0 deletions tests/test_invalid_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from pathlib import Path

import pytest

from ome_types import from_xml

DATA = Path(__file__).parent / "data"


def test_bad_xml_annotation() -> None:
"""Test that a bad XML annotation is not imported"""
with pytest.warns(match="Casting invalid AnnotationID"):
ome = from_xml(DATA / "invalid_xml_annotation.ome.xml")
assert len(ome.images) == 1
assert ome.structured_annotations[0].id == "Annotation:0"
7 changes: 6 additions & 1 deletion tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
# Some timestamps have negative years which datetime doesn't support.
"timestampannotation",
}
SHOULD_FAIL_VALIDATION = {"invalid_xml_annotation"}
SHOULD_RAISE_READ = {"bad"}
SHOULD_FAIL_ROUNDTRIP = {
# Order of elements in StructuredAnnotations and Union are jumbled.
"timestampannotation-posix-only",
"transformations-downgrade",
"invalid_xml_annotation",
}
SHOULD_FAIL_ROUNDTRIP_LXML = {
"folders-simple-taxonomy",
Expand Down Expand Up @@ -81,7 +83,8 @@ def true_stem(p):
@pytest.mark.parametrize("validate", validate)
def test_from_xml(xml, parser: str, validate: bool, benchmark):

if true_stem(xml) in SHOULD_RAISE_READ:
should_raise = SHOULD_RAISE_READ.union(SHOULD_FAIL_VALIDATION if validate else [])
if true_stem(xml) in should_raise:
with pytest.raises(
(XMLSchemaValidateError, ValidationError, XMLSchemaValidationError)
):
Expand Down Expand Up @@ -171,6 +174,8 @@ def test_serialization(xml, validate, parser):
"""Test pickle serialization and reserialization."""
if true_stem(xml) in SHOULD_RAISE_READ:
pytest.skip("Can't pickle unreadable xml")
if validate and true_stem(xml) in SHOULD_FAIL_VALIDATION:
pytest.skip("Can't pickle invalid xml with validate=True")

ome = from_xml(xml, parser=parser, validate=validate)
serialized = pickle.dumps(ome)
Expand Down

0 comments on commit d14a7a0

Please sign in to comment.