Skip to content

Commit 16c8abf

Browse files
Update type hints using pyupgrade --py39-plus and --py310-plus
1 parent 70ff34e commit 16c8abf

File tree

10 files changed

+48
-54
lines changed

10 files changed

+48
-54
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ repos:
4848
hooks:
4949
- id: pyupgrade
5050
description: 'Automatically upgrade syntax for newer versions.'
51-
args: [--py3-plus, --py36-plus, --py38-plus]
51+
args: [--py3-plus, --py36-plus, --py38-plus, --py39-plus, --py310-plus]
5252

5353
- repo: https://github.com/pre-commit/pygrep-hooks
5454
rev: v1.10.0

src/gitingest/cli.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import os
2-
from typing import Optional, Tuple
32

43
import click
54

6-
from gitingest.ignore_patterns import DEFAULT_IGNORE_PATTERNS
75
from gitingest.ingest import ingest
86
from gitingest.ingest_from_query import MAX_FILE_SIZE
97

@@ -24,10 +22,10 @@ def normalize_pattern(pattern: str) -> str:
2422
@click.option('--include-pattern', '-i', multiple=True, help='Patterns to include')
2523
def main(
2624
source: str,
27-
output: Optional[str],
25+
output: str | None,
2826
max_size: int,
29-
exclude_pattern: Tuple[str, ...],
30-
include_pattern: Tuple[str, ...],
27+
exclude_pattern: tuple[str, ...],
28+
include_pattern: tuple[str, ...],
3129
) -> None:
3230
"""Analyze a directory and create a text dump of its contents."""
3331
try:

src/gitingest/clone.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import asyncio
22
from dataclasses import dataclass
3-
from typing import Optional, Tuple
43

54
from gitingest.utils import AsyncTimeoutError, async_timeout
65

@@ -11,8 +10,8 @@
1110
class CloneConfig:
1211
url: str
1312
local_path: str
14-
commit: Optional[str] = None
15-
branch: Optional[str] = None
13+
commit: str | None = None
14+
branch: str | None = None
1615

1716

1817
async def check_repo_exists(url: str) -> bool:
@@ -44,7 +43,7 @@ async def check_repo_exists(url: str) -> bool:
4443
return "HTTP/1.1 404" not in stdout_str and "HTTP/2 404" not in stdout_str
4544

4645

47-
async def run_git_command(*args: str) -> Tuple[bytes, bytes]:
46+
async def run_git_command(*args: str) -> tuple[bytes, bytes]:
4847
"""
4948
Executes a git command asynchronously and captures its output.
5049
@@ -77,7 +76,7 @@ async def run_git_command(*args: str) -> Tuple[bytes, bytes]:
7776

7877

7978
@async_timeout(CLONE_TIMEOUT)
80-
async def clone_repo(config: CloneConfig) -> Tuple[bytes, bytes]:
79+
async def clone_repo(config: CloneConfig) -> tuple[bytes, bytes]:
8180
"""
8281
Clones a repository to a local path based on the provided query parameters.
8382
@@ -107,8 +106,8 @@ async def clone_repo(config: CloneConfig) -> Tuple[bytes, bytes]:
107106
# Extract and validate query parameters
108107
url: str = config.url
109108
local_path: str = config.local_path
110-
commit: Optional[str] = config.commit
111-
branch: Optional[str] = config.branch
109+
commit: str | None = config.commit
110+
branch: str | None = config.branch
112111

113112
if not url:
114113
raise ValueError("The 'url' parameter is required.")

src/gitingest/ignore_patterns.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
from typing import List
2-
3-
DEFAULT_IGNORE_PATTERNS: List[str] = [
1+
DEFAULT_IGNORE_PATTERNS: list[str] = [
42
# Python
53
'*.pyc',
64
'*.pyo',

src/gitingest/ingest.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import inspect
33
import shutil
44
from pathlib import Path
5-
from typing import List, Optional, Tuple, Union
65

76
from gitingest.clone import CloneConfig, clone_repo
87
from gitingest.ingest_from_query import ingest_from_query
@@ -12,10 +11,10 @@
1211
def ingest(
1312
source: str,
1413
max_file_size: int = 10 * 1024 * 1024, # 10 MB
15-
include_patterns: Union[List[str], str, None] = None,
16-
exclude_patterns: Union[List[str], str, None] = None,
17-
output: Optional[str] = None,
18-
) -> Tuple[str, str, str]:
14+
include_patterns: list[str] | str | None = None,
15+
exclude_patterns: list[str] | str | None = None,
16+
output: str | None = None,
17+
) -> tuple[str, str, str]:
1918
try:
2019
query = parse_query(
2120
source=source,

src/gitingest/ingest_from_query.py

Lines changed: 18 additions & 18 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 Any, Dict, List, Optional, Set, Tuple
3+
from typing import Any
44

55
import tiktoken
66

@@ -10,7 +10,7 @@
1010
MAX_TOTAL_SIZE_BYTES = 500 * 1024 * 1024 # 500 MB
1111

1212

13-
def should_include(path: str, base_path: str, include_patterns: List[str]) -> bool:
13+
def should_include(path: str, base_path: str, include_patterns: list[str]) -> bool:
1414
rel_path = path.replace(base_path, "").lstrip(os.sep)
1515
include = False
1616
for pattern in include_patterns:
@@ -19,7 +19,7 @@ def should_include(path: str, base_path: str, include_patterns: List[str]) -> bo
1919
return include
2020

2121

22-
def should_exclude(path: str, base_path: str, ignore_patterns: List[str]) -> bool:
22+
def should_exclude(path: str, base_path: str, ignore_patterns: list[str]) -> bool:
2323
rel_path = path.replace(base_path, "").lstrip(os.sep)
2424
for pattern in ignore_patterns:
2525
if pattern == '':
@@ -60,11 +60,11 @@ def read_file_content(file_path: str) -> str:
6060

6161
def scan_directory(
6262
path: str,
63-
query: Dict[str, Any],
64-
seen_paths: Optional[Set[str]] = None,
63+
query: dict[str, Any],
64+
seen_paths: set[str] | None = None,
6565
depth: int = 0,
66-
stats: Optional[Dict[str, int]] = None,
67-
) -> Optional[Dict[str, Any]]:
66+
stats: dict[str, int] | None = None,
67+
) -> dict[str, Any] | None:
6868
"""Recursively analyzes a directory and its contents with safety limits."""
6969
if seen_paths is None:
7070
seen_paths = set()
@@ -220,11 +220,11 @@ def scan_directory(
220220

221221

222222
def extract_files_content(
223-
query: Dict[str, Any],
224-
node: Dict[str, Any],
223+
query: dict[str, Any],
224+
node: dict[str, Any],
225225
max_file_size: int,
226-
files: Optional[List[Dict[str, Any]]] = None,
227-
) -> List[Dict[str, Any]]:
226+
files: list[dict[str, Any]] | None = None,
227+
) -> list[dict[str, Any]]:
228228
"""Recursively collects all text files with their contents."""
229229
if files is None:
230230
files = []
@@ -248,7 +248,7 @@ def extract_files_content(
248248
return files
249249

250250

251-
def create_file_content_string(files: List[Dict[str, Any]]) -> str:
251+
def create_file_content_string(files: list[dict[str, Any]]) -> str:
252252
"""Creates a formatted string of file contents with separators."""
253253
output = ""
254254
separator = "=" * 48 + "\n"
@@ -278,7 +278,7 @@ def create_file_content_string(files: List[Dict[str, Any]]) -> str:
278278
return output
279279

280280

281-
def create_summary_string(query: Dict[str, Any], nodes: Dict[str, Any]) -> str:
281+
def create_summary_string(query: dict[str, Any], nodes: dict[str, Any]) -> str:
282282
"""Creates a summary string with file counts and content size."""
283283
if "user_name" in query:
284284
summary = f"Repository: {query['user_name']}/{query['repo_name']}\n"
@@ -297,7 +297,7 @@ def create_summary_string(query: Dict[str, Any], nodes: Dict[str, Any]) -> str:
297297
return summary
298298

299299

300-
def create_tree_structure(query: Dict[str, Any], node: Dict[str, Any], prefix: str = "", is_last: bool = True) -> str:
300+
def create_tree_structure(query: dict[str, Any], node: dict[str, Any], prefix: str = "", is_last: bool = True) -> str:
301301
"""Creates a tree-like string representation of the file structure."""
302302
tree = ""
303303

@@ -319,7 +319,7 @@ def create_tree_structure(query: Dict[str, Any], node: Dict[str, Any], prefix: s
319319
return tree
320320

321321

322-
def generate_token_string(context_string: str) -> Optional[str]:
322+
def generate_token_string(context_string: str) -> str | None:
323323
"""Returns the number of tokens in a text string."""
324324
formatted_tokens = ""
325325
try:
@@ -340,7 +340,7 @@ def generate_token_string(context_string: str) -> Optional[str]:
340340
return formatted_tokens
341341

342342

343-
def ingest_single_file(path: str, query: Dict[str, Any]) -> Tuple[str, str, str]:
343+
def ingest_single_file(path: str, query: dict[str, Any]) -> tuple[str, str, str]:
344344
if not os.path.isfile(path):
345345
raise ValueError(f"Path {path} is not a file")
346346

@@ -376,7 +376,7 @@ def ingest_single_file(path: str, query: Dict[str, Any]) -> Tuple[str, str, str]
376376
return summary, tree, files_content
377377

378378

379-
def ingest_directory(path: str, query: Dict[str, Any]) -> Tuple[str, str, str]:
379+
def ingest_directory(path: str, query: dict[str, Any]) -> tuple[str, str, str]:
380380
nodes = scan_directory(path=path, query=query)
381381
if not nodes:
382382
raise ValueError(f"No files found in {path}")
@@ -392,7 +392,7 @@ def ingest_directory(path: str, query: Dict[str, Any]) -> Tuple[str, str, str]:
392392
return summary, tree, files_content
393393

394394

395-
def ingest_from_query(query: Dict[str, Any]) -> Tuple[str, str, str]:
395+
def ingest_from_query(query: dict[str, Any]) -> tuple[str, str, str]:
396396
"""Main entry point for analyzing a codebase directory or single file."""
397397
path = f"{query['local_path']}{query['subpath']}"
398398
if not os.path.exists(path):

src/gitingest/parse_query.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
import string
33
import uuid
4-
from typing import Any, Dict, List, Optional, Union
4+
from typing import Any
55
from urllib.parse import unquote
66

77
from gitingest.ignore_patterns import DEFAULT_IGNORE_PATTERNS
@@ -10,7 +10,7 @@
1010
HEX_DIGITS = set(string.hexdigits)
1111

1212

13-
def parse_url(url: str) -> Dict[str, Any]:
13+
def parse_url(url: str) -> dict[str, Any]:
1414
url = url.split(" ")[0]
1515
url = unquote(url) # Decode URL-encoded characters
1616

@@ -88,7 +88,7 @@ def normalize_pattern(pattern: str) -> str:
8888
return pattern
8989

9090

91-
def parse_patterns(pattern: Union[List[str], str]) -> List[str]:
91+
def parse_patterns(pattern: list[str] | str) -> list[str]:
9292
patterns = pattern if isinstance(pattern, list) else [pattern]
9393
patterns = [p.strip() for p in patterns]
9494

@@ -102,7 +102,7 @@ def parse_patterns(pattern: Union[List[str], str]) -> List[str]:
102102
return [normalize_pattern(p) for p in patterns]
103103

104104

105-
def override_ignore_patterns(ignore_patterns: List[str], include_patterns: List[str]) -> List[str]:
105+
def override_ignore_patterns(ignore_patterns: list[str], include_patterns: list[str]) -> list[str]:
106106
"""
107107
Removes patterns from ignore_patterns that are present in include_patterns using set difference.
108108
@@ -121,7 +121,7 @@ def override_ignore_patterns(ignore_patterns: List[str], include_patterns: List[
121121
return list(set(ignore_patterns) - set(include_patterns))
122122

123123

124-
def parse_path(path: str) -> Dict[str, Any]:
124+
def parse_path(path: str) -> dict[str, Any]:
125125
query = {
126126
"url": None,
127127
"local_path": os.path.abspath(path),
@@ -136,9 +136,9 @@ def parse_query(
136136
source: str,
137137
max_file_size: int,
138138
from_web: bool,
139-
include_patterns: Optional[Union[List[str], str]] = None,
140-
ignore_patterns: Optional[Union[List[str], str]] = None,
141-
) -> Dict[str, Any]:
139+
include_patterns: list[str] | str | None = None,
140+
ignore_patterns: list[str] | str | None = None,
141+
) -> dict[str, Any]:
142142
"""
143143
Parses the input source to construct a query dictionary with specified parameters.
144144

src/gitingest/tests/test_ingest.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from pathlib import Path
2-
from typing import Any, Dict
2+
from typing import Any
33

44
import pytest
55

@@ -8,7 +8,7 @@
88

99
# Test fixtures
1010
@pytest.fixture
11-
def sample_query() -> Dict[str, Any]:
11+
def sample_query() -> dict[str, Any]:
1212
return {
1313
'user_name': 'test_user',
1414
'repo_name': 'test_repo',
@@ -73,7 +73,7 @@ def temp_directory(tmp_path: Path) -> Path:
7373
return test_dir
7474

7575

76-
def test_scan_directory(temp_directory: Path, sample_query: Dict[str, Any]) -> None:
76+
def test_scan_directory(temp_directory: Path, sample_query: dict[str, Any]) -> None:
7777
result = scan_directory(str(temp_directory), query=sample_query)
7878
if result is None:
7979
assert False, "Result is None"
@@ -84,7 +84,7 @@ def test_scan_directory(temp_directory: Path, sample_query: Dict[str, Any]) -> N
8484
assert len(result['children']) == 5 # file1.txt, file2.py, src, dir1, dir2
8585

8686

87-
def test_extract_files_content(temp_directory: Path, sample_query: Dict[str, Any]) -> None:
87+
def test_extract_files_content(temp_directory: Path, sample_query: dict[str, Any]) -> None:
8888
nodes = scan_directory(str(temp_directory), query=sample_query)
8989
if nodes is None:
9090
assert False, "Nodes is None"

src/gitingest/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
## Async Timeout decorator
22
import asyncio
33
import functools
4-
from typing import Awaitable, Callable, ParamSpec, TypeVar
4+
from collections.abc import Awaitable, Callable
5+
from typing import ParamSpec, TypeVar
56

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

src/main.py

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

43
from api_analytics.fastapi import Analytics
54
from dotenv import load_dotenv
@@ -52,7 +51,7 @@ async def rate_limit_exception_handler(request: Request, exc: Exception) -> Resp
5251

5352

5453
@app.get("/health")
55-
async def health_check() -> Dict[str, str]:
54+
async def health_check() -> dict[str, str]:
5655
return {"status": "healthy"}
5756

5857

0 commit comments

Comments
 (0)