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

Handle annotated indexes #762

Merged
merged 1 commit into from
Nov 5, 2023
Merged

Conversation

MrEarle
Copy link
Contributor

@MrEarle MrEarle commented Oct 30, 2023

This PR aims to introduce a way to declare indexed fields using python's Annotated, extending the current use of Indexed. It implements the syntax proposed in #638.

It also supports the use of UUID and EmailStr when using Pydantic 2 and Annotated. Kinda solving #701, though I couldn't reproduce the issue with PydanticObjectId.

@MrEarle MrEarle changed the title feat: handle annotated indexes Handle annotated indexes Oct 30, 2023
Comment on lines +71 to +76
@dataclass(frozen=True)
class IndexedAnnotation:
_indexed: Tuple[int, Dict[str, Any]]


def Indexed(typ=None, index_type=ASCENDING, **kwargs):
Copy link
Contributor Author

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.

Copy link
Contributor Author

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?

Copy link
Contributor

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).

Copy link
Contributor Author

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

Copy link
Member

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.

Copy link
Contributor Author

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!

@roman-right roman-right merged commit 0add350 into BeanieODM:main Nov 5, 2023
21 checks passed
@roman-right
Copy link
Member

Thank you for the PR! Merged. It will be published tomorrow

@smeggingsmegger
Copy link

Is this applied to Bunnet as well?

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

Successfully merging this pull request may close these issues.

4 participants