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

NotRequired receives (T,) instead of T in Generic TypedDict #7931

Closed
1 task done
NCPlayz opened this issue Oct 26, 2023 · 2 comments · Fixed by #7932
Closed
1 task done

NotRequired receives (T,) instead of T in Generic TypedDict #7931

NCPlayz opened this issue Oct 26, 2023 · 2 comments · Fixed by #7932
Assignees
Labels
bug V2 Bug related to Pydantic V2

Comments

@NCPlayz
Copy link

NCPlayz commented Oct 26, 2023

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Pydantic seems to resolve the Generic in a TypedDict as a tuple value for NotRequired and Required, which only accepts single types. (str,) should become str.

Traceback (most recent call last):
  File "test.py", line 18, in <module>
    FooWithStrAdapter = TypeAdapter(FooWithStr)
... # truncated error
  File "env/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 830, in _match_generic_type
    return self._typed_dict_schema(obj, origin)
  File "env/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 1097, in _typed_dict_schema
    annotation = replace_types(annotation, typevars_map)
  File "env/lib/python3.10/site-packages/pydantic/_internal/_generics.py", line 314, in replace_types
    return origin_type[resolved_type_args]
  File "/opt/conda/envs/py310/lib/python3.10/typing.py", line 312, in inner
    return func(*args, **kwds)
  File "/opt/conda/envs/py310/lib/python3.10/typing.py", line 403, in __getitem__
    return self._getitem(self, parameters)
  File "env/lib/python3.10/site-packages/typing_extensions.py", line 1884, in NotRequired
    item = typing._type_check(parameters, f'{self._name} accepts only a single type.')
  File "/opt/conda/envs/py310/lib/python3.10/typing.py", line 176, in _type_check
    raise TypeError(f"{msg} Got {arg!r:.100}.")
TypeError: NotRequired accepts only a single type. Got (<class 'str'>,).

Example Code

from typing_extensions import TypedDict, TypeVar, NotRequired, Generic
from pydantic import TypeAdapter

T = TypeVar("T")


class FooStr(TypedDict):
    type: NotRequired[str]


class FooGeneric(TypedDict, Generic[T]):
    type: NotRequired[T]


FooWithStr = FooGeneric[str]

FooStrAdapter = TypeAdapter(FooStr# passes
FooWithStrAdapter = TypeAdapter(FooWithStr) # fails

Python, Pydantic & OS Version

pydantic version: 2.4.2
        pydantic-core version: 2.10.1
          pydantic-core build: profile=release pgo=true
                 install path: env/lib/python3.10/site-packages/pydantic
               python version: 3.10.5 | packaged by conda-forge | (main, Jun 14 2022, 07:06:46) [GCC 10.3.0]
                     platform: Linux
             related packages: typing_extensions-4.8.0 fastapi-0.104.0
@NCPlayz NCPlayz added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Oct 26, 2023
@sydney-runkle sydney-runkle removed the pending Awaiting a response / confirmation label Oct 26, 2023
@sydney-runkle sydney-runkle self-assigned this Oct 26, 2023
@sydney-runkle
Copy link
Member

Hi @NCPlayz,

Thanks for reporting this. This does look like a bug. I'll have a fix up shortly 🐛 🔨 !

@sydney-runkle
Copy link
Member

@NCPlayz,

I just merged the fix - this fix should be active in the next release :).

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.

2 participants