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

Support custom generic container types in v2 #5019

Closed
dmontagu opened this issue Feb 6, 2023 · 4 comments
Closed

Support custom generic container types in v2 #5019

dmontagu opened this issue Feb 6, 2023 · 4 comments
Assignees

Comments

@dmontagu
Copy link
Contributor

dmontagu commented Feb 6, 2023

In v2, currently the following code:

from typing import TypeVar, List

from pydantic import BaseModel

T = TypeVar('T')


class GenericList(List[T]):
    pass


class Model(BaseModel):
    field: GenericList[int]

results in:

pydantic_core._pydantic_core.SchemaError: Invalid Schema:
typed-dict -> fields -> field -> schema
  Input tag 'genericlist' found using self-schema does not match any of the expected tags: 'any', 'none', 'bool', 'int', 'float', 'str', 'bytes', 'date', 'time', 'datetime', 'timedelta', 'literal', 'is-instance', 'is-subclass', 'callable', 'list', 'tuple-positional', 'tuple-variable', 'set', 'frozenset', 'generator', 'dict', 'function', 'function-wrap', 'function-plain', 'default', 'nullable', 'union', 'tagged-union', 'chain', 'lax-or-strict', 'typed-dict', 'model', 'arguments', 'call', 'recursive-ref', 'custom-error', 'json', 'url', 'multi-host-url' [type=union_tag_invalid, input_value={'type': 'genericlist', '...chema': {'type': 'int'}}, input_type=dict]

I believe support for custom generic containers was added to v1.X by PR #2554; that PR referenced this comment: #2465 (comment) as providing its motivation.

@dmontagu dmontagu added this to the Version 2 Issues milestone Feb 6, 2023
@samuelcolvin
Copy link
Member

Should be able to cover this with the same logic we use for deque.

@dmontagu
Copy link
Contributor Author

dmontagu commented Apr 28, 2023

I think actually we maybe shouldn't support this (though we should make sure we provide a better error) out of the box, but should instead raise an error message that links to documentation for how to implement __get_pydantic_core_schema__ for the custom generic class.

The gist I link in this comment: #2274 (comment) shows how to create a non-contrived pydantic core schema for a custom generic container, I think we should put something like this in the docs and ideally link to it in the error message.

(In this PR: #5629 I implement similar "error for custom generic containers" logic specifically for subtypes of Sequence)

@dmontagu
Copy link
Contributor Author

dmontagu commented May 8, 2023

Added @adriangb since I think he's looking into changes to how we do subclasses/generic containers more generally. @adriangb I think you can close this issue if you implement something that overrules any discussion above / etc.

@adriangb
Copy link
Member

It's a bit unclear to me what the original issue is asking for. I see two different things going on:

  • Generic containers: we fully support these, at least for the common standard library types dict, set, Counter, etc.
  • Subclasses of known types: these are supported, you just need to implement __get_pydantic_core_schema__ and you'll get an error telling you to do so if you don't

So this issue is resolved unless I'm missing something.

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

4 participants