Skip to content

Commit

Permalink
Merge pull request #73 from evo-company/add-typings
Browse files Browse the repository at this point in the history
Add typings
  • Loading branch information
kindermax committed Aug 11, 2022
2 parents 463513f + 0aa63bd commit 1173fff
Show file tree
Hide file tree
Showing 37 changed files with 1,668 additions and 625 deletions.
4 changes: 1 addition & 3 deletions examples/graphql_aiohttp.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
from typing import Dict

from aiohttp import web

Expand All @@ -9,7 +8,6 @@
Integer,
TypeRef,
Record,
RecordMeta,
)
from hiku.graph import Graph, Root, Field, Option
from hiku.engine import Engine
Expand All @@ -32,7 +30,7 @@ async def action_func(fields):
return results


DATA_TYPES: Dict[str, RecordMeta] = {
DATA_TYPES = {
'Point': Record[{
'x': Integer,
'y': Integer,
Expand Down
6 changes: 5 additions & 1 deletion examples/graphql_federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
Sequence,
)
from hiku.executors.sync import SyncExecutor
from hiku.utils import listify

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -54,18 +55,21 @@ def find_all_by_id(id_, collection, key='id'):
}


@listify
def cart_resolver(fields, ids):
for cart_id in ids:
cart = get_by_id(cart_id, data['carts'])
yield [cart[f.name] for f in fields]


@listify
def cart_item_resolver(fields, ids):
for item_id in ids:
item = get_by_id(item_id, data['cart_items'])
yield [item[f.name] for f in fields]


@listify
def link_cart_items(cart_ids):
for cart_id in cart_ids:
yield [item['id'] for item
Expand Down Expand Up @@ -100,7 +104,7 @@ def direct_link(ids):
Field('id', Integer, cart_item_resolver),
Field('cart_id', Integer, cart_item_resolver),
Field('name', String, cart_item_resolver),
Field('photo', Optional[String], lambda: None, options=[
Field('photo', Optional[String], cart_item_resolver, options=[
Option('width', Integer),
Option('height', Integer),
]),
Expand Down
6 changes: 3 additions & 3 deletions hiku/compat.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import sys
import ast as _ast

from typing import Any

PY38 = sys.version_info >= (3, 8)


class _AST:

def __getattr__(self, name):
def __getattr__(self, name: str) -> Any:
return getattr(_ast, name)

if PY38:
arguments = _ast.arguments
else:
@staticmethod
def arguments(_, args, vararg, kwonlyargs, kw_defaults, kwarg, defaults): # noqa
def arguments(_, args, vararg, kwonlyargs, kw_defaults, kwarg, defaults): # type: ignore[no-untyped-def] # noqa
return _ast.arguments(args, vararg, kwonlyargs, kw_defaults, kwarg, defaults) # noqa


Expand Down
39 changes: 31 additions & 8 deletions hiku/denormalize/base.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,49 @@
from collections import deque
from typing import (
Deque,
Dict,
)

from ..query import QueryVisitor, Link, Field
from ..types import TypeRefMeta, OptionalMeta, SequenceMeta, get_type
from ..graph import Graph
from ..query import (
QueryVisitor,
Link,
Field,
Node,
)
from ..result import Proxy
from ..types import (
TypeRefMeta,
OptionalMeta,
SequenceMeta,
get_type,
RecordMeta,
)


class Denormalize(QueryVisitor):

def __init__(self, graph, result):
def __init__(
self,
graph: Graph,
result: Proxy
) -> None:
self._types = graph.__types__
self._result = result
self._type = deque([self._types['__root__']])
self._type: Deque[RecordMeta] = deque([self._types['__root__']])
self._data = deque([result])
self._res = deque()
self._res: Deque = deque()

def process(self, query):
def process(self, query: Node) -> Dict:
assert not self._res, self._res
self._res.append({})
self.visit(query)
return self._res.pop()

def visit_field(self, obj: Field):
def visit_field(self, obj: Field) -> None:
self._res[-1][obj.result_key] = self._data[-1][obj.result_key]

def visit_link(self, obj: Link):
def visit_link(self, obj: Link) -> None:
type_ = self._type[-1].__field_types__[obj.name]
if isinstance(type_, TypeRefMeta):
self._type.append(get_type(self._types, type_))
Expand All @@ -33,6 +54,7 @@ def visit_link(self, obj: Link):
self._res[-1][obj.result_key] = self._res.pop()
self._type.pop()
elif isinstance(type_, SequenceMeta):
assert isinstance(type_.__item_type__, TypeRefMeta)
self._type.append(get_type(self._types, type_.__item_type__))
items = []
for item in self._data[-1][obj.result_key]:
Expand All @@ -47,6 +69,7 @@ def visit_link(self, obj: Link):
if self._data[-1][obj.result_key] is None:
self._res[-1][obj.result_key] = None
else:
assert isinstance(type_.__type__, TypeRefMeta)
self._type.append(get_type(self._types, type_.__type__))
self._res.append({})
self._data.append(self._data[-1][obj.result_key])
Expand Down
13 changes: 10 additions & 3 deletions hiku/denormalize/graphql.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from collections import deque

from ..graph import Graph
from ..query import Field, Link
from ..result import Proxy
from ..types import (
TypeRefMeta,
SequenceMeta,
Expand All @@ -13,17 +15,22 @@

class DenormalizeGraphQL(Denormalize):

def __init__(self, graph, result, root_type_name):
def __init__(
self,
graph: Graph,
result: Proxy,
root_type_name: str
) -> None:
super().__init__(graph, result)
self._type_name = deque([root_type_name])

def visit_field(self, obj: Field):
def visit_field(self, obj: Field) -> None:
if obj.name == '__typename':
self._res[-1][obj.result_key] = self._type_name[-1]
else:
super().visit_field(obj)

def visit_link(self, obj: Link):
def visit_link(self, obj: Link) -> None:
type_ = self._type[-1].__field_types__[obj.name]
type_ref: GenericMeta
if isinstance(type_, TypeRefMeta):
Expand Down
5 changes: 3 additions & 2 deletions hiku/directives/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from typing import (
Optional,
Union,
TYPE_CHECKING
TYPE_CHECKING,
Any,
)

if TYPE_CHECKING:
Expand All @@ -19,7 +20,7 @@ class Deprecated(DirectiveBase):
def __init__(self, reason: Optional[str] = None):
self.reason = reason

def accept(self, visitor):
def accept(self, visitor: Any) -> Any:
return visitor.visit_deprecated_directive(self)


Expand Down

0 comments on commit 1173fff

Please sign in to comment.