Skip to content

Commit

Permalink
Satisfy ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
TimDiekmann committed Aug 18, 2023
1 parent a06337c commit 90678b9
Show file tree
Hide file tree
Showing 17 changed files with 155 additions and 79 deletions.
8 changes: 6 additions & 2 deletions libs/@local/hash-graph-client/python/graph_client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Client for the HASH API."""
from typing import Literal, TypeAlias, TypeVar
from __future__ import annotations

from typing import TYPE_CHECKING, Literal, TypeAlias, TypeVar

import httpx
from pydantic import BaseModel
from yarl import URL

from graph_client.models import (
CreateDataTypeRequest,
Expand Down Expand Up @@ -32,6 +33,9 @@
UpdatePropertyTypeRequest,
)

if TYPE_CHECKING:
from yarl import URL

T = TypeVar("T", bound=BaseModel)

QueryToken: TypeAlias = (
Expand Down
2 changes: 2 additions & 0 deletions libs/@local/hash-graph-sdk/python/graph_sdk/client/_compat.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import TypeVar

from pydantic import BaseModel
Expand Down
27 changes: 16 additions & 11 deletions libs/@local/hash-graph-sdk/python/graph_sdk/client/blocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,27 @@
the problem with that approach however is that users loose the ability to look
at the source code)
"""
from typing import Self, TypeVar
from uuid import UUID
from __future__ import annotations

from graph_client.models import (
MaybeListOfOntologyElementMetadata,
OntologyElementMetadata,
Subgraph,
)
from graph_types import DataTypeSchema, EntityTypeSchema, PropertyTypeSchema
from yarl import URL
from typing import TYPE_CHECKING, Self, TypeVar

from graph_sdk.client.concurrent import HASHClient as ConcurrentHASHClient
from graph_sdk.options import Options
from graph_sdk.query import BaseFilter
from graph_sdk.utils import async_to_sync

if TYPE_CHECKING:
from uuid import UUID

from graph_client.models import (
MaybeListOfOntologyElementMetadata,
OntologyElementMetadata,
Subgraph,
)
from graph_types import DataTypeSchema, EntityTypeSchema, PropertyTypeSchema
from yarl import URL

from graph_sdk.options import Options
from graph_sdk.query import BaseFilter

T = TypeVar("T")


Expand Down
22 changes: 14 additions & 8 deletions libs/@local/hash-graph-sdk/python/graph_sdk/client/concurrent.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Concurrent (async) client for the HASH API."""
from collections.abc import Generator
from __future__ import annotations

from contextlib import contextmanager
from typing import Self, TypeVar
from uuid import UUID
from typing import TYPE_CHECKING, Self, TypeVar

from graph_client import GraphClient as LowLevelClient
from graph_client.models import (
Expand Down Expand Up @@ -32,13 +32,19 @@
UpdatePropertyTypeRequest,
VersionedURL,
)
from graph_types import DataTypeSchema, EntityTypeSchema, PropertyTypeSchema
from pydantic_core._pydantic_core import Url
from yarl import URL

from graph_sdk.client._compat import recast
from graph_sdk.options import Options
from graph_sdk.query import BaseFilter

if TYPE_CHECKING:
from collections.abc import Generator
from uuid import UUID

from graph_types import DataTypeSchema, EntityTypeSchema, PropertyTypeSchema
from yarl import URL

from graph_sdk.options import Options
from graph_sdk.query import BaseFilter

T = TypeVar("T")

Expand All @@ -53,7 +59,7 @@ def assert_not_none(value: T | None) -> T:


@contextmanager
def with_actor(client: "HASHClient", actor: UUID) -> Generator[None, None, None]:
def with_actor(client: HASHClient, actor: UUID) -> Generator[None, None, None]:
"""Context manager for setting the actor on the client."""
old_actor = client.actor
client.actor = actor
Expand Down
8 changes: 6 additions & 2 deletions libs/@local/hash-graph-sdk/python/graph_sdk/filter/base.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
"""Base and generic classes for query paths."""
from __future__ import annotations

from abc import ABC
from typing import Generic, Self, TypeVar
from typing import TYPE_CHECKING, Generic, Self, TypeVar

from graph_client import QueryToken
from graph_client.models import Selector

from graph_sdk.query import Path

if TYPE_CHECKING:
from graph_client import QueryToken


class AbstractQueryPath(ABC):
"""Path definition shared across different query paths."""
Expand Down
14 changes: 9 additions & 5 deletions libs/@local/hash-graph-sdk/python/graph_sdk/options.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Ergonomic API to configure options for structural queries."""
from datetime import datetime
from typing import Protocol
from __future__ import annotations

