We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
You can continue the conversation there. Go to discussion →
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
After a brainstorming session with @jtyoung84 , we came up with this idea:
from pydantic import BaseModel, RootModel, Field from typing import Annotated, List, Literal, Union, ClassVar, Dict class _Base(BaseModel): key: str a: int def __hash__(self) -> int: return hash(self.key) class _Foo(_Base): key: Literal["Foo"] = "Foo" a: int = 1 class _Bar(_Base): key: Literal["Bar"] = "Bar" a: int = 2 class MyClass(RootModel): root: Annotated[Union[_Foo, _Bar], Field(..., discriminator="key")] ONE_OF: ClassVar[Annotated[Union[_Foo, _Bar], Field(..., discriminator="key")]] = Annotated[Union[_Foo, _Bar], Field(..., discriminator="key")] Foo: ClassVar[_Foo] = _Foo() Bar: ClassVar[_Bar] = _Bar() class Container(BaseModel): prop_to_be_deprecated: MyClass.ONE_OF prop: MyClass dict_of_prop: Dict[MyClass.ONE_OF, int] # This doesn't work :( new = Container( prop_to_be_deprecated=MyClass.Bar, prop=MyClass.Foo, dict_of_prop={MyClass.Foo: 1}) # However this works :D a = {} a[_Foo()] = 1 a[_Bar()] = 2 print(a[_Bar()])
This would solve a few problems. Namely:
The text was updated successfully, but these errors were encountered:
Was this intended to be closed? I don't think that any of the points in the issue were address by the referenced PR (?)
If you are using the issues as a ticketing system I feel moving this to a discussion feels more appropriate if you don't want to track it.
Sorry, something went wrong.
Nope just a mistake, but yes this should probably be a discussion.
Successfully merging a pull request may close this issue.
After a brainstorming session with @jtyoung84 , we came up with this idea:
This would solve a few problems. Namely:
The text was updated successfully, but these errors were encountered: