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

Provide a mode parameter in validate_python and model_validate #9009

Open
3 of 13 tasks
DanielRosenwasser opened this issue Mar 14, 2024 · 3 comments
Open
3 of 13 tasks

Comments

@DanielRosenwasser
Copy link

DanielRosenwasser commented Mar 14, 2024

Initial Checks

  • I have searched Google & GitHub for similar requests and couldn't find anything
  • I have read and followed the docs and still think this feature is missing

Description

Given the following:

from dataclasses import dataclass
import pydantic
import json

@dataclass
class Blah:
  a: str

adapted_model = pydantic.TypeAdapter(Blah) 

The following two calls will differ in behavior:

# works! ✅
adapted.model.validate_json('{"a": "hello!"}', strict=True)

# fails! ❌
adapted_model.validate_python(json.loads('{"a": "hello!"}'), strict=True)

This is because validate_python really expects an instance of a Blah instead of a dict, whereas validate_json loosens the rules here.

This is a problem if the data has already been parsed from JSON, or comes from a source other than JSON (e.g. YAML).

A suggestion for exposing something like this already exists over at pydantic-core (pydantic/pydantic-core#712).

This issue is really more meant to track exposing the same logic in the appropriate methods Pydantic ships.

Affected Components

@sydney-runkle
Copy link
Member

@DanielRosenwasser,

Thanks for the well thought out feature request. Do you feel as though the feature request you have here is different enough from #8086 that we should leave both issues open?

If so, happy to leave this open. We could add this to the v2.8.0 milestone 👍

@DanielRosenwasser
Copy link
Author

My hunch is that since that one is more concerned with serialization, and this one is concerned with validation (plus considering some of the involved issues discussed at pydantic/pydantic-core#712 and pydantic/pydantic-core#1226), it might be better to keep both of these open.

I'll defer to your expertise and familiarity here - I'm sure the core maintainers are way more familiar with what needs to be done to close out both issues at the same time.

@sydney-runkle
Copy link
Member

Let's leave this open for now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants