Skip to content

Commit

Permalink
Merge pull request #77 from evo-company/fix-py.typed
Browse files Browse the repository at this point in the history
fix py.typed
  • Loading branch information
kindermax committed Aug 15, 2022
2 parents b26a685 + a3c141b commit c4d1fbf
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 33 deletions.
8 changes: 6 additions & 2 deletions hiku/endpoint/graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from abc import ABC, abstractmethod
from asyncio import gather
from inspect import isawaitable

from ..engine import Engine
from ..graph import (
Expand All @@ -12,6 +13,7 @@
QueryTransformer,
Node,
)
from ..result import Proxy
from ..validate.query import validate
from ..readers.graphql import (
read_operation,
Expand Down Expand Up @@ -130,6 +132,7 @@ def execute(
) -> t.Dict:
stripped_query = _process_query(graph, op.query)
result = self.engine.execute(graph, stripped_query, ctx)
assert isinstance(result, Proxy)
type_name = _type_names[op.type]
return DenormalizeGraphQL(graph, result, type_name).process(op.query)

Expand Down Expand Up @@ -173,8 +176,9 @@ async def execute(
self, graph: Graph, op: Operation, ctx: t.Optional[t.Dict]
) -> t.Dict:
stripped_query = _process_query(graph, op.query)
# TODO: create AsyncEngine class
result = await self.engine.execute(graph, stripped_query, ctx) # type: ignore # noqa: E501
coro = self.engine.execute(graph, stripped_query, ctx)
assert isawaitable(coro)
result = await coro
type_name = _type_names[op.type]
return DenormalizeGraphQL(graph, result, type_name).process(op.query)

Expand Down
7 changes: 4 additions & 3 deletions hiku/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
Optional,
overload,
DefaultDict,
Awaitable,
)
from functools import partial
from itertools import chain, repeat
Expand All @@ -26,14 +27,14 @@
Concatenate,
)

from .executors.base import SyncAsyncExecutor
from .query import (
Node as QueryNode,
Field as QueryField,
Link as QueryLink,
QueryTransformer,
QueryVisitor
)
from .executors.sync import SyncExecutor
from .graph import (
Link,
Maybe,
Expand Down Expand Up @@ -627,15 +628,15 @@ def __getitem__(self, item: Any) -> Any:

class Engine:

def __init__(self, executor: SyncExecutor) -> None:
def __init__(self, executor: SyncAsyncExecutor) -> None:
self.executor = executor

def execute(
self,
graph: Graph,
query: QueryNode,
ctx: Optional[Dict] = None
) -> Proxy:
) -> Union[Proxy, Awaitable[Proxy]]:
if ctx is None:
ctx = {}
query = InitOptions(graph).visit(query)
Expand Down
4 changes: 2 additions & 2 deletions hiku/executors/asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
cast,
)

from hiku.executors.base import BaseExecutor
from hiku.executors.base import BaseAsyncExecutor
from hiku.result import Proxy


Expand All @@ -29,7 +29,7 @@
)


class AsyncIOExecutor(BaseExecutor):
class AsyncIOExecutor(BaseAsyncExecutor):

def __init__(self, loop: Optional[AbstractEventLoop] = None) -> None:
self._loop = loop or get_event_loop()
Expand Down
4 changes: 2 additions & 2 deletions hiku/executors/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from typing_extensions import ParamSpec

from hiku.executors.base import BaseExecutor
from hiku.executors.base import BaseSyncExecutor
from hiku.result import Proxy

if TYPE_CHECKING:
Expand All @@ -29,7 +29,7 @@ def result(self) -> T:
return self._result


class SyncExecutor(BaseExecutor):
class SyncExecutor(BaseSyncExecutor):

def submit(
self,
Expand Down
4 changes: 2 additions & 2 deletions hiku/executors/threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Any,
)

from hiku.executors.base import BaseExecutor
from hiku.executors.base import BaseSyncExecutor
from hiku.result import Proxy

if TYPE_CHECKING:
Expand All @@ -20,7 +20,7 @@
)


class ThreadsExecutor(BaseExecutor):
class ThreadsExecutor(BaseSyncExecutor):