from typing import TYPE_CHECKING, Protocol

from graph_client.models import (
DecisionTime,
Expand Down Expand Up @@ -28,6 +29,9 @@
TemporalBound as FFITemporalBound,
)

if TYPE_CHECKING:
from datetime import datetime


class ToLimitedTemporalBound(Protocol):
"""Convert to a limited temporal bound.
Expand Down Expand Up @@ -71,17 +75,17 @@ class TemporalBound:
"""

@classmethod
def unbounded(cls) -> "UnboundedTemporalBound":
def unbounded(cls) -> UnboundedTemporalBound:
"""Return an unbounded interval."""
return UnboundedTemporalBound()

@classmethod
def inclusive(cls, time: datetime) -> "InclusiveTemporalBound":
def inclusive(cls, time: datetime) -> InclusiveTemporalBound:
"""Return an inclusive interval."""
return InclusiveTemporalBound(time)

@classmethod
def exclusive(cls, time: datetime) -> "ExclusiveTemporalBound":
def exclusive(cls, time: datetime) -> ExclusiveTemporalBound:
"""Return an exclusive interval."""
return ExclusiveTemporalBound(time)

Expand Down
38 changes: 26 additions & 12 deletions libs/@local/hash-graph-sdk/python/graph_sdk/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,21 @@
To create a filter that will always match use `BaseFilter.always()`.
Use `BaseFilter.never()` to create a filter that will never match.
"""
from __future__ import annotations

from abc import ABC, abstractmethod
from collections.abc import Sequence
from enum import Enum
from typing import Any, Generic, Never, Protocol, Self, TypeVar, assert_never
from typing import (
TYPE_CHECKING,
Any,
Generic,
Never,
Protocol,
Self,
TypeVar,
assert_never,
)

from graph_client import QueryToken
from graph_client.models import (
AllFilter,
AnyFilter,
Expand All @@ -34,7 +43,12 @@
PathExpression,
StartsWithFilter,
)
from pydantic import BaseModel

if TYPE_CHECKING:
from collections.abc import Sequence

from graph_client import QueryToken
from pydantic import BaseModel


class FFIConversionProtocol(Protocol):
Expand Down Expand Up @@ -79,11 +93,11 @@ class BaseFilter(ABC):
def all_(
self,
other: Sequence[F],
) -> "NaryFilter[Self | F]":
) -> NaryFilter[Self | F]:
"""Combines two or more queries with an AND."""
return NaryFilter(NaryOperation.ALL, [self, *other])

def __and__(self, other: F) -> "NaryFilter[Self | F]":
def __and__(self, other: F) -> NaryFilter[Self | F]:
"""Combines two queries with an AND."""
if isinstance(other, BaseFilter):
return self.all_((other,))
Expand All @@ -95,11 +109,11 @@ def __and__(self, other: F) -> "NaryFilter[Self | F]":
def any_(
self,
other: Sequence[F],
) -> "NaryFilter[Self | F]":
) -> NaryFilter[Self | F]:
"""Combines two or more queries with an OR."""
return NaryFilter(NaryOperation.ANY, [self, *other])

def __or__(self, other: F) -> "NaryFilter[Self | F]":
def __or__(self, other: F) -> NaryFilter[Self | F]:
"""Combines two queries with an OR."""
if isinstance(other, BaseFilter):
return self.any_((other,))
Expand All @@ -108,16 +122,16 @@ def __or__(self, other: F) -> "NaryFilter[Self | F]":
msg = f"unsupported operand type(s) for |: '{type(self)}' and '{type(other)}'"
raise TypeError(msg)

def not_(self) -> "UnaryFilter[Self]":
def not_(self) -> UnaryFilter[Self]:
"""Negates the query."""
return UnaryFilter(UnaryOperation.NOT, self)

def __invert__(self) -> "UnaryFilter[Self]":
def __invert__(self) -> UnaryFilter[Self]:
"""Negates the query."""
return self.not_()

@classmethod
def always(cls) -> "NaryFilter[Never]":
def always(cls) -> NaryFilter[Never]:
"""Returns a query that always returns true.
Warning:
Expand All @@ -131,7 +145,7 @@ def always(cls) -> "NaryFilter[Never]":
return NaryFilter(NaryOperation.ALL, [])

