Skip to content

Commit 931cbfb

Browse files
Copilotbact
andcommitted
Fix mypy errors with compact dependencies: handle numpy return types and remove unused type ignores
Co-authored-by: bact <128572+bact@users.noreply.github.com>
1 parent ea77ca3 commit 931cbfb

8 files changed

Lines changed: 18 additions & 18 deletions

File tree

pythainlp/summarize/keybert.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def embed(self, docs: Union[str, list[str]]) -> np.ndarray:
154154
[np.array(emb[0]).mean(axis=0) for emb in embs]
155155
)
156156

157-
return emb_mean
157+
return emb_mean # type: ignore[no-any-return]
158158

159159

160160
def _generate_ngrams(
@@ -223,10 +223,10 @@ def l2_norm(v: np.ndarray) -> np.ndarray:
223223
)
224224
if not np.isclose(np.linalg.norm(result, axis=1), 1).all():
225225
raise ValueError("Cannot normalize a vector to unit vector.")
226-
return result
226+
return result # type: ignore[no-any-return]
227227

228228
def cosine_sim(a: np.ndarray, b: np.ndarray) -> np.ndarray:
229-
return (np.matmul(a, b.T).T).sum(axis=1)
229+
return (np.matmul(a, b.T).T).sum(axis=1) # type: ignore[no-any-return]
230230

231231
doc_vector = l2_norm(doc_vector)
232232
word_vectors = l2_norm(word_vectors)

pythainlp/tag/wangchanberta_onnx.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def postprocess(self, logits_data: "np.ndarray") -> "np.ndarray":
8888
maxes = np.max(logits_t, axis=-1, keepdims=True)
8989
shifted_exp = np.exp(logits_t - maxes)
9090
scores = shifted_exp / shifted_exp.sum(axis=-1, keepdims=True)
91-
return scores
91+
return scores # type: ignore[no-any-return]
9292

9393
def clean_output(
9494
self, list_text: list[tuple[str, str]]

pythainlp/tokenize/nlpo3.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from typing import TYPE_CHECKING, Any, Optional
1010

1111
if TYPE_CHECKING:
12-
from nlpo3 import ( # type: ignore[import-not-found]
12+
from nlpo3 import (
1313
load_dict as nlpo3_load_dict, # noqa: F401
1414
)
1515
from nlpo3 import segment as nlpo3_segment # noqa: F401
@@ -89,7 +89,7 @@ def load_dict(file_path: str, dict_name: str) -> bool:
8989
msg, success = nlpo3_load_dict(file_path=file_path, dict_name=dict_name)
9090
if not success:
9191
print(msg, file=stderr)
92-
return success # type: ignore[no-any-return]
92+
return success
9393

9494

9595
def segment(
@@ -127,7 +127,7 @@ def segment(
127127
if custom_dict == _NLPO3_DEFAULT_DICT_NAME:
128128
_ensure_default_dict_loaded()
129129

130-
return nlpo3_segment( # type: ignore[no-any-return]
130+
return nlpo3_segment(
131131
text=text,
132132
dict_name=custom_dict,
133133
safe=safe_mode,

pythainlp/transliterate/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def transliterate(
179179
elif engine == "thaig2p_v2":
180180
from pythainlp.transliterate.thaig2p_v2 import transliterate # noqa: I001
181181
elif engine == "umt5_thaig2p":
182-
from pythainlp.translate.umt5_thaig2p import transliterate # type: ignore[import-not-found,no-redef] # noqa: I001
182+
from pythainlp.translate.umt5_thaig2p import transliterate # type: ignore[import-untyped,no-redef] # noqa: I001
183183
else: # use default engine: "thaig2p"
184184
from pythainlp.transliterate.thaig2p import transliterate # noqa: I001
185185

pythainlp/transliterate/thai2rom_onnx.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def _prepare_sequence_in(self, text: str) -> "np.ndarray":
7676
else:
7777
idxs.append(self._char_to_ix["<UNK>"])
7878
idxs.append(self._char_to_ix["<end>"])
79-
return np.array(idxs)
79+
return np.array(idxs) # type: ignore[no-any-return]
8080

8181
def romanize(self, text: str) -> str:
8282
""":param str text: Thai text to be romanized
@@ -131,7 +131,7 @@ def __init__(
131131

132132
def create_mask(self, source_seq: "np.ndarray") -> "np.ndarray":
133133
mask = source_seq != self.pad_idx
134-
return mask
134+
return mask # type: ignore[no-any-return]
135135

136136
def run(
137137
self, source_seq: "np.ndarray", source_seq_len: List[int]
@@ -196,9 +196,9 @@ def run(
196196
decoder_input = np.array([topi])
197197

198198
if decoder_input == end_token:
199-
return outputs[:di]
199+
return outputs[:di] # type: ignore[no-any-return]
200200

201-
return outputs
201+
return outputs # type: ignore[no-any-return]
202202

203203

204204
_THAI_TO_ROM_ONNX: ThaiTransliterator_ONNX = ThaiTransliterator_ONNX()

pythainlp/transliterate/w2p.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def _load_variables(self) -> None:
151151
def _sigmoid(self, x: "np.ndarray") -> "np.ndarray":
152152
import numpy as np
153153

154-
return 1 / (1 + np.exp(-x))
154+
return 1 / (1 + np.exp(-x)) # type: ignore[no-any-return]
155155

156156
def _grucell(
157157
self,
@@ -205,7 +205,7 @@ def _gru(
205205
h = self._grucell(x[:, t, :], h, w_ih, w_hh, b_ih, b_hh) # (b, h)
206206
outputs[:, t, ::] = h
207207

208-
return outputs
208+
return outputs # type: ignore[no-any-return]
209209

210210
def _encode(self, word: str) -> "np.ndarray":
211211
import numpy as np
@@ -214,7 +214,7 @@ def _encode(self, word: str) -> "np.ndarray":
214214
x = [self.g2idx.get(char, self.g2idx["<unk>"]) for char in chars]
215215
x = np.take(self.enc_emb, np.expand_dims(x, 0), axis=0)
216216

217-
return x
217+
return x # type: ignore[no-any-return]
218218

219219
def _short_word(self, word: str) -> Optional[str]:
220220
self.word: str = word

pythainlp/ulmfit/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def document_vector(
239239
else:
240240
raise ValueError("Aggregate by mean or sum")
241241

242-
return res
242+
return res # type: ignore[no-any-return]
243243

244244

245245
def merge_wgts(

pythainlp/word_vector/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ def sentence_vectorizer(self, text: str, use_mean: bool = True) -> ndarray:
306306
len_words = len(words)
307307

308308
if not len_words:
309-
return vec
309+
return vec # type: ignore[no-any-return]
310310

311311
for word in words:
312312
if word == " " and self.model_name == "thai2fit_wv":
@@ -320,4 +320,4 @@ def sentence_vectorizer(self, text: str, use_mean: bool = True) -> ndarray:
320320
if use_mean:
321321
vec /= len_words
322322

323-
return vec
323+
return vec # type: ignore[no-any-return]

0 commit comments

Comments
 (0)