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 should accept Annotated[T, ...] #8735

Closed
1 task done
rijenkii opened this issue Feb 6, 2024 · 3 comments
Closed
1 task done

TypeAdapter should accept Annotated[T, ...] #8735

rijenkii opened this issue Feb 6, 2024 · 3 comments

Comments

@rijenkii
Copy link

rijenkii commented Feb 6, 2024

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

See https://discuss.python.org/t/is-annotated-compatible-with-type-t/43898, microsoft/pyright#7091, https://github.com/python/typing/pull/1618/files.

Basically, I use TypeAdapter(Annotated[...]).validate_python in my custom validators quite ofter, but now after pydantic began to actually follow the PEPs, following examples no longer pass the type checker.

I have submitted this issue because Jelle Zijlstra explicitly said that this is a problem with pydantic types: https://discuss.python.org/t/is-annotated-compatible-with-type-t/43898/24

Example Code

from typing import Annotated, Any
import shortuuid
from uuid import UUID
from pydantic import PlainValidator, Field, TypeAdapter

def _shortuuid_validator(value: Any) -> UUID:
    value = TypeAdapter(
        Annotated[str, Field(min_length=22, max_length=22)]
    ).validate_python(value)
    return shortuuid.decode(value)


type ShortUUID = Annotated[UUID, PlainValidator(_shortuuid_validator)]
from typing import Annotated, Any
from pydantic import BeforeValidator, Field, TypeAdapter

class ArtifactObject(BaseModel):
    name: Annotated[str, Field(pattern=r"^[a-zA-Z0-9_\-]+$")]
    path: Annotated[str, Field(pattern=r"^[a-zA-Z0-9_\-/\\]+$")]


type ArtifactLongStr = Annotated[
    ArtifactObject,
    BeforeValidator(
        lambda x: (lambda y: {"name": y[0], "path": y[1]})(
            TypeAdapter(
                Annotated[str, Field(pattern=r"^[a-zA-Z0-9_\-]+:[a-zA-Z0-9_\-/\\]+$")],
            )
            .validate_python(x)
            .split(":"),
        ),
    ),
]

type ArtifactShortStr = Annotated[
    ArtifactObject,
    BeforeValidator(
        lambda x: (lambda y: {"name": y, "path": y})(
            TypeAdapter(
                Annotated[str, Field(pattern=r"^[a-zA-Z0-9_\-]+$")],
            ).validate_python(x),
        ),
    ),
]
from typing import Annotated, Any, Literal
from pydantic import BeforeValidator, Field, TypeAdapter, AliasPath, Json, Base64Bytes


class _PushBody(BaseModel):
    ref: str
    after: str
    clone_url: Annotated[
        str, Field(validation_alias=AliasPath("repository", "clone_url"))
    ]


class PushPayload(BaseModel):
    event: Annotated[
        Literal["push"], Field(validation_alias=AliasPath("headers", "x-github-event"))
    ]
    body: Annotated[
        Json[_PushBody], BeforeValidator(TypeAdapter(Base64Bytes).validate_python)
    ]

Python, Pydantic & OS Version

> python -c "import pydantic.version; print(pydantic.version.version_info())"
             pydantic version: 2.5.2
        pydantic-core version: 2.14.5
          pydantic-core build: profile=release pgo=true
                 install path: /home/rijenkii/.local/lib/python3.12/site-packages/pydantic
               python version: 3.12.1 (main, Dec 18 2023, 00:00:00) [GCC 13.2.1 20231205 (Red Hat 13.2.1-6)]
                     platform: Linux-6.6.14-200.fc39.x86_64-x86_64-with-glibc2.38
             related packages: email-validator-2.1.0.post1 mypy-1.7.1 pyright-1.1.339 typing_extensions-4.9.0 fastapi-0.109.0
Pylance v2024.1.104 (pre-release) (Pyright 1.1.348).
@rijenkii rijenkii added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Feb 6, 2024
@sydney-runkle
Copy link
Member

@rijenkii,

Thanks for reporting this here as well. @adriangb recently responded on the discussion linked above 👍.

@sydney-runkle sydney-runkle added feature request and removed pending Awaiting a response / confirmation bug V2 Bug related to Pydantic V2 labels Feb 8, 2024
@adriangb
Copy link
Member

adriangb commented Apr 4, 2024

It is a "problem" with pydantic types that as far as I know does not have a real solution until Python makes changes to it's typing system to offer one. And it wasn't a problem until (necessary, correct) breaking changes were made to Python typing that were outside of Pydantic's control.

In the meantime, we relaxed our typing to Any so you won't get a type error as of the next release #8923. But you also won't get any useful typing.

The only thing you can do now is upvote discussion related to TypeForm.

Also, and unrelated, I highly recommend you don't create TypeAdapter instances dynamically. Create them as global constants and reference them from within your lambda functions.

@adriangb adriangb closed this as completed Apr 4, 2024
@sydney-runkle
Copy link
Member

Thanks for the thoughtful reply @adriangb. Makes sense!!

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

3 participants