8000 Fix some typing issues by skovbasa · Pull Request #186 · evo-company/hiku · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix some typing issues #186

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

Merged
merged 1 commit into from
Jun 11, 2025
Merged
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
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.9-slim as base
FROM python:3.9-slim AS base

WORKDIR /work

Expand All @@ -22,7 +22,7 @@ COPY pdm.lock .
RUN pdm export --prod -o requirements-base.txt -f requirements && \
uv pip install --system -r requirements-base.txt --no-deps --no-cache-dir --index-strategy unsafe-best-match

FROM base as dev
FROM base AS dev

RUN pdm export -G dev -G lint -o requirements-dev.txt -f requirements && \
uv pip install --system -r requirements-dev.txt --no-deps --no-cache-dir --index-strategy unsafe-best-match
Expand All @@ -33,12 +33,12 @@ RUN pdm export -G dev -G test -o requirements-test.txt -f requirements && \
uv pip install --system -r requirements-test.txt --no-deps --no-cache-dir --index-strategy unsafe-best-match && \
uv pip install tox tox-pdm

FROM base as docs
FROM base AS docs

RUN pdm export -G dev -G docs -o requirements-docs.txt -f requirements && \
uv pip install --system -r requirements-docs.txt --no-deps --no-cache-dir --index-strategy unsafe-best-match

FROM base as examples
FROM base AS examples

RUN pdm export -G dev -G examples -o requirements-examples.txt -f requirements && \
uv pip install --system -r requirements-examples.txt --no-deps --no-cache-dir --index-strategy unsafe-best-match
78 changes: 28 additions & 50 deletions hiku/engine.py
Original file line number Diff line number Diff line change
@@ -1,81 +1,61 @@
import contextlib
import dataclasses
import inspect
import warnings
import dataclasses

from collections import defaultdict
from collections.abc import Hashable, Mapping, Sequence
from functools import partial
from itertools import chain, repeat
from typing import (
Any,
Generic,
TypeVar,
Callable,
cast,
Iterator,
Tuple,
Union,
DefaultDict,
Dict,
Generic,
Iterator,
List,
Set,
NoReturn,
Optional,
DefaultDict,
Set,
Tuple,
TypeVar,
Union,
overload,
)
from functools import partial
from itertools import chain, repeat
from collections import defaultdict
from collections.abc import Sequence, Mapping, Hashable

from hiku.types import OptionalMeta

from .cache import (
CacheVisitor,
CacheInfo,
CacheSettings,
)
from .compat import Concatenate, ParamSpec
from .cache import CacheInfo, CacheSettings, CacheVisitor
from .compat import ParamSpec
from .context import ExecutionContext
from .executors.base import (
BaseAsyncExecutor,
BaseSyncExecutor,
SyncAsyncExecutor,
)
from .query import (
Fragment,
Node as QueryNode,
Field as QueryField,
Link as QueryLink,
QueryTransformer,
QueryVisitor,
)
from .executors.queue import Queue, SubmitRes, TaskSet, Workflow
from .graph import (
Field,
FieldType,
Graph,
Link,
LinkType,
Many,
Maybe,
MaybeMany,
One,
Many,
Nothing,
Field,
Graph,
Node,
Nothing,
One,
)
from .result import (
Proxy,
Index,
ROOT,
Reference,
)
from .executors.queue import (
Workflow,
Queue,
TaskSet,
SubmitRes,
)
from .query import Field as QueryField
from .query import Fragment
from .query import Link as QueryLink
from .query import Node as QueryNode
from .query import QueryTransformer, QueryVisitor
from .result import ROOT, Index, Proxy, Reference
from .utils import ImmutableDict
from .utils.serialize import serialize


NodePath = Tuple[Optional[str], ...]


Expand Down Expand Up @@ -998,16 +978,14 @@ def store_link_cache() -> None:
P = ParamSpec("P")


def pass_context(
func: Callable[P, R]
) -> Callable[Concatenate["Context", P], R]:
def pass_context(func: Callable[P, R]) -> Callable[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)
return func


def _do_pass_context(func: Callable) -> bool:
Expand Down
25 changes: 9 additions & 16 deletions hiku/expr/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,16 @@
"""

import typing as t

from collections import namedtuple
from functools import wraps
from itertools import chain
from collections import namedtuple

from ..compat import ParamSpec

from ..query import Node as QueryNode, Link, Field, Base as QueryBase
from ..types import (
Record,
Callable,
Any,
GenericMeta,
)

from .nodes import Symbol, Tuple, List, Keyword, Dict, Node

from ..query import Base as QueryBase
from ..query import Field, Link
from ..query import Node as QueryNode
from ..types import Any, Callable, GenericMeta, Record
from .nodes import Dict, Keyword, List, Node, Symbol, Tuple

THIS = "this"

Expand All @@ -37,7 +30,7 @@ def __init__(self, obj: t.Union[Symbol, Tuple]) -> None:
def __repr__(self) -> str:
return repr(self.obj)

def __getattr__(self, name: str) -> "DotHandler":
def __getattr__(self, name: str) -> t.Any:
return DotHandler(Tuple([Symbol("get"), self.obj, Symbol(name)]))


Expand All @@ -46,7 +39,7 @@ def __getattr__(self, name: str) -> "DotHandler":


class _S:
def __getattr__(self, name: str) -> DotHandler:
def __getattr__(self, name: str) -> t.Any:
return DotHandler(Symbol(name))


Expand Down Expand Up @@ -106,7 +99,7 @@ def _query_to_types(

def define(
*types: GenericMeta, **kwargs: str
) -> t.Callable[[t.Callable[P, T]], t.Callable[P, _Func]]:
) -> t.Callable[[t.Callable[P, T]], t.Callable[P, t.Any]]:
"""Annotates function arguments with types.

