Skip to content

Commit 9093a37

Browse files
committed
modernize text helpers types
1 parent dca98dd commit 9093a37

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

eli5/formatters/text_helpers.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
from collections import Counter
2-
from typing import List, Optional
2+
from typing import Optional
33

44
import numpy as np
55

6-
from eli5.base import TargetExplanation, WeightedSpans, DocWeightedSpans
6+
from eli5.base import TargetExplanation, DocWeightedSpans
77
from eli5.base_utils import attrs
88
from eli5.utils import max_or_0
99

1010

11-
def get_char_weights(doc_weighted_spans, preserve_density=None):
12-
# type: (DocWeightedSpans, Optional[bool]) -> np.ndarray
11+
def get_char_weights(
12+
doc_weighted_spans: DocWeightedSpans, preserve_density: Optional[bool] = None,
13+
) -> np.ndarray:
1314
""" Return character weights for a text document with highlighted features.
1415
If preserve_density is True, then color for longer fragments will be
1516
less intensive than for shorter fragments, so that "sum" of intensities
@@ -35,11 +36,10 @@ def get_char_weights(doc_weighted_spans, preserve_density=None):
3536
@attrs
3637
class PreparedWeightedSpans(object):
3738
def __init__(self,
38-
doc_weighted_spans, # type: DocWeightedSpans
39-
char_weights, # type: np.ndarray
40-
weight_range, # type: float
39+
doc_weighted_spans: DocWeightedSpans,
40+
char_weights: np.ndarray,
41+
weight_range: float,
4142
):
42-
# type: (...) -> None
4343
self.doc_weighted_spans = doc_weighted_spans
4444
self.char_weights = char_weights
4545
self.weight_range = weight_range
@@ -55,25 +55,24 @@ def __eq__(self, other):
5555
return False
5656

5757

58-
def prepare_weighted_spans(targets, # type: List[TargetExplanation]
59-
preserve_density=None, # type: Optional[bool]
60-
):
61-
# type: (...) -> List[Optional[List[PreparedWeightedSpans]]]
58+
def prepare_weighted_spans(targets: list[TargetExplanation],
59+
preserve_density: Optional[bool] = None,
60+
) -> list[Optional[list[PreparedWeightedSpans]]]:
6261
""" Return weighted spans prepared for rendering.
6362
Calculate a separate weight range for each different weighted
6463
span (for each different index): each target has the same number
6564
of weighted spans.
6665
"""
67-
targets_char_weights = [
66+
targets_char_weights: list[Optional[list[np.ndarray]]] = [
6867
[get_char_weights(ws, preserve_density=preserve_density)
6968
for ws in t.weighted_spans.docs_weighted_spans]
7069
if t.weighted_spans else None
71-
for t in targets] # type: List[Optional[List[np.ndarray]]]
70+
for t in targets]
7271
max_idx = max_or_0(len(ch_w or []) for ch_w in targets_char_weights)
7372

74-
targets_char_weights_not_None = [
73+
targets_char_weights_not_None: list[list[np.ndarray]] = [
7574
cw for cw in targets_char_weights
76-
if cw is not None] # type: List[List[np.ndarray]]
75+
if cw is not None]
7776

7877
spans_weight_ranges = [
7978
max_or_0(

0 commit comments

Comments
 (0)