Skip to content

Adding generics #343

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion arango/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
__all__ = ["ApiGroup"]

from typing import Callable, Optional, TypeVar
from typing import Any, Callable, Optional, TypeVar

from arango.connection import Connection
from arango.executor import ApiExecutor
Expand Down
13 changes: 8 additions & 5 deletions arango/aql.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__all__ = ["AQL", "AQLQueryCache"]

from numbers import Number
from typing import MutableMapping, Optional, Sequence, Union
from typing import Generic, MutableMapping, Optional, Sequence, TypeVar, Union

from arango.api import ApiGroup
from arango.connection import Connection
Expand Down Expand Up @@ -145,14 +145,17 @@ def response_handler(resp: Response) -> bool:
return self._execute(request, response_handler)


class AQL(ApiGroup):
T = TypeVar("T")


class AQL(ApiGroup, Generic[T]):
"""AQL (ArangoDB Query Language) API wrapper.

:param connection: HTTP connection.
:param executor: API executor.
"""

def __init__(self, connection: Connection, executor: ApiExecutor) -> None:
def __init__(self, connection: Connection[T], executor: ApiExecutor) -> None:
super().__init__(connection, executor)

def __repr__(self) -> str:
Expand All @@ -173,7 +176,7 @@ def explain(
all_plans: bool = False,
max_plans: Optional[int] = None,
opt_rules: Optional[Sequence[str]] = None,
bind_vars: Optional[MutableMapping[str, DataTypes]] = None,
bind_vars: Optional[MutableMapping[str, Union[DataTypes, T]]] = None,
) -> Result[Union[Json, Jsons]]:
"""Inspect the query and return its metadata without executing it.

Expand Down Expand Up @@ -257,7 +260,7 @@ def execute(
count: bool = False,
batch_size: Optional[int] = None,
ttl: Optional[Number] = None,
bind_vars: Optional[MutableMapping[str, DataTypes]] = None,
bind_vars: Optional[MutableMapping[str, Union[DataTypes, T]]] = None,
full_count: Optional[bool] = None,
max_plans: Optional[int] = None,
optimizer_rules: Optional[Sequence[str]] = None,
Expand Down
42 changes: 9 additions & 33 deletions arango/client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
__all__ = ["ArangoClient"]

from json import dumps, loads
from typing import Any, Callable, Optional, Sequence, Union
from typing import Any, Generic, Optional, Sequence, TypeVar, Union

import importlib_metadata

Expand All @@ -27,33 +26,13 @@
RoundRobinHostResolver,
SingleHostResolver,
)
from arango.serializer import Deserializer, JsonDeserializer, JsonSerializer, Serializer
from arango.typings import DataTypes


def default_serializer(x: Any) -> str:
"""
Default JSON serializer

:param x: A JSON data type object to serialize
:type x: Any
:return: The object serialized as a JSON string
:rtype: str
"""
return dumps(x, separators=(",", ":"))


def default_deserializer(x: str) -> Any:
"""
Default JSON de-serializer

:param x: A JSON string to deserialize
:type x: str
:return: The de-serialized JSON object
:rtype: Any
"""
return loads(x)
T = TypeVar("T")


class ArangoClient:
class ArangoClient(Generic[T]):
"""ArangoDB client.

:param hosts: Host URL or list of URLs (coordinators in a cluster).
Expand Down Expand Up @@ -104,8 +83,8 @@ def __init__(
host_resolver: Union[str, HostResolver] = "fallback",
resolver_max_tries: Optional[int] = None,
http_client: Optional[HTTPClient] = None,
serializer: Callable[..., str] = default_serializer,
deserializer: Callable[[str], Any] = default_deserializer,
serializer: Serializer[T] = JsonSerializer(),
deserializer: Deserializer[DataTypes] = JsonDeserializer(),
verify_override: Union[bool, str, None] = None,
request_timeout: Union[int, float, None] = DEFAULT_REQUEST_TIMEOUT,
request_compression: Optional[RequestCompression] = None,
Expand Down Expand Up @@ -199,8 +178,7 @@ def db(
auth_method: str = "basic",
user_token: Optional[str] = None,
superuser_token: Optional[str] = None,
verify_certificate: bool = True,
) -> StandardDatabase:
) -> StandardDatabase[T]:
"""Connect to an ArangoDB database and return the database API wrapper.

:param name: Database name.
Expand Down Expand Up @@ -228,14 +206,12 @@ def db(
are ignored. This token is not refreshed automatically. Token
expiry will not be checked.
:type superuser_token: str
:param verify_certificate: Verify TLS certificates.
:type verify_certificate: bool
:return: Standard database API wrapper.
:rtype: arango.database.StandardDatabase
:raise arango.exceptions.ServerConnectionError: If **verify** was set
to True and the connection fails.
"""
connection: Connection
connection: Connection[T]

if superuser_token is not None:
connection = JwtSuperuserConnection(
Expand Down
Loading
Loading