-
Notifications
You must be signed in to change notification settings - Fork 289
Refactor type: ignore suppressions to proper type fixes #1235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7646188
998728a
936a507
0b76729
ce850f8
b8a531d
c1afb66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,7 @@ | |
|
|
||
| import threading | ||
| from importlib.resources import as_file, files | ||
| from typing import Optional, Union | ||
| from typing import Optional, Union, cast | ||
|
|
||
| try: | ||
| import pycrfsuite | ||
|
|
@@ -101,9 +101,9 @@ def featurize( | |
| if indiv_char: | ||
| left_key = "|".join([str(relative_index_left), char_left]) | ||
| if return_type == "dict": | ||
| features[left_key] = 1 | ||
| cast(dict[str, int], features)[left_key] = 1 | ||
| else: | ||
| features.append(left_key) | ||
| cast(list[str], features).append(left_key) | ||
|
|
||
| abs_index_right += ( | ||
| 1 # สมมุติคือตำแหน่งที่ 0 จะได้ 0, 1, 2, 3, 4 (radius = 5) | ||
|
|
@@ -119,9 +119,9 @@ def featurize( | |
| [str(relative_index_right), char_right] | ||
| ) | ||
| if return_type == "dict": | ||
| features[right_key] = 1 | ||
| cast(dict[str, int], features)[right_key] = 1 | ||
| else: | ||
| features.append(right_key) | ||
| cast(list[str], features).append(right_key) | ||
|
Comment on lines
121
to
+124
|
||
|
|
||
| counter += 1 | ||
|
|
||
|
|
@@ -130,13 +130,14 @@ def featurize( | |
| ngram = chars[i : i + self.N] | ||
| ngram_key = "|".join([str(i - self.radius), ngram]) | ||
| if return_type == "dict": | ||
| features[ngram_key] = 1 | ||
| cast(dict[str, int], features)[ngram_key] = 1 | ||
| else: | ||
| features.append(ngram_key) | ||
| cast(list[str], features).append(ngram_key) | ||
|
Comment on lines
132
to
+135
|
||
| all_features.append(features) | ||
| if return_type == "list": | ||
| cut = str(cut) | ||
| all_labels.append(cut) | ||
| all_labels.append(str(cut)) | ||
| else: | ||
| all_labels.append(cut) | ||
|
|
||
| return {"X": all_features, "Y": all_labels} | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -202,12 +202,12 @@ def convert_years(year: str, src="be", target="ad") -> str: | |||||
| return output_year | ||||||
|
|
||||||
|
|
||||||
| def _find_month(text: str) -> Optional[int]: | ||||||
| def _find_month(text: str) -> int: | ||||||
| for i, m in enumerate(thai_full_month_lists): | ||||||
| for j in m: | ||||||
| if j in text: | ||||||
| return i + 1 | ||||||
| return None | ||||||
| return 0 # Not found in list | ||||||
|
||||||
| return 0 # Not found in list | |
| raise ValueError(f"Month not found in text: {text!r}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using cast() to work around Union type narrowing defeats the purpose of static type checking and can lead to runtime errors. The cast() function tells the type checker to trust that the value is of a certain type without actually performing any runtime validation.
A more type-safe approach would be to properly narrow the Union type through control flow. Instead of using cast(), you could check the type after initialization:
Or refactor the code to avoid the Union type altogether by using separate variables or a more structured approach. The repeated casting throughout the method (lines 104, 106, 122, 124, 133, 135) suggests the control flow could be simplified.