Skip to content

tests/test_fastapi/test_fastapi_usage.py::test_read_main fails with Pydantic 2.13 #1692

@cjwatson

Description

@cjwatson

Describe the bug
tests/test_fastapi/test_fastapi_usage.py::test_read_main fails with Pydantic 2.13.

To Reproduce
In a clone of ormar:

  1. poetry install -E all
  2. `poetry add 'pydantic>=2.13'
  3. .venv/bin/pytest tests/test_fastapi/test_fastapi_usage.py -vv
FAILED tests/test_fastapi/test_fastapi_usage.py::test_read_main - AssertionError: assert {'id': 1, 'name': 'test', 'category': {'id': None, 'name': 'test cat'}} == {'category': {'id': None, 'items': [{'category': {'id': None, 'name': 'test cat'}, 'id': 1, 'name': 'test'}], 'name': 'test cat'}, 'id': 1, 'name': 'test'}

  Common items:
  {'id': 1, 'name': 'test'}
  Differing items:
  {'category': {'id': None, 'name': 'test cat'}} != {'category': {'id': None, 'items': [{'category': {'id': None, 'name': 'test cat'}, 'id': 1, 'name': 'test'}], 'name': 'test cat'}}

  Full diff:
    {
        'category': {
            'id': None,
  -         'items': [
  -             {
  -                 'category': {
  -                     'id': None,
  -                     'name': 'test cat',
  -                 },
  -                 'id': 1,
  -                 'name': 'test',
  -             },
  -         ],
            'name': 'test cat',
        },
        'id': 1,
        'name': 'test',
    }

Expected behavior
Passing tests.

Versions (please complete the following information):
All versions as in current poetry.lock, except for pydantic-core 2.46.4 and pydantic 2.13.4.

Additional context
I realize that this isn't what the Poetry lock file specifies, but Debian unstable currently has pydantic 2.13.3 and we're running into this in distribution-wide tests there. It's also visible in #1660.

I tracked this down as far as pydantic.type_adapter.TypeAdapter.dump_json. Setting a breakpoint there, I find that instance is something along the lines of Item({'id': 1, 'name': 'test', 'category': Category({'id': None, 'name': 'test cat', 'items': [<weakproxy at 0x7f92342dbf60; to 'tests.test_fastapi.test_fastapi_usage.Item' at 0x7f92342e04b0>]})}). pydantic 2.12.5 serializes this as ormar's tests expect, but pydantic 2.13.4 drops the items. But I'm not sure whether this is because of a change in the handling of weakref proxies, or because something is different about the instance's pydantic metadata. Hopefully you can figure it out?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions