8000 🐛 Fix optional sequence handling in `serialize sequence value` with Pydantic V2 by YuriiMotov · Pull Request #14297 · fastapi/fastapi · GitHub 8000
[go: up one dir, main page]

Skip to content

Conversation

@YuriiMotov
Copy link
Member

Currently the following test code leads to TypeError: issubclass() arg 1 must be a class (with Python 3.14 error is different, but the root cause is the same):

from typing import List, Optional

from fastapi import FastAPI, File
from fastapi.testclient import TestClient

app = FastAPI()


@app.post("/files")
async def upload_files(files: Optional[List[bytes]] = File(None)):
    if files is None:
        return {"files_count": 0}
    return {"files_count": len(files), "sizes": [len(f) for f in files]}


def test_optional_bytes_list():
    client = TestClient(app)
    response = client.post(
        "/files",
        files=[("files", b"content1"), ("files", b"content2")],
    )
    assert response.status_code == 200
    assert response.json() == {"files_count": 2, "sizes": [8, 8]}

This is because for optional sequence the origin_type in the following line is Union:

assert issubclass(origin_type, shared.sequence_types) # type: ignore[arg-type]

So, we need to unwrap that Uinion, take non-None type from its arguments and use it as origin_type

@YuriiMotov YuriiMotov added the bug Something isn't working label Nov 5, 2025
@YuriiMotov YuriiMotov marked this pull request as ready for review November 5, 2025 13:17
Copy link
Member
@tiangolo tiangolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thank you @YuriiMotov! 🙌

This will be available in FastAPI 0.123.3, released in the next hours. 🚀

@tiangolo tiangolo merged commit 0f613d9 into fastapi:master Dec 2, 2025
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

0