Skip to content

Commit

Permalink
Merge pull request #68 from evo-company/typings-hiku.graph
Browse files Browse the repository at this point in the history
[typings] hiku.graph
  • Loading branch information
kindermax committed Jul 29, 2022
2 parents c15b9a1 + 621906c commit 3a73f86
Show file tree
Hide file tree
Showing 10 changed files with 290 additions and 96 deletions.
9 changes: 4 additions & 5 deletions hiku/directives/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from typing import (
Optional,
Union,
TYPE_CHECKING
)

from hiku.graph import (
Field,
Link,
)
if TYPE_CHECKING:
from hiku.graph import Field, Link


class DirectiveBase:
Expand All @@ -24,7 +23,7 @@ def accept(self, visitor):
return visitor.visit_deprecated_directive(self)


def get_deprecated(field: Union[Field, Link]) -> Optional[Deprecated]:
def get_deprecated(field: Union['Field', 'Link']) -> Optional[Deprecated]:
"""Get deprecated directive"""
return next(
(d for d in field.directives if isinstance(d, Deprecated)),
Expand Down
32 changes: 26 additions & 6 deletions hiku/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@
import warnings
import dataclasses

from typing import Any
from typing import (
Any,
TypeVar,
Callable,
cast,
)
from functools import partial
from itertools import chain, repeat
from collections import defaultdict
from collections.abc import Sequence, Mapping, Hashable
from typing_extensions import (
ParamSpec,
Concatenate,
)

from . import query as hiku_query
from .graph import Link, Maybe, One, Many, Nothing, Field
Expand Down Expand Up @@ -326,7 +335,7 @@ def link_result_to_ids(from_list, link_type, result):

class Query(Workflow):

def __init__(self, queue, task_set, graph, query, ctx):
def __init__(self, queue, task_set, graph, query, ctx: 'Context'):
self._queue = queue
self._task_set = task_set
self._graph = graph
Expand Down Expand Up @@ -437,12 +446,23 @@ def _schedule_link(self, node, graph_link, query_link, ids):
return dep


def pass_context(func):
func.__pass_context__ = True
return func
R = TypeVar('R')
P = ParamSpec('P')


def _do_pass_context(func):
def pass_context(
func: Callable[P, R]
) -> Callable[Concatenate['Context', P], R]:
"""Decorator to pass context to a function as a first argument.
Can be used on functions for ``Field`` and ``Link``.
Can not be used on functions with ``@define`` decorator
"""
func.__pass_context__ = True # type: ignore[attr-defined]
return cast(Callable[Concatenate['Context', P], R], func)


def _do_pass_context(func: Callable) -> bool:
return getattr(func, '__pass_context__', False)


Expand Down
2 changes: 1 addition & 1 deletion hiku/federation/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def execute(
self, graph: Graph, op: Operation, ctx: Optional[Dict]
) -> Dict:
stripped_query = _process_query(graph, op.query)
result = self.engine.execute(graph, stripped_query, ctx)
result = self.engine.execute(graph, stripped_query, ctx or {})
return self.postprocess_result(result, graph, op)

def dispatch(self, data: Dict) -> Dict:
Expand Down
4 changes: 2 additions & 2 deletions hiku/federation/sdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
AnyMeta,
FloatMeta,
BooleanMeta,
Record,
RecordMeta,
)


Expand Down Expand Up @@ -111,7 +111,7 @@ def _encode_default_value(value) -> Optional[ast.ValueNode]:


class Exporter(GraphVisitor):
def export_record(self, type_name: str, obj: Record):
def export_record(self, type_name: str, obj: RecordMeta):
def new_field(name: str, type_):
return ast.FieldDefinitionNode(
name=_name(name),
Expand Down

0 comments on commit 3a73f86

Please sign in to comment.