Skip to content

Commit ff2594c

Browse files
authored
Add mypy warn_unreachable (#623)
1 parent 8e10a4f commit ff2594c

File tree

10 files changed

+67
-66
lines changed

10 files changed

+67
-66
lines changed

libs/colbert/pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ asyncio_mode = "auto"
3333

3434
[tool.mypy]
3535
strict = true
36-
follow_imports = "normal"
36+
warn_unreachable = true
37+
pretty = true
3738
show_error_codes = true
3839
show_error_context = true
3940

libs/colbert/tests/integration_tests/test_database.py

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ def test_database_sync(session: Session) -> None:
3232

3333
results = database.add_chunks(chunks=[chunk_0, chunk_1])
3434

35-
assert len(results) == 2 # noqa: PLR2004
36-
assert results[0] == (doc_id, 0)
37-
assert results[1] == (doc_id, 1)
35+
assert results == [(doc_id, 0), (doc_id, 1)]
3836

3937
# TODO: verify other db methods.
4038

@@ -71,46 +69,51 @@ async def test_database_async(session: Session) -> None:
7169
)
7270

7371
results = await database.aadd_chunks(chunks=[chunk_0, chunk_1])
74-
assert len(results) == 2 # noqa: PLR2004
75-
assert results[0] == (doc_id, 0)
76-
assert results[1] == (doc_id, 1)
72+
assert results == [(doc_id, 0), (doc_id, 1)]
7773

7874
chunks = await database.search_relevant_chunks(
7975
vector=climate_change_embedding[5], n=2
8076
)
81-
assert len(chunks) == 1
82-
assert chunks[0].doc_id == doc_id
83-
assert chunks[0].chunk_id == 0
84-
assert chunks[0].text is None
85-
assert chunks[0].metadata == {}
86-
assert chunks[0].embedding is None
77+
assert chunks == [
78+
Chunk(
79+
doc_id=doc_id,
80+
chunk_id=0,
81+
embedding=None,
82+
)
83+
]
8784

8885
chunk = await database.get_chunk_embedding(doc_id=doc_id, chunk_id=1)
89-
assert chunk.doc_id == doc_id
90-
assert chunk.chunk_id == 1
91-
assert chunk.text is None
92-
assert chunk.metadata == {}
93-
assert chunk.embedding == chunk_1.embedding
86+
assert chunk == Chunk(
87+
doc_id=doc_id,
88+
chunk_id=1,
89+
embedding=chunk_1.embedding,
90+
)
9491

9592
chunk = await database.get_chunk_data(doc_id=doc_id, chunk_id=0)
96-
assert chunk.doc_id == doc_id
97-
assert chunk.chunk_id == 0
98-
assert chunk.text == chunk_0.text
99-
# this is broken due to a cassio bug
100-
# which converts Number fields to strings
101-
# assert chunk.metadata == chunk_0.metadata
102-
assert chunk.embedding is None
93+
94+
assert chunk == Chunk(
95+
doc_id=doc_id,
96+
chunk_id=0,
97+
text=chunk_0.text,
98+
# this is broken due to a cassio bug
99+
# which converts Number fields to strings
100+
# metadata=chunk_0.metadata,
101+
embedding=None,
102+
)
103103

104104
chunk = await database.get_chunk_data(
105105
doc_id=doc_id, chunk_id=0, include_embedding=True
106106
)
107-
assert chunk.doc_id == doc_id
108-
assert chunk.chunk_id == 0
109-
assert chunk.text == chunk_0.text
110-
# this is broken due to a cassio bug
111-
# which converts Number fields to strings
112-
# assert chunk.metadata == chunk_0.metadata
113-
assert chunk.embedding == chunk_0.embedding
107+
108+
assert chunk == Chunk(
109+
doc_id=doc_id,
110+
chunk_id=0,
111+
text=chunk_0.text,
112+
# this is broken due to a cassio bug
113+
# which converts Number fields to strings
114+
# metadata=chunk_0.metadata,
115+
embedding=chunk_0.embedding,
116+
)
114117

115118
result = await database.adelete_chunks(doc_ids=[doc_id])
116119
assert result

libs/knowledge-graph/pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ build-backend = "poetry.core.masonry.api"
4444

4545
[tool.mypy]
4646
strict = true
47-
follow_imports = "normal"
47+
warn_unreachable = true
48+
pretty = true
4849
show_error_codes = true
4950
show_error_context = true
5051

