bioimage-io/spec-bioimage-io

SpaceInputAxis id not taken into account with Pydantic 2.9

Closed this issue · 4 comments

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

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

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

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

issue was in pydantic and has been patched