2020"""
2121
2222import re
23- import sys
2423from cwcwidth import wcswidth , wcwidth
2524from itertools import chain
2625from 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
4241try :
7675xforms .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
9796def 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"""
0 commit comments