Skip to content

Commit 1fdbeda

Browse files
committed
Improve type annotations
Also rename FrozenDict to FrozenAttributes to better match its intent.
1 parent 68ded0f commit 1fdbeda

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

curtsies/formatstring.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,22 @@
2020
"""
2121

2222
import re
23-
import sys
2423
from cwcwidth import wcswidth, wcwidth
2524
from itertools import chain
2625
from typing import (
26+
Any,
27+
Callable,
28+
Dict,
29+
Iterable,
2730
Iterator,
28-
Tuple,
2931
List,
30-
Union,
31-
Optional,
32-
Any,
3332
Mapping,
34-
cast,
3533
MutableMapping,
34+
Optional,
35+
Tuple,
36+
Union,
37+
cast,
3638
no_type_check,
37-
Type,
38-
Callable,
39-
Iterable,
4039
)
4140

4241
try:
@@ -76,8 +75,8 @@
7675
xforms.update(two_arg_xforms)
7776

7877

79-
class FrozenDict(dict):
80-
"""Immutable dictionary class"""
78+
class FrozenAttributes(Dict[str, Union[int, bool]]):
79+
"""Immutable dictionary class for format string attributes"""
8180

8281
@no_type_check
8382
def __setitem__(self, key, value):
@@ -87,11 +86,11 @@ def __setitem__(self, key, value):
8786
def update(self, *args, **kwds):
8887
raise Exception("Cannot change value.")
8988

90-
def extend(self, dictlike: Mapping[str, Union[int, bool]]) -> "FrozenDict":
91-
return FrozenDict(chain(self.items(), dictlike.items()))
89+
def extend(self, dictlike: Mapping[str, Union[int, bool]]) -> "FrozenAttributes":
90+
return FrozenAttributes(chain(self.items(), dictlike.items()))
9291

93-
def remove(self, *keys: str) -> "FrozenDict":
94-
return FrozenDict((k, v) for k, v in self.items() if k not in keys)
92+
def remove(self, *keys: str) -> "FrozenAttributes":
93+
return FrozenAttributes((k, v) for k, v in self.items() if k not in keys)
9594

9695

9796
def stable_format_dict(d: Mapping) -> str:
@@ -121,14 +120,14 @@ def __init__(
121120
if not isinstance(string, str):
122121
raise ValueError("unicode string required, got %r" % string)
123122
self._s = string
124-
self._atts = FrozenDict(atts if atts else {})
123+
self._atts = FrozenAttributes(atts if atts else {})
125124

126125
@property
127126
def s(self) -> str:
128127
return self._s
129128

130129
@property
131-
def atts(self) -> Mapping[str, Union[int, bool]]:
130+
def atts(self) -> FrozenAttributes:
132131
"Attributes, e.g. {'fg': 34, 'bold': True} where 34 is the escape code for ..."
133132
return self._atts
134133

@@ -417,8 +416,9 @@ def append(self, string: Union[str, "FmtStr"]) -> "FmtStr":
417416
def copy_with_new_atts(self, **attributes: Union[bool, int]) -> "FmtStr":
418417
"""Returns a new FmtStr with the same content but new formatting"""
419418

420-
result = FmtStr(*(Chunk(bfs.s, bfs.atts.extend(attributes)) for bfs in self.chunks)) # type: ignore
421-
return result
419+
return FmtStr(
420+
*(Chunk(bfs.s, bfs.atts.extend(attributes)) for bfs in self.chunks)
421+
)
422422

423423
def join(self, iterable: Iterable[Union[str, "FmtStr"]]) -> "FmtStr":
424424
"""Joins an iterable yielding strings or FmtStrs with self as separator"""

curtsies/formatstringarray.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,9 @@ def blink(x: str) -> str:
251251
)
252252

253253

254-
def fsarray(strings: List[Union[FmtStr, str]], *args: Any, **kwargs: Any) -> FSArray:
254+
def fsarray(
255+
strings: Sequence[Union[FmtStr, str]], *args: Any, **kwargs: Any
256+
) -> FSArray:
255257
"""fsarray(list_of_FmtStrs_or_strings, width=None) -> FSArray
256258
257259
Returns a new FSArray of width of the maximum size of the provided
@@ -279,7 +281,7 @@ def fsarray(strings: List[Union[FmtStr, str]], *args: Any, **kwargs: Any) -> FSA
279281
return arr
280282

281283

282-
def simple_format(x: Union[FSArray, List[FmtStr]]) -> str:
284+
def simple_format(x: Union[FSArray, Sequence[FmtStr]]) -> str:
283285
return "\n".join(str(l) for l in x)
284286

285287

0 commit comments

Comments
 (0)