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

TypeAdapter fails to validate against generic dataclass when using different type parameters #7430

Closed
1 task done
timwie opened this issue Sep 13, 2023 · 1 comment · Fixed by #7435
Closed
1 task done
Assignees
Labels
bug V2 Bug related to Pydantic V2

Comments

@timwie
Copy link

timwie commented Sep 13, 2023

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

I've just started using pydantic, and I'm using TypeAdapters to turn JSON strings into dataclass instances. This works really well until I try to validate the same generic class with a different type parameter. It seems to me that in my example, once you create a TypeAdapter for Outer[int], you can only ever validate that exact type, and not types that use a different type parameter like Outer[str].

Example Code

from pydantic import TypeAdapter
from pydantic.dataclasses import dataclass
from typing import Generic, TypeVar

T = TypeVar("T")

@dataclass
class Outer(Generic[T]):
    data: T

json1 = '{"data": 42}'
json2 = '{"data": "something else"}'

adapter1 = TypeAdapter(Outer[int])
adapter2 = TypeAdapter(Outer[str])

assert Outer(42) == adapter1.validate_json(json1, strict=True)  # works
assert Outer("something else") == adapter2.validate_json(json2, strict=True)  # crashes

# pydantic_core._pydantic_core.ValidationError: 1 validation error for Outer
# data
#   Input should be a valid integer [type=int_type, input_value='something else', input_type=str]
#     For further information visit https://errors.pydantic.dev/2.3/v/int_type

Python, Pydantic & OS Version

pydantic version: 2.3.0
        pydantic-core version: 2.6.3
          pydantic-core build: profile=release pgo=false
                 install path: /Users/timwie/.pyenv/versions/3.9.17/envs/aio-taginfo39/lib/python3.9/site-packages/pydantic
               python version: 3.9.17 (main, Jun 26 2023, 11:24:22)  [Clang 14.0.3 (clang-1403.0.22.14.1)]
                     platform: macOS-13.5.2-arm64-arm-64bit
     optional deps. installed: ['typing-extensions']
@timwie timwie added bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable labels Sep 13, 2023
@sydney-runkle
Copy link
Member

Hey @timwie, thanks for bringing this to our attention. This is a bug. I'm working on a fix now and will get a PR up shortly.

@sydney-runkle sydney-runkle removed the unconfirmed Bug not yet confirmed as valid/applicable label Sep 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants