From 0a5b45ffd5778c1c495c35827b52c936fc42ed3a Mon Sep 17 00:00:00 2001 From: Javier Martinez Date: Thu, 16 Jul 2026 16:07:13 +0200 Subject: [PATCH] fix: mypy --- .../processors/events/citations/citations.py | 12 ++---- .../interceptors/condensation_interceptor.py | 6 ++- .../extract_citation_interceptor.py | 4 +- tests/components/tools/test_tool_scheduler.py | 11 +++-- tests/conftest.py | 40 ++++++++++++++----- tests/engines/test_processor_citations.py | 3 +- 6 files changed, 50 insertions(+), 26 deletions(-) diff --git a/private_gpt/components/chat/processors/events/citations/citations.py b/private_gpt/components/chat/processors/events/citations/citations.py index dd996743..51b97505 100644 --- a/private_gpt/components/chat/processors/events/citations/citations.py +++ b/private_gpt/components/chat/processors/events/citations/citations.py @@ -30,7 +30,6 @@ async def process_citations( current_text = "" citation_indices: dict[str, int] = {} last_delta_type: str | None = None - last_block_id: str | None = None async for event in event_generator: if not event or isinstance(event, Exception): @@ -42,12 +41,10 @@ async def process_citations( send_citations = [] current_text = "" last_delta_type = None - last_block_id = event.block_id elif isinstance(event, RawContentBlockDeltaEvent) and event.delta: if isinstance(event.delta, TextDelta): last_delta_type = "text" - last_block_id = event.block_id delta_text = event.delta.text or "" current_text += delta_text @@ -81,7 +78,6 @@ async def process_citations( elif isinstance(event.delta, ThinkingDelta): last_delta_type = "thinking" - last_block_id = event.block_id delta_thinking = event.delta.thinking or "" current_text += delta_thinking @@ -143,7 +139,9 @@ async def process_citations( else: delta = TextDelta.from_citations(delta_text, delta_citation) - yield RawContentBlockDeltaEvent(block_id=event.block_id, delta=delta) + yield RawContentBlockDeltaEvent( + block_id=event.block_id, delta=delta + ) send_text = cleaned_text send_citations.extend(delta_citation) @@ -153,9 +151,7 @@ async def process_citations( current_documents = documents_fn() if documents_fn else None if current_documents and current_text: if not citation_indices: - citation_indices = ( - citation_indices_fn() if citation_indices_fn else {} - ) + citation_indices = citation_indices_fn() if citation_indices_fn else {} result = await asyncio.to_thread( extract_citations_by_original_text, text=current_text, diff --git a/private_gpt/server/chat/interceptors/condensation_interceptor.py b/private_gpt/server/chat/interceptors/condensation_interceptor.py index 2b1c6e88..a3dd37c2 100644 --- a/private_gpt/server/chat/interceptors/condensation_interceptor.py +++ b/private_gpt/server/chat/interceptors/condensation_interceptor.py @@ -170,8 +170,12 @@ class CondensationRequestInterceptor(ChatRequestLoopInterceptor): if not self._enabled or not history: return + token_limit = state.runtime.effective_token_limit + if token_limit is None: + return + max_length = _token_limit_with_buffer( - context.state.runtime.effective_token_limit, + token_limit, state.input.request.condensation.token_buffer, ) diff --git a/private_gpt/server/chat/interceptors/extract_citation_interceptor.py b/private_gpt/server/chat/interceptors/extract_citation_interceptor.py index bd6b9d3c..d8f56147 100644 --- a/private_gpt/server/chat/interceptors/extract_citation_interceptor.py +++ b/private_gpt/server/chat/interceptors/extract_citation_interceptor.py @@ -101,7 +101,9 @@ class ExtractCitationInterceptor(ChatResponseLoopInterceptor): if self._last_delta_type == "text": delta = TextDelta.from_citations(new_delta_text, new_citations) elif self._last_delta_type == "thinking": - delta = ThinkingDelta.from_citations(new_delta_text, new_citations) + delta = ThinkingDelta.from_citations( + new_delta_text, new_citations + ) else: delta = TextDelta.from_citations(new_delta_text, new_citations) diff --git a/tests/components/tools/test_tool_scheduler.py b/tests/components/tools/test_tool_scheduler.py index 0edda0c3..0b492fe1 100644 --- a/tests/components/tools/test_tool_scheduler.py +++ b/tests/components/tools/test_tool_scheduler.py @@ -35,10 +35,13 @@ async def test_local_tool_scheduler_logs_execution_failure( execute_tool_request, ) - with caplog.at_level( - logging.ERROR, - logger="private_gpt.components.tools.tool_scheduler", - ), pytest.raises(RuntimeError, match="boom"): + with ( + caplog.at_level( + logging.ERROR, + logger="private_gpt.components.tools.tool_scheduler", + ), + pytest.raises(RuntimeError, match="boom"), + ): await LocalToolScheduler().execute(tool_request()) assert "Local tool 'bash' execution failed" in caplog.text diff --git a/tests/conftest.py b/tests/conftest.py index 3dda5906..5cd36521 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -7,11 +7,14 @@ root_path = pathlib.Path(__file__).parents[1] # This is to prevent a bug in intellij that uses the wrong working directory os.chdir(root_path) + def _as_module(fixture_path: str) -> str: return fixture_path.replace("/", ".").replace("\\", ".").replace(".py", "") + pytest_plugins = [_as_module(fixture) for fixture in glob("tests/fixtures/[!_]*.py")] + def pytest_addoption(parser): parser.addoption( "--git-diff", @@ -32,17 +35,24 @@ def pytest_addoption(parser): help="Only run tests in the specified block(s) / directory (e.g. server, components)", ) + def pytest_collection_modifyitems(config, items): blocks = config.getoption("--block") if blocks: selected = [] deselected = [] for item in items: - test_file_path = str(getattr(item, "path", getattr(item, "fspath", ""))).replace("\\", "/") + test_file_path = str( + getattr(item, "path", getattr(item, "fspath", "")) + ).replace("\\", "/") matched = False for block in blocks: block_pattern = f"/{block}/" - if block_pattern in test_file_path or test_file_path.startswith(f"tests/{block}/") or test_file_path.endswith(f"/{block}"): + if ( + block_pattern in test_file_path + or test_file_path.startswith(f"tests/{block}/") + or test_file_path.endswith(f"/{block}") + ): matched = True break if matched: @@ -55,17 +65,19 @@ def pytest_collection_modifyitems(config, items): if config.getoption("--git-diff"): changed_files = set() - + def run_cmd(cmd): try: - res = subprocess.run(cmd, shell=True, capture_output=True, text=True, check=True) + res = subprocess.run( + cmd, shell=True, capture_output=True, text=True, check=True + ) return [line.strip() for line in res.stdout.split("\n") if line.strip()] except Exception: return [] changed_files.update(run_cmd("git diff --name-only")) changed_files.update(run_cmd("git diff --cached --name-only")) - + target = config.getoption("--git-target") if not target: if run_cmd("git rev-parse --verify origin/main"): @@ -81,7 +93,7 @@ def pytest_collection_modifyitems(config, items): changed_files.update(run_cmd(f"git diff --name-only {merge_base[0]}")) else: changed_files.update(run_cmd(f"git diff --name-only {target}")) - + if not changed_files: print("\n[pytest-git-diff] No changed files detected. Running all tests.") return @@ -104,14 +116,18 @@ def pytest_collection_modifyitems(config, items): has_global_change = any(f in global_impact_files for f in changed_files) if has_global_change: - print("\n[pytest-git-diff] Global config or core files changed. Running all tests.") + print( + "\n[pytest-git-diff] Global config or core files changed. Running all tests." + ) return selected = [] deselected = [] for item in items: - test_file_path = str(getattr(item, "path", getattr(item, "fspath", ""))).replace("\\", "/") + test_file_path = str( + getattr(item, "path", getattr(item, "fspath", "")) + ).replace("\\", "/") affected = False for f in changed_files: f = f.replace("\\", "/") @@ -119,7 +135,7 @@ def pytest_collection_modifyitems(config, items): affected = True break if f.startswith("private_gpt/"): - subpath = f[len("private_gpt/"):] + subpath = f[len("private_gpt/") :] if "/" not in subpath: affected = True break @@ -136,10 +152,12 @@ def pytest_collection_modifyitems(config, items): if f.startswith("tests/fixtures/") or f.startswith("tests/settings/"): affected = True break - if f.startswith("tests/") and (f in test_file_path or test_file_path in f): + if f.startswith("tests/") and ( + f in test_file_path or test_file_path in f + ): affected = True break - + if affected: selected.append(item) else: diff --git a/tests/engines/test_processor_citations.py b/tests/engines/test_processor_citations.py index 9b76a8b1..e31e3df9 100644 --- a/tests/engines/test_processor_citations.py +++ b/tests/engines/test_processor_citations.py @@ -147,6 +147,7 @@ async def test_process_citations_trailing_backtick() -> None: documents = [doc] cb_text = [] + def my_cb(t, c): cb_text.append(t) @@ -154,7 +155,7 @@ async def test_process_citations_trailing_backtick() -> None: events = [ create_text_delta("This is a test with a trailing backtick `"), ] - async for event in process_citations( + async for _event in process_citations( generate_events(*events), lambda **kwargs: documents, callback=my_cb ): pass