-
Notifications
You must be signed in to change notification settings - Fork 53
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
Feature: Using pydantic
models as parameters.
#55
Comments
Generally seems like a good idea 👍 |
Using dataclasses instead of pydantic in a project can make it less dependent on third-party packages for data validation and settings management. For example, in my own Python client for SurrealDB called SurrealPy, I am using dataclasses instead of pydantic. This not only simplifies the project but also makes it more robust by reducing the number of external dependencies. You can check out the code on this link: https://github.com/Vulnware/SurrealPy/blob/Dev3.9/surrealpy/ws/models.py import dataclasses
from typing import Any, Optional, Union
__all__ = ("SurrealRequest","SurrealResponse","LoginParams")
@dataclasses.dataclass(frozen=True)
class LoginParams:
"""
A class used to represent SurrealDB login parameters.
...
Attributes
----------
username: str
The username of the user. alias of field is user
password: str
The password of the user. alias of field is pass
Methods
-------
to_dict() -> dict[str, Any]
Convert the SurrealDB login parameters to a dict with alias as keys
"""
username: str = dataclasses.field(metadata={"alias": "user"})
password: str = dataclasses.field(metadata={"alias": "pass"})
def __post_init__(self):
if not self.username:
raise ValueError("username is required")
if not self.password:
raise ValueError("password is required")
def to_dict(self) -> dict[str, Any]:
return {
dataclasses.fields(self)[i].metadata["alias"]: getattr(
self, dataclasses.fields(self)[i].name
)
for i in range(len(dataclasses.fields(self)))
}
@dataclasses.dataclass(frozen=True)
class SurrealRequest:
id: str
method: str
params: tuple[Any]
@dataclasses.dataclass(frozen=True)
class SurrealResponse:
result: Union[dict, list[Any]]
id: Optional[str] = None |
Ah, good suggestion. I opted for |
Yeah, you're right about it. But in my opinion, the official package of surrealDB for Python must be light-weight and use less dependency. |
Thanks for the suggestion @bilinenkisi, we are definitely aiming for it to be light-weight and use as few dependencies as practical. Currently, the only external dependencies we have in the code are In your opinion do you think that is too much? 🤔 My thinking so far has been that |
While |
I seem to have made a mistake. The current version of I think the developer experience is great. It also used if other popular packages like |
Yes, I also found out after a search the new core will be Rust based. And as you mentioned many popular packages use |
there is only one question (in my opinion) is needed to answer, will we keep using the original |
I am not sure what you mean, sorry. Do you have an example? |
Of course. For example like this, will we be using Line 718 in c16b0ef
|
Ah, good spot! Yes, we should make that change if sticking with |
Yeah, I think it's worth testing out the performance/compatibility of using the pydantic json 🤔 |
I am happy to throw together a PR for this feature with what we have discussed here. Along with some unit tests 😉 |
Of course 😆 |
That would definitely allow us to compare 🙌 |
@AlexFrid @Ce11an @bilinenkisi Any progress on this? Pydantic is becoming the industry standard for defining data models. Pydantic v2 is made with Rust and has been out for a while now |
Is your feature request related to a problem?
Using the
signin
method is difficult on theSurreal
websockets class. Different key and value pairs are needed for each sign in type. For example, root sign in requires a user and pass, whereas scope sign in requires a user, pass, database, namespace, scope, and any extra parameters. Using an untyped dictionary does not inform the developer what is required to sign in.Describe the solution
To be more inline with the Rust client, we can create "credential" models using
pydantic
. Depending on the type of credentials the user needs, they will know exactly what parameters are needed.For example:
The
signin
method would on theSurreal
class would look something like:Using the method:
Alternative methods
Potentially could use a
TypedDict
to replicate this behaviour instead. However,pydantic
will allow us to create stricter types overall.SurrealDB version
surreal 1.0.0-beta.8+20220930.c246533 for macos on x86_64
surrealdb.py version
surrealdb.py 0.30 for macOS on aarch64 using Python 3.9.1
Contact Details
[email protected]
Is there an existing issue for this?
Code of Conduct
The text was updated successfully, but these errors were encountered: