Skip to content
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

ValidationError: ValidationError: [{'loc': ['Categories'], 'err': 'expected type null, found array'}] #642

Open
naviot opened this issue Feb 5, 2025 · 0 comments

Comments

@naviot
Copy link

naviot commented Feb 5, 2025

Steps to reproduce:

from dataclasses import dataclass
from typing import Optional
from apischema import deserialize

@dataclass
class Categories:
    Name: str
    Url: str


@dataclass
class Item:
    Name: str
    Domain: str
    Categories: Optional[list[Categories]] = None


item = deserialize(Item, {'Name': 'name', 'Domain': 'domain', 'Categories': [{'Name': 'name', 'Url': 'url'}]})

this is a kind of edge case but Python allows us to declare in this way and it's a bit tricky to debug

Workaround:

from dataclasses import dataclass
from typing import Optional
from apischema import deserialize

@dataclass
class Category:
    Name: str
    Url: str


@dataclass
class Item:
    Name: str
    Domain: str
    Categories: Optional[list[Category]] = None


item = deserialize(Item, {'Name': 'name', 'Domain': 'domain', 'Categories': [{'Name': 'name', 'Url': 'url'}]})
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

No branches or pull requests

1 participant