bioimage-io / spec-bioimage-io

Specification for the bioimage.io model description file.
https://bioimage-io.github.io/spec-bioimage-io/
MIT License
18 stars 17 forks source link

SpaceInputAxis id not taken into account with Pydantic 2.9 #631

Closed jdeschamps closed 1 month ago

jdeschamps commented 2 months ago

Pydantic 2.9 was released yesterday and it caused ids to not properly validates in SpaceInputAxis. Given the following snippet:

SpaceInputAxis(id=AxisId('x'), size=32)

We get different class attributes value depending on the package version.

Pydantic 2.8:

> SpaceInputAxis(size=32, id='x', description='', type='space', unit=None, scale=1.0, concatenable=False)

Pydantic 2.9:

> SpaceInputAxis(size=32, id=None, description='', type='space', unit=None, scale=1.0, concatenable=False)

which down the line raises an error when trying to package all this into a InputTensorDescr:

pydantic_core._pydantic_core.ValidationError: 1 validation error for InputTensorDescr
axes
  Value error, Duplicate axis ids: {None} [type=value_error, input_value=[BatchAxis(id='batch', de....0, concatenable=False)], input_type=list]
    For further information visit https://errors.pydantic.dev/2.9/v/value_error
FynnBe commented 2 months ago

until we found the issue we could pin pydantic to <2.9. PR for fix or the pin welcome

mese79 commented 2 months ago

I couldn't find what really causes this problem but if I comment this line: NonBatchAxisId = Annotated[AxisId, Predicate(lambda x: x != "batch")] The axis will have the given id.
So, instead of Predicate from annotated_types package, I used pydantic StringConstraints with a regex pattern:

NonBatchAxisId = Annotated[AxisId, StringConstraints(pattern="[^batch$].*")]

Then the axis will receive the AxisId properly.

from bioimageio.spec.model.v0_5 import SpaceInputAxis

ax1 = SpaceInputAxis(id=AxisId("y"), size=100)
print(ax1)
# size=100 id='y' description='' type='space' unit=None scale=1.0 concatenable=False
FynnBe commented 2 months ago

caused by https://github.com/pydantic/pydantic/issues/10319 fixed on pydantic side with https://github.com/pydantic/pydantic/pull/10321 We should update our pydantic pin as commented in #632 to avoid running into this issue.

FynnBe commented 1 month ago

issue was in pydantic and has been patched