maxfordham / ipyautoui

create ipywidgets user input form pydantic model or jsonschema
https://maxfordham.github.io/ipyautoui/
42 stars 4 forks source link

🐛 pydantic>=2.9 tests failing #342

Closed ollyhensby closed 1 month ago

ollyhensby commented 1 month ago

Describe the bug When pydantic>=2.9, the tests fail:

image

ollyhensby commented 1 month ago

Code to reproduce error:

import jsonref
import stringcase
import typing as ty
from enum import Enum

from pydantic import BaseModel, Field, RootModel, ConfigDict
from ipyautoui.autoobject import AutoObject, AutoObjectForm

class Obj(BaseModel):
    a: int
    b: str

ObjSet = ty.ForwardRef("ObjSet")

class ObjSet(BaseModel):
    obj_set: list[ty.Union[Obj, ObjSet]]

jsonref.replace_refs(ObjSet.model_json_schema())

It appears that replace_refs has stopped working...

ollyhensby commented 1 month ago

What does model_json_schema return?

pyndatic<2.9

{
    "$defs": {
        "Obj": {
            "properties": {
                "a": {
                    "title": "A",
                    "type": "integer"
                },
                "b": {
                    "title": "B",
                    "type": "string"
                }
            },
            "required": [
                "a",
                "b"
            ],
            "title": "Obj",
            "type": "object"
        },
        "ObjSet": {
            "properties": {
                "obj_set": {
                    "items": {
                        "anyOf": [
                            {
                                "$ref": "#/$defs/Obj"
                            },
                            {
                                "$ref": "#/$defs/ObjSet"
                            }
                        ]
                    },
                    "title": "Obj Set",
                    "type": "array"
                }
            },
            "required": [
                "obj_set"
            ],
            "title": "ObjSet",
            "type": "object"
        }
    },
    "allOf": [
        {
            "$ref": "#/$defs/ObjSet"
        }
    ]
}

pydantic 2.9

{
    "$defs": {
        "Obj": {
            "properties": {
                "a": {
                    "title": "A",
                    "type": "integer"
                },
                "b": {
                    "title": "B",
                    "type": "string"
                }
            },
            "required": [
                "a",
                "b"
            ],
            "title": "Obj",
            "type": "object"
        },
        "ObjSet": {
            "properties": {
                "obj_set": {
                    "items": {
                        "anyOf": [
                            {
                                "$ref": "#/$defs/Obj"
                            },
                            {
                                "$ref": "#/$defs/ObjSet"
                            }
                        ]
                    },
                    "title": "Obj Set",
                    "type": "array"
                }
            },
            "required": [
                "obj_set"
            ],
            "title": "ObjSet",
            "type": "object"
        }
    },
    "$ref": "#/$defs/ObjSet"
}

from a quick comparison it looks as though the allOf has disappeared in the model_json_schema for pydantic>=2.9.

ollyhensby commented 1 month ago

I think it may be related to this change: https://github.com/pydantic/pydantic/pull/10029

ollyhensby commented 1 month ago

I've posted an issue about this on the jsonref repository: https://github.com/gazpachoking/jsonref/issues/68