def __init__(self, pool: Executor):
self._pool = pool
Expand Down
6 changes: 5 additions & 1 deletion hiku/federation/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
)
from asyncio import gather
from contextlib import contextmanager
from inspect import isawaitable
from typing import (
List,
Dict,
Expand Down Expand Up @@ -157,6 +158,7 @@ def execute(
) -> Dict:
stripped_query = _process_query(graph, op.query)
result = self.engine.execute(graph, stripped_query, ctx or {})
assert isinstance(result, Proxy)
return self.postprocess_result(result, graph, op)

def dispatch(self, data: Dict) -> Dict:
Expand All @@ -178,7 +180,9 @@ async def execute(
self, graph: Graph, op: Operation, ctx: Optional[Dict]
) -> Dict:
stripped_query = _process_query(graph, op.query)
result = await self.engine.execute_async(graph, stripped_query, ctx)
coro = self.engine.execute(graph, stripped_query, ctx)
assert isawaitable(coro)
result = await coro
return self.postprocess_result(result, graph, op)

async def dispatch(self, data: Dict) -> Dict:
Expand Down
25 changes: 7 additions & 18 deletions hiku/federation/engine.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import inspect
from collections import defaultdict
from typing import (
Optional,
Dict,
Any,
Awaitable,
Union,
)

from .sdl import print_sdl
Expand All @@ -20,15 +20,15 @@
Index,
ROOT,
)
from ..executors.base import SyncAsyncExecutor
from ..query import (
Node,
Field,
)


class Engine:
# TODO: add type for executor
def __init__(self, executor: Any):
def __init__(self, executor: SyncAsyncExecutor):
self.executor = executor

def execute_service(self, graph: Graph) -> Proxy:
Expand All @@ -42,7 +42,7 @@ def execute_entities(
graph: Graph,
query: Node,
ctx: Dict
) -> Proxy:
) -> Union[Proxy, Awaitable[Proxy]]:
entities_link = query.fields_map['_entities']
query = entities_link.node
representations = entities_link.options['representations']
Expand Down Expand Up @@ -74,7 +74,7 @@ def execute_query(
graph: Graph,
query: Node,
ctx: Dict
) -> Proxy:
) -> Union[Proxy, Awaitable[Proxy]]:
query = InitOptions(graph).visit(query)
queue = Queue(self.executor)
task_set = queue.fork(None)
Expand All @@ -88,7 +88,7 @@ def execute(
graph: Graph,
query: Node,
ctx: Optional[Dict] = None
) -> Proxy:
) -> Union[Proxy, Awaitable[Proxy]]:
if ctx is None:
ctx = {}

Expand All @@ -98,14 +98,3 @@ def execute(
return self.execute_entities(graph, query, ctx)

return self.execute_query(graph, query, ctx)

async def execute_async(
self,
graph: Graph,
query: Node,
ctx: Optional[Dict] = None
) -> Proxy:
result = self.execute(graph, query, ctx)
if inspect.iscoroutine(result):
return await result
return result
8 changes: 7 additions & 1 deletion hiku/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
)
from .directives import DirectiveBase

if t.TYPE_CHECKING:
from .sources.graph import SubGraph
from .sources.graph import BoundExpr


Maybe = const('Maybe')

Expand Down Expand Up @@ -76,7 +80,7 @@ class Option(AbstractOption):
def __init__(
self,
name: str,
type_: GenericMeta,
type_: t.Optional[GenericMeta],
*,
default: t.Any = Nothing,
description: t.Optional[str] = None
Expand Down Expand Up @@ -130,6 +134,8 @@ class AbstractField(AbstractBase, ABC):
RootFieldFunc,
NotRootFieldFunc,
NotRootFieldFuncCtx,
'SubGraph',
'BoundExpr'
]


Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion lets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ commands:
bench:
description: Run benchmarks
depends: [_build-tests]
cmd: docker-compose run --rm pytest tests/benchmarks $@
cmd: docker-compose run --rm pytest tests/benchmarks --benchmark-enable $@

flake:
description: Run flake8
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
author_email='vladimir@magamedov.com',
url='https://github.com/evo-company/hiku',
packages=find_packages(exclude=['test*']),
package_data={'hiku': ['py.typed']},
include_package_data=True,
license='BSD-3-Clause',
python_requires='>=3.7',
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ setenv =
commands = mypy --config-file {toxinidir}/mypy.ini --show-error-codes {posargs:-p hiku}

[pytest]
addopts = -q --tb=native
addopts = -q --tb=native --benchmark-disable
testpaths = tests
filterwarnings =
once::DeprecationWarning
Expand Down

0 comments on commit c4d1fbf

Please sign in to comment.