Skip to content

Work with pydantic 2.10 #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 28, 2024
Merged

Work with pydantic 2.10 #87

merged 2 commits into from
Nov 28, 2024

Conversation

rwb27
Copy link
Collaborator

@rwb27 rwb27 commented Nov 28, 2024

Pydantic 2.10 is stricter about forward references for types. This caused my NDArray annotated type to fail, because the return type of the serialiser function was not in scope when function input/output models were built.

This PR fixes the problem by explicitly specifying a return_type for the serialiser, and tidies up a few loose ends that appeared while debugging. A test is added to ensure this issue will trigger a failure if it recurs.

After much confusion around TypeAlias types, this is fixed by adding
return_type to the PlainSerialiser annotation.

Pydantic 2.10 is stricter about forward references in type annotations. This
becomes a problem for NDArray, because the NestedListOfNumbers
type it returns isn't present in the
scope of the model (which probably
only imported NDArray).

The solution to this is to specify return_type
explicitly, rather than infer it based on the
annotation of the serialiser function.

Using the `type` keyword might help in the future, but is currently only Python 3.12 and up.

As part of this I realised there was some incorrect logic in
`type_to_dataschema` which was
using TypeAdapter on BaseModel
subclasses, which is ineefficient.

A test is added that should fail if this issue resurfaces.
Copy link

barecheck bot commented Nov 28, 2024

Barecheck - Code coverage report

Total: 81.55%

Your code coverage diff: 0.00% ▴

Uncovered files and lines
FileLines
src/labthings_fastapi/thing_description/__init__.py46, 55-56, 110-115, 122-126, 132, 229-230, 237

@rwb27 rwb27 merged commit b446f8d into main Nov 28, 2024
9 checks passed
@rwb27 rwb27 deleted the pydantic-2-10 branch November 28, 2024 15:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant