Skip to content

Commit

Permalink
chore: python 3.9 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Goldziher committed Jul 16, 2024
1 parent eee63ff commit 43e0845
Show file tree
Hide file tree
Showing 22 changed files with 238 additions and 63 deletions.
2 changes: 2 additions & 0 deletions gitmind/caching/base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from abc import ABC, abstractmethod


Expand Down
8 changes: 6 additions & 2 deletions gitmind/caching/file.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from os import PathLike
from __future__ import annotations

from pathlib import Path as SyncPath
from pathlib import PurePath
from typing import Final
from typing import TYPE_CHECKING, Final

from anyio import Path as AsyncPath

from gitmind.caching.base import CacheBase

if TYPE_CHECKING:
from os import PathLike

DEFAULT_FOLDER_NAME: Final[str] = ".git_critic_cache"


Expand Down
20 changes: 13 additions & 7 deletions gitmind/cli/_utils.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
from collections.abc import Callable
from pathlib import Path
from __future__ import annotations

from typing import ( # type: ignore[attr-defined]
TYPE_CHECKING,
Any,
NotRequired,
ParamSpec,
TypedDict,
TypeVar,
_LiteralGenericAlias,
cast,
)

from pydantic import ValidationError
from pygit2 import Repository
from rich_click import Choice, Context, UsageError, echo, option
from typing_extensions import NotRequired, ParamSpec

from gitmind.config import GitMindSettings
from gitmind.prompts.describe_commit import CommitDescriptionResult
from gitmind.utils.repository import get_or_clone_repository

if TYPE_CHECKING:
from collections.abc import Callable
from pathlib import Path

from pygit2 import Repository

from gitmind.prompts.describe_commit import CommitDescriptionResult

T = TypeVar("T")
P = ParamSpec("P")

Expand Down Expand Up @@ -52,7 +58,7 @@ def get_or_set_cli_context(ctx: Context, **kwargs: Any) -> CLIContext:
if ctx.obj is None:
settings = GitMindSettings(**{k: v for k, v in kwargs.items() if v is not None})
# since we use a pydantic validator to ensure this value is not actually None, this cast is safe
target_repo = cast(Path | str, settings.target_repo)
target_repo = cast("Path | str", settings.target_repo)
ctx.obj = CLIContext(settings=settings, repo=get_or_clone_repository(target_repo))
return cast(CLIContext, ctx.obj)
except ValidationError as e:
Expand Down
61 changes: 32 additions & 29 deletions gitmind/config.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from __future__ import annotations

from functools import cached_property
from pathlib import Path
from re import Pattern
from re import compile as compile_regex
from typing import Annotated, Any, Final, Literal
from typing import TYPE_CHECKING, Annotated, Any, Final, Literal

from pydantic import DirectoryPath, Field, SecretStr, field_validator, model_validator
from pydantic_core import Url
from pydantic_settings import (
BaseSettings,
JsonConfigSettingsSource,
Expand All @@ -16,7 +17,10 @@
YamlConfigSettingsSource,
)

from gitmind.llm.base import LLMClient
if TYPE_CHECKING:
from pydantic_core import Url

from gitmind.llm.base import LLMClient

CONFIG_FILE_NAME: Final[str] = "gitmind-config"

Expand Down Expand Up @@ -145,29 +149,28 @@ def llm_client(self) -> LLMClient:
Returns:
The LLM client for the provider.
"""
match self.provider_name:
case "azure-openai":
from gitmind.llm.openai_client import OpenAIClient

return OpenAIClient(
api_key=self.provider_api_key.get_secret_value(),
model_name=self.provider_model,
endpoint_url=self.provider_endpoint_url, # type: ignore[arg-type]
deployment_id=self.provider_deployment_id,
)
case "groq":
from gitmind.llm.groq_client import GroqClient

return GroqClient(
api_key=self.provider_api_key.get_secret_value(),
model_name=self.provider_model,
endpoint_url=self.provider_endpoint_url, # type: ignore[arg-type]
)
case "openai":
from gitmind.llm.openai_client import OpenAIClient

return OpenAIClient(
api_key=self.provider_api_key.get_secret_value(),
model_name=self.provider_model,
endpoint_url=self.provider_endpoint_url, # type: ignore[arg-type]
)
if self.provider_name == "azure-openai":
from gitmind.llm.openai_client import OpenAIClient

return OpenAIClient(
api_key=self.provider_api_key.get_secret_value(),
model_name=self.provider_model,
endpoint_url=self.provider_endpoint_url, # type: ignore[arg-type]
deployment_id=self.provider_deployment_id,
)
if self.provider_name == "groq":
from gitmind.llm.groq_client import GroqClient

return GroqClient(
api_key=self.provider_api_key.get_secret_value(),
model_name=self.provider_model,
endpoint_url=self.provider_endpoint_url, # type: ignore[arg-type]
)

from gitmind.llm.openai_client import OpenAIClient

return OpenAIClient(
api_key=self.provider_api_key.get_secret_value(),
model_name=self.provider_model,
endpoint_url=self.provider_endpoint_url, # type: ignore[arg-type]
)
2 changes: 2 additions & 0 deletions gitmind/llm/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Base classes for LLM clients."""

from __future__ import annotations

from abc import ABC, abstractmethod
from typing import Any, Literal

Expand Down
4 changes: 3 additions & 1 deletion gitmind/llm/groq_client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Any, cast