@classmethod
def never(cls) -> "NaryFilter[Never]":
def never(cls) -> NaryFilter[Never]:
"""Returns a query that always returns false."""
return NaryFilter(NaryOperation.ANY, [])

Expand Down
8 changes: 7 additions & 1 deletion libs/@local/hash-graph-sdk/python/graph_sdk/types.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"""Create dynamic types from entity, property, and data types via pydantic."""
from uuid import UUID

from __future__ import annotations

from typing import TYPE_CHECKING

from graph_types import (
DataTypeSchema,
Expand All @@ -21,6 +24,9 @@
filter_latest_ontology_types_from_subgraph,
)

if TYPE_CHECKING:
from uuid import UUID


class TypeAPI:
"""GraphAPI for use with hash-graph-types."""
Expand Down
6 changes: 4 additions & 2 deletions libs/@local/hash-graph-sdk/python/graph_sdk/utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"""Miscellaneous utilities for the SDK."""
from __future__ import annotations

import asyncio
from collections.abc import Awaitable
from typing import TYPE_CHECKING, Any, TypeVar

from graph_client.models import KnowledgeGraphVertex, OntologyVertex, Subgraph
from pydantic import BaseModel, ValidationError

if TYPE_CHECKING:
from collections.abc import Awaitable
from types import EllipsisType

try:
Expand All @@ -27,7 +29,7 @@ def async_to_sync(awaitable: Awaitable[T]) -> T:
Different from `asyncio.run` in that it does not create a new event loop each time.
"""
response: T | "EllipsisType" = Missing
response: T | EllipsisType = Missing

async def run_and_capture() -> None:
nonlocal response
Expand Down
10 changes: 7 additions & 3 deletions libs/@local/hash-graph-types/graph_types/_annotations/const.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from typing import Annotated, Any, TypeVar, cast, overload
from __future__ import annotations

from typing import TYPE_CHECKING, Annotated, Any, TypeVar, cast, overload

from pydantic import GetCoreSchemaHandler, GetJsonSchemaHandler
from pydantic.json_schema import JsonSchemaValue
from pydantic_core import CoreSchema, core_schema

if TYPE_CHECKING:
from pydantic import GetCoreSchemaHandler, GetJsonSchemaHandler
from pydantic.json_schema import JsonSchemaValue

T = TypeVar("T")


Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from typing import Annotated, Any, TypeVar, cast
from __future__ import annotations

from typing import TYPE_CHECKING, Annotated, Any, TypeVar, cast

from pydantic import GetCoreSchemaHandler
from pydantic_core import CoreSchema, core_schema

if TYPE_CHECKING:
from pydantic import GetCoreSchemaHandler

T = TypeVar("T")


Expand Down
8 changes: 6 additions & 2 deletions libs/@local/hash-graph-types/graph_types/_cache.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from __future__ import annotations

from asyncio import Event
from collections.abc import Awaitable, Callable
from typing import Generic, TypeVar
from typing import TYPE_CHECKING, Generic, TypeVar

if TYPE_CHECKING:
from collections.abc import Awaitable, Callable

T = TypeVar("T")

Expand Down
Loading

0 comments on commit 90678b9

Please sign in to comment.