Home / File/ test_inherited_custom_class.py — fastapi Source File

test_inherited_custom_class.py — fastapi Source File

Architecture documentation for test_inherited_custom_class.py, a python file in the fastapi codebase. 5 imports, 0 dependents.

File python FastAPI Routing 5 imports 1 functions 2 classes

Entity Profile

Dependency Diagram

graph LR
  28da70dd_46c1_67e6_3846_754eca0b4f7d["test_inherited_custom_class.py"]
  90bacbf7_ee4e_4955_c1b3_1214f56df7b1["uuid"]
  28da70dd_46c1_67e6_3846_754eca0b4f7d --> 90bacbf7_ee4e_4955_c1b3_1214f56df7b1
  5befe8bf_65d1_d058_6b78_4a597a8488e9["pytest"]
  28da70dd_46c1_67e6_3846_754eca0b4f7d --> 5befe8bf_65d1_d058_6b78_4a597a8488e9
  534f6e44_61b8_3c38_8b89_6934a6df9802["__init__.py"]
  28da70dd_46c1_67e6_3846_754eca0b4f7d --> 534f6e44_61b8_3c38_8b89_6934a6df9802
  a7c04dee_ee23_5891_b185_47ff6bed036d["testclient.py"]
  28da70dd_46c1_67e6_3846_754eca0b4f7d --> a7c04dee_ee23_5891_b185_47ff6bed036d
  6913fbd4_39df_d14b_44bb_522e99b65b90["pydantic"]
  28da70dd_46c1_67e6_3846_754eca0b4f7d --> 6913fbd4_39df_d14b_44bb_522e99b65b90
  style 28da70dd_46c1_67e6_3846_754eca0b4f7d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import uuid

import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from pydantic import BaseModel


class MyUuid:
    def __init__(self, uuid_string: str):
        self.uuid = uuid_string

    def __str__(self):
        return self.uuid

    @property  # type: ignore
    def __class__(self):
        return uuid.UUID

    @property
    def __dict__(self):
        """Spoof a missing __dict__ by raising TypeError, this is how
        asyncpg.pgroto.pgproto.UUID behaves"""
        raise TypeError("vars() argument must have __dict__ attribute")


def test_pydanticv2():
    from pydantic import field_serializer

    app = FastAPI()

    @app.get("/fast_uuid")
    def return_fast_uuid():
        asyncpg_uuid = MyUuid("a10ff360-3b1e-4984-a26f-d3ab460bdb51")
        assert isinstance(asyncpg_uuid, uuid.UUID)
        assert type(asyncpg_uuid) is not uuid.UUID
        with pytest.raises(TypeError):
            vars(asyncpg_uuid)
        return {"fast_uuid": asyncpg_uuid}

    class SomeCustomClass(BaseModel):
        model_config = {"arbitrary_types_allowed": True}

        a_uuid: MyUuid

        @field_serializer("a_uuid")
        def serialize_a_uuid(self, v):
            return str(v)

    @app.get("/get_custom_class")
    def return_some_user():
        # Test that the fix also works for custom pydantic classes
        return SomeCustomClass(a_uuid=MyUuid("b8799909-f914-42de-91bc-95c819218d01"))

    client = TestClient(app)

    with client:
        response_simple = client.get("/fast_uuid")
        response_pydantic = client.get("/get_custom_class")

    assert response_simple.json() == {
        "fast_uuid": "a10ff360-3b1e-4984-a26f-d3ab460bdb51"
    }

    assert response_pydantic.json() == {
        "a_uuid": "b8799909-f914-42de-91bc-95c819218d01"
    }

Domain

Subdomains

Functions

Dependencies

Frequently Asked Questions

What does test_inherited_custom_class.py do?
test_inherited_custom_class.py is a source file in the fastapi codebase, written in python. It belongs to the FastAPI domain, Routing subdomain.
What functions are defined in test_inherited_custom_class.py?
test_inherited_custom_class.py defines 1 function(s): test_pydanticv2.
What does test_inherited_custom_class.py depend on?
test_inherited_custom_class.py imports 5 module(s): __init__.py, pydantic, pytest, testclient.py, uuid.
Where is test_inherited_custom_class.py in the architecture?
test_inherited_custom_class.py is located at tests/test_inherited_custom_class.py (domain: FastAPI, subdomain: Routing, directory: tests).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free