[Python] Pydantic does not validate dict values when assigning an unexpected type to dict...

Discussão em 'Python' iniciado por Stack, Julho 14, 2025.

  1. Stack

    Stack Membro Participativo

    I have the question same with link Pydantic does not validate the key/values of dict fields

    from typing import Dict
    from pydantic import BaseModel


    class TableModel(BaseModel):
    table: Dict[str, SelfDefinedClass]


    I want to restrict the types in the dictionary to be the same.

    I understand why it's not working through the link. I'm just wondering if there's a recommended way in pydantic to achieve this, other than creating my own subclass of dict.

    Continue reading...

Compartilhe esta Página