These annotations are used to type-check expressions and to analyze,
Expand Down
22 changes: 10 additions & 12 deletions hiku/federation/graph.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import typing as t
from collections import defaultdict
from inspect import isawaitable
from hiku.compat import TypeAlias

from hiku.federation.version import DEFAULT_FEDERATION_VERSION
from hiku.fede 9E12 ration.scalars import _Any, FieldSet, LinkImport
from hiku.federation.utils import get_entity_types

from hiku.engine import pass_context
from hiku.compat import TypeAlias
from hiku.directives import SchemaDirective
from hiku.engine import pass_context
from hiku.enum import BaseEnum
from hiku.scalar import Scalar
from hiku.types import Optional, Record, Sequence, String, TypeRef, UnionRef

from hiku.federation.scalars import FieldSet, LinkImport, _Any
from hiku.federation.utils import get_entity_types
from hiku.federation.version import DEFAULT_FEDERATION_VERSION
from hiku.graph import Field
from hiku.graph import Graph as _Graph
from hiku.graph import (
Field,
Graph as _Graph,
GraphTransformer,
Input,
Interface,
Expand All @@ -25,6 +21,8 @@
Root,
Union,
)
from hiku.scalar import Scalar
from hiku.types import Optional, Record, Sequence, String, TypeRef, UnionRef

RawRef: TypeAlias = t.Any

Expand Down Expand Up @@ -219,7 +217,7 @@ async def wrapper(*args: t.Any, **kwargs: t.Any) -> t.List[t.Dict]:
return Field(
"_service",
TypeRef["_Service"],
_async(service_resolver) if self.is_async else service_resolver, # type: ignore[arg-type] # noqa: E501
_async(service_resolver) if self.is_async else service_resolver,
)


Expand Down
44 changes: 22 additions & 22 deletions hiku/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,43 @@

import dataclasses
import typing as t

from abc import ABC, abstractmethod
from collections import OrderedDict, defaultdict
from enum import Enum
from functools import cached_property, reduce
from itertools import chain
from functools import reduce, cached_property
from collections import OrderedDict, defaultdict
from typing import List

from hiku.enum import BaseEnum
from .scalar import Scalar, ScalarMeta

from .compat import TypeAlias
from .directives import Deprecated, SchemaDirective
from .scalar import Scalar, ScalarMeta
from .types import (
Any,
AnyMeta,
EnumRefMeta,
GenericMeta,
InputRefMeta,
InterfaceRef,
InterfaceRefMeta,
Optional,
OptionalMeta,
Record,
RefMeta,
Sequence,
SequenceMeta,
TypeRef,
Record,
Any,
GenericMeta,
TypeRefMeta,
TypingMeta,
AnyMeta,
UnionRef,
UnionRefMeta,
)
from .utils import (
const,
Const,
)
from .directives import Deprecated, SchemaDirective

from .compat import TypeAlias

from .utils import Const, const

if t.TYPE_CHECKING:
from .sources.graph import SubGraph
from .sources.graph import BoundExpr
from .query import Field as QueryField
from .sources.graph import BoundExpr, SubGraph

# TODO enum ???
Maybe = const("Maybe")
Expand Down Expand Up @@ -103,7 +97,7 @@ class Option(AbstractOption):
def __init__(
self,
name: str,
type_: t.Optional[GenericMeta],
type_: t.Optional[t.Union[GenericMeta, t.Type[Scalar]]],
*,
default: t.Any = Nothing,
description: t.Optional[str] = None,
Expand Down Expand Up @@ -139,20 +133,26 @@ class AbstractField(AbstractBase, ABC):
SyncAsync = t.Union[R, t.Awaitable[R]]

# (fields) -> []
RootFieldFunc = t.Callable[[t.List["Field"]], SyncAsync[List[t.Any]]]
RootFieldFunc = t.Callable[[t.List["QueryField"]], SyncAsync[t.Iterable[t.Any]]]
# (ctx, fields) -> []
RootFieldFuncCtx = t.Callable[
[t.Any, t.List["QueryField"]], SyncAsync[t.Iterable[t.Any]]
]
# (fields, ids) -> [[]]
NotRootFieldFunc = t.Callable[
[t.List["Field"], List[t.Any]], SyncAsync[List[List[t.Any]]]
[t.List["QueryField"], List[t.Any]], SyncAsync[t.Iterable[List[t.Any]]]
]
# (ctx, fields, ids) -> [[]]
NotRootFieldFuncCtx = t.Callable[
[t.Any, t.List["Field"], List[t.Any]], SyncAsync[List[List[t.Any]]]
[t.Any, t.List["QueryField"], List[t.Any]],
SyncAsync[t.Iterable[List[t.Any]]],
]


FieldT = t.Optional[t.Union[GenericMeta, ScalarMeta]]
FieldFunc = t.Union[
RootFieldFunc,
RootFieldFuncCtx,
NotRootFieldFunc,
NotRootFieldFuncCtx,
"SubGraph",
Expand Down
Loading
0