Skip to content

Commit d0a320f

Browse files
Resolve error and fix remaining type hint violations
1 parent 32808dd commit d0a320f

File tree

12 files changed

+62
-24
lines changed

12 files changed

+62
-24
lines changed

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
black
22
click>=8.0.0
33
djlint
4+
dotenv
45
fastapi-analytics
56
fastapi[standard]
67
pre-commit
78
pytest
89
pytest-asyncio
910
slowapi
11+
starlette
1012
tiktoken
1113
uvicorn

src/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
MAX_DISPLAY_SIZE = 300000
1+
MAX_DISPLAY_SIZE = 300_000
22
TMP_BASE_PATH = "../tmp"
33

44
EXAMPLE_REPOS = [

src/gitingest/cli.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
from typing import Optional, Tuple
23

34
import click
45

src/gitingest/clone.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import asyncio
2-
from typing import Dict, Tuple
2+
from typing import Any, Dict, Tuple
33

44
from gitingest.utils import async_timeout
55

src/gitingest/ingest.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import asyncio
2+
import inspect
23
import shutil
34
from pathlib import Path
4-
from typing import List, Union
5+
from typing import List, Optional, Tuple, Union
56

67
from gitingest.clone import clone_repo
78
from gitingest.ingest_from_query import ingest_from_query
@@ -16,9 +17,19 @@ def ingest(
1617
output: Optional[str] = None,
1718
) -> Tuple[str, str, str]:
1819
try:
19-
query = parse_query(source, max_file_size, False, include_patterns, exclude_patterns)
20+
query = parse_query(
21+
source=source,
22+
max_file_size=max_file_size,
23+
from_web=False,
24+
include_patterns=include_patterns,
25+
ignore_patterns=exclude_patterns,
26+
)
2027
if query['url']:
21-
asyncio.run(clone_repo(query))
28+
clone_result = clone_repo(query)
29+
if inspect.iscoroutine(clone_result):
30+
asyncio.run(clone_result)
31+
else:
32+
raise TypeError("clone_repo did not return a coroutine as expected.")
2233

2334
summary, tree, content = ingest_from_query(query)
2435

src/gitingest/ingest_from_query.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
from fnmatch import fnmatch
3-
from typing import Dict, List, Set
3+
from typing import Any, Dict, List, Optional, Set, Tuple
44

55
import tiktoken
66

@@ -335,10 +335,10 @@ def generate_token_string(context_string: str) -> Optional[str]:
335335
print(e)
336336
return None
337337

338-
if total_tokens > 1000000:
339-
formatted_tokens = f"{total_tokens/1000000:.1f}M"
340-
elif total_tokens > 1000:
341-
formatted_tokens = f"{total_tokens/1000:.1f}k"
338+
if total_tokens > 1_000_000:
339+
formatted_tokens = f"{total_tokens / 1_000_000:.1f}M"
340+
elif total_tokens > 1_000:
341+
formatted_tokens = f"{total_tokens / 1_000:.1f}k"
342342
else:
343343
formatted_tokens = f"{total_tokens}"
344344

@@ -383,6 +383,8 @@ def ingest_single_file(path: str, query: Dict[str, Any]) -> Tuple[str, str, str]
383383

384384
def ingest_directory(path: str, query: Dict[str, Any]) -> Tuple[str, str, str]:
385385
nodes = scan_directory(path=path, query=query)
386+
if not nodes:
387+
raise ValueError(f"No files found in {path}")
386388
files = extract_files_content(query=query, node=nodes, max_file_size=query['max_file_size'])
387389
summary = create_summary_string(query, nodes, files)
388390
tree = "Directory structure:\n" + create_tree_structure(query, nodes)

src/gitingest/parse_query.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
TMP_BASE_PATH = "../tmp"
88

99

10-
def parse_url(url: str) -> Dict[str, Optional[str]]:
10+
def parse_url(url: str) -> Dict[str, Any]:
1111
parsed = {
1212
"user_name": None,
1313
"repo_name": None,
@@ -97,8 +97,8 @@ def parse_query(
9797
source: str,
9898
max_file_size: int,
9999
from_web: bool,
100-
include_patterns: Union[List[str], str] = None,
101-
ignore_patterns: Union[List[str], str] = None,
100+
include_patterns: Optional[Union[List[str], str]] = None,
101+
ignore_patterns: Optional[Union[List[str], str]] = None,
102102
) -> Dict[str, Any]:
103103
if from_web:
104104
query = parse_url(source)
@@ -107,6 +107,7 @@ def parse_query(
107107
query = parse_url(source)
108108
else:
109109
query = parse_path(source)
110+
110111
query['max_file_size'] = max_file_size
111112

112113
if ignore_patterns and ignore_patterns != "":

src/gitingest/tests/test_clone.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from unittest.mock import AsyncMock, patch
22

33
import pytest
4-
from clone import check_repo_exists, clone_repo
4+
5+
from gitingest.clone import check_repo_exists, clone_repo
56

67

78
@pytest.mark.asyncio

src/gitingest/tests/test_ingest.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ def temp_directory(tmp_path: Path) -> Path:
7575

7676
def test_scan_directory(temp_directory: Path, sample_query: Dict[str, Any]) -> None:
7777
result = scan_directory(str(temp_directory), query=sample_query)
78+
if result is None:
79+
assert False, "Result is None"
7880

7981
assert result['type'] == 'directory'
8082
assert result['file_count'] == 8 # All .txt and .py files
@@ -84,6 +86,8 @@ def test_scan_directory(temp_directory: Path, sample_query: Dict[str, Any]) -> N
8486

8587
def test_extract_files_content(temp_directory: Path, sample_query: Dict[str, Any]) -> None:
8688
nodes = scan_directory(str(temp_directory), query=sample_query)
89+
if nodes is None:
90+
assert False, "Nodes is None"
8791
files = extract_files_content(query=sample_query, node=nodes, max_file_size=1_000_000)
8892
assert len(files) == 8 # All .txt and .py files
8993

src/gitingest/utils.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
## Async Timeout decorator
22
import asyncio
33
import functools
4-
from typing import Awaitable, Callable, TypeVar
4+
from typing import Awaitable, Callable, ParamSpec, TypeVar
55

66
T = TypeVar("T")
7+
P = ParamSpec("P")
78

89

910
class AsyncTimeoutError(Exception):
@@ -12,14 +13,14 @@ class AsyncTimeoutError(Exception):
1213
pass
1314

1415

15-
def async_timeout(seconds: int = 10) -> Callable[[Callable[..., Awaitable[T]]], Callable[..., Awaitable[T]]]:
16-
def decorator(func: Callable[..., T]) -> Callable[..., T]:
16+
def async_timeout(seconds: int = 10) -> Callable[[Callable[P, Awaitable[T]]], Callable[P, Awaitable[T]]]:
17+
def decorator(func: Callable[P, Awaitable[T]]) -> Callable[P, Awaitable[T]]:
1718
@functools.wraps(func)
18-
async def wrapper(*args, **kwargs) -> T:
19+
async def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
1920
try:
2021
return await asyncio.wait_for(func(*args, **kwargs), timeout=seconds)
2122
except asyncio.TimeoutError:
22-
raise AsyncTimeoutError(f"Clone timed out after {seconds} seconds")
23+
raise AsyncTimeoutError(f"Operation timed out after {seconds} seconds")
2324

2425
return wrapper
2526

0 commit comments

Comments
 (0)