libs/knowledge-graph/ragstack_knowledge_graph/knowledge_schema.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,16 @@ def validate_graph_document(self, document: GraphDocument) -> None:
109109
relationships = self._relationships.get(r.type, None)
110110
if relationships is None:
111111
e.add_note(f"No edge type '{r.type}")
112-
else:
113-
relationship = next(
114-
candidate
115-
for candidate in relationships
116-
if r.source.type in candidate.source_types
117-
if r.target.type in candidate.target_types
112+
elif not any(
113+
candidate
114+
for candidate in relationships
115+
if r.source.type in candidate.source_types
116+
and r.target.type in candidate.target_types
117+
):
118+
e.add_note(
119+
"No relationship allows "
120+
f"({r.source.id} -> {r.type} -> {r.target.type})"
118121
)
119-
if relationship is None:
120-
e.add_note(
121-
"No relationship allows "
122-
f"({r.source.id} -> {r.type} -> {r.target.type})"
123-
)
124122

125123
if e.__notes__:
126124
raise e

libs/knowledge-store/pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ build-backend = "poetry.core.masonry.api"
3838

3939
[tool.mypy]
4040
strict = true
41-
follow_imports = "normal"
41+
warn_unreachable = true
42+
pretty = true
4243
show_error_codes = true
4344
show_error_context = true
4445

libs/knowledge-store/ragstack_knowledge_store/graph_store.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@
44
import logging
55
import re
66
import secrets
7+
from collections.abc import Iterable
78
from dataclasses import asdict, dataclass, field, is_dataclass
89
from enum import Enum
910
from typing import (
1011
TYPE_CHECKING,
1112
Any,
12-
Dict,
13-
Iterable,
14-
List,
1513
Sequence,
16-
Set,
17-
Tuple,
1814
Union,
1915
cast,
2016
)
@@ -70,8 +66,8 @@ class MetadataIndexingMode(Enum):
7066
DEFAULT_TO_SEARCHABLE = 2
7167

7268

73-
MetadataIndexingType = Union[Tuple[str, Iterable[str]], str]
74-
MetadataIndexingPolicy = Tuple[MetadataIndexingMode, Set[str]]
69+
MetadataIndexingType = Union[tuple[str, Iterable[str]], str]
70+
MetadataIndexingPolicy = tuple[MetadataIndexingMode, set[str]]
7571

7672

7773
def _is_metadata_field_indexed(field_name: str, policy: MetadataIndexingPolicy) -> bool:
@@ -84,7 +80,7 @@ def _is_metadata_field_indexed(field_name: str, policy: MetadataIndexingPolicy)
8480

8581

8682
def _serialize_metadata(md: dict[str, Any]) -> str:
87-
if isinstance(md.get("links"), Set):
83+
if isinstance(md.get("links"), set):
8884
md = md.copy()
8985
md["links"] = list(md["links"])
9086
return json.dumps(md)
@@ -93,15 +89,12 @@ def _serialize_metadata(md: dict[str, Any]) -> str:
9389
def _serialize_links(links: set[Link]) -> str:
9490
class SetAndLinkEncoder(json.JSONEncoder):
9591
def default(self, obj: Any) -> Any:
96-
if is_dataclass(obj) and not isinstance(obj, type):
92+
if not isinstance(obj, type) and is_dataclass(obj):
9793
return asdict(obj)
9894

99-
try:
100-
iterable = iter(obj)
101-
except TypeError:
102-
pass
103-
else:
104-
return list(iterable)
95+
if isinstance(obj, Iterable):
96+
return list(obj)
97+
10598
# Let the base class default method raise the TypeError
10699
return super().default(obj)
107100

@@ -111,13 +104,13 @@ def default(self, obj: Any) -> Any:
111104
def _deserialize_metadata(json_blob: str | None) -> dict[str, Any]:
112105
# We don't need to convert the links list back to a set -- it will be
113106
# converted when accessed, if needed.
114-
return cast(Dict[str, Any], json.loads(json_blob or ""))
107+
return cast(dict[str, Any], json.loads(json_blob or ""))
115108

116109

117110
def _deserialize_links(json_blob: str | None) -> set[Link]:
118111
return {
119112
Link(kind=link["kind"], direction=link["direction"], tag=link["tag"])
120-
for link in cast(List[Dict[str, Any]], json.loads(json_blob or ""))
113+
for link in cast(list[dict[str, Any]], json.loads(json_blob or ""))
121114
}
122115

123116

libs/langchain/pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ mypy = "^1.11.0"
4545

4646
[tool.mypy]
4747
strict = true
48-
follow_imports = "normal"
48+
warn_unreachable = true
49+
pretty = true
4950
show_error_codes = true
5051
show_error_context = true
5152

libs/llamaindex/pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ ragstack-ai-colbert = { path = "../colbert", develop = true }
5353

5454
[tool.mypy]
5555
strict = true
56-
follow_imports = "normal"
56+
warn_unreachable = true
57+
pretty = true
5758
show_error_codes = true
5859
show_error_context = true
5960

libs/ragulate/pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ test_integration = "scripts.test_integration_runner:main"
5656

5757
[tool.mypy]
5858
strict = true
59-
follow_imports = "normal"
59+
warn_unreachable = true
60+
pretty = true
6061
show_error_codes = true
6162
show_error_context = true
6263

libs/tests-utils/pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ mypy = "^1.10.0"
2121

2222
[tool.mypy]
2323
strict = true
24-
follow_imports = "normal"
24+
warn_unreachable = true
25+
pretty = true
2526
show_error_codes = true
2627
show_error_context = true
2728

0 commit comments

Comments
 (0)