-
Notifications
You must be signed in to change notification settings - Fork 212
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
Handle annotated indexes #762
Conversation
@dataclass(frozen=True) | ||
class IndexedAnnotation: | ||
_indexed: Tuple[int, Dict[str, Any]] | ||
|
||
|
||
def Indexed(typ=None, index_type=ASCENDING, **kwargs): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we don't want to keep the same interface for both ways of defining indexes, we could just have the users instantiate IndexedAnnotation
when using Annotated
. Though in this case I would clean up the interface for this class and probably the name for a better one.
Maybe we could even add a deprecation warning to Indexed
if we intend to prefer the Annotated
route.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe the name could be IndexedField
or IdxField
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My 2¢:
-1 on introducing a new name, Indexed
is fine.
+1 for emitting a deprecation warning if typ is not None
(at least for Python 3.8+/Pydantic v2; not sure if Annotated
is supported for Pydantic v1).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In pydantic v1, annotated is supported. It is even used for discriminating unions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we don't want to keep the same interface for both ways of defining indexes
It is better to keep both interfaces. Otherwise this breaking change will affect too many users.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I meant keeping the old one, but making a different one for the new way. Anyways, that's your call, I'm glad you liked it!
Thank you for the PR! Merged. It will be published tomorrow |
Is this applied to Bunnet as well? |
This PR aims to introduce a way to declare indexed fields using python's
Annotated
, extending the current use ofIndexed
. It implements the syntax proposed in #638.It also supports the use of
UUID
andEmailStr
when using Pydantic 2 andAnnotated
. Kinda solving #701, though I couldn't reproduce the issue withPydanticObjectId
.