from gitmind.exceptions import EmptyContentError, LLMClientError, MissingDependencyError
Expand Down Expand Up @@ -37,7 +39,7 @@ class GroqClient(LLMClient):
**kwargs: Additional client options.
"""

_client: "AsyncClient"
_client: AsyncClient
"""The Groq client instance."""
_model: str
"""The model to use for generating completions."""
Expand Down
10 changes: 7 additions & 3 deletions gitmind/llm/openai_client.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from typing import TYPE_CHECKING, Any, Union, cast
from __future__ import annotations

from typing import TYPE_CHECKING, Any, cast

from gitmind.exceptions import EmptyContentError, LLMClientError, MissingDependencyError
from gitmind.llm.base import LLMClient, MessageDefinition, MessageRole, ToolDefinition

if TYPE_CHECKING:
from openai.types import ChatModel

try:
from openai import NOT_GIVEN, OpenAIError
from openai.types import ChatModel
from openai.types.chat import (
ChatCompletionMessageParam,
ChatCompletionSystemMessageParam,
Expand Down Expand Up @@ -39,7 +43,7 @@ class OpenAIClient(LLMClient):
**kwargs: Additional keyword arguments.
"""

_client: Union["AsyncAzureOpenAI", "AsyncClient"]
_client: AsyncAzureOpenAI | AsyncClient
"""The OpenAI client instance."""
_model: ChatModel | str
"""The model to use for generating completions."""
Expand Down
2 changes: 2 additions & 0 deletions gitmind/prompts/base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from abc import ABC, abstractmethod
from typing import Any, Final, Generic, TypeVar

Expand Down
3 changes: 2 additions & 1 deletion gitmind/prompts/describe_commit.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Any, Final, TypedDict, override
from typing import Any, Final, TypedDict

from inflection import titleize
from typing_extensions import override

from gitmind.llm.base import MessageDefinition, ToolDefinition
from gitmind.prompts.base import AbstractPromptHandler
Expand Down
10 changes: 8 additions & 2 deletions gitmind/prompts/grade_commit.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
from typing import Final, Literal, TypedDict, override
from __future__ import annotations

from typing import TYPE_CHECKING, Final, Literal, TypedDict

from typing_extensions import override

from gitmind.llm.base import MessageDefinition, ToolDefinition
from gitmind.prompts.base import AbstractPromptHandler
from gitmind.rules import DEFAULT_GRADING_RULES, Rule
from gitmind.utils.commit import CommitMetadata
from gitmind.utils.serialization import serialize

if TYPE_CHECKING:
from gitmind.utils.commit import CommitMetadata


class CommitGradingResult(TypedDict):
"""DTO for grading results."""
Expand Down
2 changes: 2 additions & 0 deletions gitmind/rules.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from pydantic import BaseModel


Expand Down
11 changes: 8 additions & 3 deletions gitmind/utils/chunking.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
from collections.abc import Generator
from typing import Literal, overload
from __future__ import annotations

from typing import TYPE_CHECKING, Literal, overload

from semantic_text_splitter import CodeSplitter, MarkdownSplitter, TextSplitter
from tree_sitter_language_pack import SupportedLanguage, get_binding
from typing_extensions import TypeAlias

if TYPE_CHECKING:
from collections.abc import Generator

TextualChunkingType = Literal["text", "markdown"]
CodeChunkingType = Literal["code"]

ChunkingType = TextualChunkingType | CodeChunkingType
ChunkingType: TypeAlias = "TextualChunkingType | CodeChunkingType"


@overload
Expand Down
2 changes: 2 additions & 0 deletions gitmind/utils/commit.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import TypedDict

from pygit2 import Commit, Repository
Expand Down
2 changes: 2 additions & 0 deletions gitmind/utils/ref.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import Generic, TypeVar

T = TypeVar("T")
Expand Down
10 changes: 8 additions & 2 deletions gitmind/utils/repository.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
from collections.abc import Callable
from __future__ import annotations

from pathlib import Path
from typing import TYPE_CHECKING

from pygit2 import Remote, Repository
from pygit2 import clone_repository as pygit_clone_repository
from pygit2.callbacks import RemoteCallbacks

if TYPE_CHECKING:
from collections.abc import Callable

from pygit2.callbacks import RemoteCallbacks


def clone_repository(
Expand Down
8 changes: 6 additions & 2 deletions gitmind/utils/serialization.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
"""Serialization related utils."""

from collections.abc import Callable
from __future__ import annotations

from enum import Enum
from inspect import isclass
from typing import Any, TypeVar
from typing import TYPE_CHECKING, Any, TypeVar

from msgspec.json import decode, encode
from pydantic import BaseModel

if TYPE_CHECKING:
from collections.abc import Callable

T = TypeVar("T")


Expand Down
12 changes: 9 additions & 3 deletions gitmind/utils/sync.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
"""Async/sync utils module."""

from __future__ import annotations

from asyncio import run as run_async
from collections.abc import Awaitable, Callable, Coroutine
from functools import partial, wraps
from typing import Any, ParamSpec, TypeVar, cast
from typing import TYPE_CHECKING, TypeVar, cast

from anyio.to_thread import run_sync as anyio_run_sync
from typing_extensions import ParamSpec

if TYPE_CHECKING:
from collections.abc import Awaitable, Callable, Coroutine
from typing import Any

P = ParamSpec("P")
T = TypeVar("T")
C = TypeVar("C", bound=Awaitable[Any] | Coroutine[None, None, Any])
C = TypeVar("C", bound="Awaitable[Any] | Coroutine[None, None, Any]")


async def run_sync(fn: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T:
Expand Down
Loading

0 comments on commit 43e0845

Please sign in to comment.