Skip to content

Commit 82ba650

Browse files
committed
Fix pyright errors: use Sequence for batch method signatures
Change list to Sequence in add_terms_batch and add_properties_batch interfaces and implementations to satisfy covariance. Add missing add_terms_batch to FakeTermIndex in conftest.py.
1 parent fcc7c23 commit 82ba650

7 files changed

Lines changed: 53 additions & 13 deletions

File tree

src/typeagent/knowpro/interfaces_core.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from __future__ import annotations
66

7+
from collections.abc import Sequence
78
from datetime import datetime as Datetime
89
from typing import (
910
Any,
@@ -170,7 +171,7 @@ async def add_term(
170171

171172
async def add_terms_batch(
172173
self,
173-
terms: list[tuple[str, SemanticRefOrdinal | ScoredSemanticRefOrdinal]],
174+
terms: Sequence[tuple[str, SemanticRefOrdinal | ScoredSemanticRefOrdinal]],
174175
) -> None: ...
175176

176177
async def remove_term(

src/typeagent/knowpro/interfaces_indexes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ async def add_property(
6161

6262
async def add_properties_batch(
6363
self,
64-
properties: list[tuple[str, str, SemanticRefOrdinal | ScoredSemanticRefOrdinal]],
64+
properties: Sequence[tuple[str, str, SemanticRefOrdinal | ScoredSemanticRefOrdinal]],
6565
) -> None: ...
6666

6767
async def lookup_property(

src/typeagent/storage/memory/propindex.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Copyright (c) Microsoft Corporation.
22
# Licensed under the MIT License.
33

4+
from collections.abc import Sequence
45
import enum
56
from typing import assert_never
67

@@ -156,7 +157,13 @@ def collect_action_properties(
156157
if action.object_entity_name != "none":
157158
props.append((PropertyNames.Object.value, action.object_entity_name, ordinal))
158159
if action.indirect_object_entity_name != "none":
159-
props.append((PropertyNames.IndirectObject.value, action.indirect_object_entity_name, ordinal))
160+
props.append(
161+
(
162+
PropertyNames.IndirectObject.value,
163+
action.indirect_object_entity_name,
164+
ordinal,
165+
)
166+
)
160167
return props
161168

162169

@@ -186,15 +193,23 @@ async def add_to_property_index(
186193
assert semantic_ref.semantic_ref_ordinal == semantic_ref_ordinal
187194
if isinstance(semantic_ref.knowledge, kplib.Action):
188195
collected.extend(
189-
collect_action_properties(semantic_ref.knowledge, semantic_ref_ordinal)
196+
collect_action_properties(
197+
semantic_ref.knowledge, semantic_ref_ordinal
198+
)
190199
)
191200
elif isinstance(semantic_ref.knowledge, kplib.ConcreteEntity):
192201
collected.extend(
193-
collect_entity_properties(semantic_ref.knowledge, semantic_ref_ordinal)
202+
collect_entity_properties(
203+
semantic_ref.knowledge, semantic_ref_ordinal
204+
)
194205
)
195206
elif isinstance(semantic_ref.knowledge, Tag):
196207
collected.append(
197-
(PropertyNames.Tag.value, semantic_ref.knowledge.text, semantic_ref_ordinal)
208+
(
209+
PropertyNames.Tag.value,
210+
semantic_ref.knowledge.text,
211+
semantic_ref_ordinal,
212+
)
198213
)
199214
elif isinstance(semantic_ref.knowledge, Topic):
200215
pass
@@ -239,7 +254,9 @@ async def add_property(
239254

240255
async def add_properties_batch(
241256
self,
242-
properties: list[tuple[str, str, SemanticRefOrdinal | ScoredSemanticRefOrdinal]],
257+
properties: Sequence[
258+
tuple[str, str, SemanticRefOrdinal | ScoredSemanticRefOrdinal]
259+
],
243260
) -> None:
244261
for name, value, ordinal in properties:
245262
await self.add_property(name, value, ordinal)

src/typeagent/storage/memory/semrefindex.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33

44
from __future__ import annotations # TODO: Avoid
55

6-
from collections.abc import AsyncIterable, Callable
6+
from collections.abc import AsyncIterable, Callable, Sequence
77

88
from typechat import Failure
99

10-
from ...knowpro import convknowledge, knowledge_schema as kplib, secindex
10+
from ...knowpro import convknowledge
11+
from ...knowpro import knowledge_schema as kplib
12+
from ...knowpro import secindex
1113
from ...knowpro.convsettings import ConversationSettings, SemanticRefIndexSettings
1214
from ...knowpro.interfaces import ( # Interfaces.; Other imports.
1315
IConversation,
@@ -711,7 +713,7 @@ async def add_term(
711713

712714
async def add_terms_batch(
713715
self,
714-
terms: list[tuple[str, SemanticRefOrdinal | ScoredSemanticRefOrdinal]],
716+
terms: Sequence[tuple[str, SemanticRefOrdinal | ScoredSemanticRefOrdinal]],
715717
) -> None:
716718
for term, ordinal in terms:
717719
await self.add_term(term, ordinal)

src/typeagent/storage/sqlite/propindex.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
"""SQLite-based property index implementation."""
55

6+
from collections.abc import Sequence
67
import sqlite3
78

89
from ...knowpro import interfaces
@@ -69,14 +70,21 @@ async def add_property(
6970

7071
async def add_properties_batch(
7172
self,
72-
properties: list[tuple[str, str, interfaces.SemanticRefOrdinal | interfaces.ScoredSemanticRefOrdinal]],
73+
properties: Sequence[
74+
tuple[
75+
str,
76+
str,
77+
interfaces.SemanticRefOrdinal | interfaces.ScoredSemanticRefOrdinal,
78+
]
79+
],
7380
) -> None:
7481
if not properties:
7582
return
7683
from ...storage.memory.propindex import (
7784
make_property_term_text,
7885
split_property_term_text,
7986
)
87+
8088
rows = []
8189
for property_name, value, ordinal in properties:
8290
if isinstance(ordinal, interfaces.ScoredSemanticRefOrdinal):

src/typeagent/storage/sqlite/semrefindex.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
"""SQLite-based semantic reference index implementation."""
55

6+
from collections.abc import Sequence
67
import re
78
import sqlite3
89
import unicodedata
@@ -58,7 +59,11 @@ async def add_term(
5859

5960
async def add_terms_batch(
6061
self,
61-
terms: list[tuple[str, interfaces.SemanticRefOrdinal | interfaces.ScoredSemanticRefOrdinal]],
62+
terms: Sequence[
63+
tuple[
64+
str, interfaces.SemanticRefOrdinal | interfaces.ScoredSemanticRefOrdinal
65+
]
66+
],
6267
) -> None:
6368
if not terms:
6469
return

tests/conftest.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright (c) Microsoft Corporation.
22
# Licensed under the MIT License.
33

4-
from collections.abc import AsyncGenerator, Callable, Iterator
4+
from collections.abc import AsyncGenerator, Callable, Iterator, Sequence
55
import os
66
from pathlib import Path
77
import tempfile
@@ -236,6 +236,13 @@ async def add_term(
236236
self.term_to_refs[term].append(scored_ref)
237237
return term
238238

239+
async def add_terms_batch(
240+
self,
241+
terms: Sequence[tuple[str, int | ScoredSemanticRefOrdinal]],
242+
) -> None:
243+
for term, ordinal in terms:
244+
await self.add_term(term, ordinal)
245+
239246
async def remove_term(self, term: str, semantic_ref_ordinal: int) -> None:
240247
if term in self.term_to_refs:
241248
self.term_to_refs[term] = [

0 commit comments

Comments
 (0)