5252import struct
5353import sys
5454import time
55- from typing import Iterator , NoReturn , List , MutableMapping , Any , Callable , TypeVar , cast
55+ from typing import Iterator , NoReturn , List , MutableMapping , Any , Callable , TypeVar , cast , IO , Iterable , Optional
5656import unicodedata
5757from dataclasses import dataclass
5858
7070from .formatter import BPythonFormatter
7171
7272# This for config
73- from .config import getpreferredencoding
73+ from .config import getpreferredencoding , Config
7474
7575# This for keys
7676from .keys import cli_key_dispatch as key_dispatch
8888
8989# --- module globals ---
9090stdscr = None
91- colors = None
91+ colors : Optional [ MutableMapping [ str , int ]] = None
9292
9393DO_RESIZE = False
9494# ---
@@ -135,17 +135,17 @@ class FakeStream:
135135 """Provide a fake file object which calls functions on the interface
136136 provided."""
137137
138- def __init__ (self , interface , get_dest ) :
138+ def __init__ (self , interface : 'CLIRepl' , get_dest : IO [ str ]) -> None :
139139 self .encoding : str = getpreferredencoding ()
140140 self .interface = interface
141141 self .get_dest = get_dest
142142
143143 @forward_if_not_current
144- def write (self , s ) -> None :
144+ def write (self , s : str ) -> None :
145145 self .interface .write (s )
146146
147147 @forward_if_not_current
148- def writelines (self , l ) -> None :
148+ def writelines (self , l : Iterable [ str ] ) -> None :
149149 for s in l :
150150 self .write (s )
151151
@@ -160,7 +160,7 @@ def flush(self) -> None:
160160class FakeStdin :
161161 """Provide a fake stdin type for things like raw_input() etc."""
162162
163- def __init__ (self , interface ) -> None :
163+ def __init__ (self , interface : 'CLIRepl' ) -> None :
164164 """Take the curses Repl on init and assume it provides a get_key method
165165 which, fortunately, it does."""
166166
@@ -171,19 +171,19 @@ def __init__(self, interface) -> None:
171171 def __iter__ (self ) -> Iterator :
172172 return iter (self .readlines ())
173173
174- def flush (self ):
174+ def flush (self ) -> None :
175175 """Flush the internal buffer. This is a no-op. Flushing stdin
176176 doesn't make any sense anyway."""
177177
178- def write (self , value ) -> NoReturn :
178+ def write (self , value : str ) -> NoReturn :
179179 # XXX IPython expects sys.stdin.write to exist, there will no doubt be
180180 # others, so here's a hack to keep them happy
181181 raise OSError (errno .EBADF , "sys.stdin is read-only" )
182182
183183 def isatty (self ) -> bool :
184184 return True
185185
186- def readline (self , size = - 1 ):
186+ def readline (self , size : int = - 1 ) -> str :
187187 """I can't think of any reason why anything other than readline would
188188 be useful in the context of an interactive interpreter so this is the
189189 only one I've done anything with. The others are just there in case
@@ -228,7 +228,7 @@ def readline(self, size=-1):
228228
229229 return buffer
230230
231- def read (self , size = None ):
231+ def read (self , size : Optional [ int ] = None ) -> str :
232232 if size == 0 :
233233 return ""
234234
@@ -243,7 +243,7 @@ def read(self, size=None):
243243
244244 return "" .join (data )
245245
246- def readlines (self , size = - 1 ):
246+ def readlines (self , size : int = - 1 ) -> List [ str ] :
247247 return list (iter (self .readline , "" ))
248248
249249
@@ -260,17 +260,20 @@ def readlines(self, size=-1):
260260# the addstr stuff to a higher level.
261261#
262262
263-
264- def get_color (config , name ):
263+ # Have to ignore the return type on this one because the colors variable
264+ # is Optional[MutableMapping[str, int]] but for the purposes of this
265+ # function it can't be None
266+ def get_color (config : Config , name : str ) -> int : # type: ignore[return]
265267 global colors
266- return colors [config .color_scheme [name ].lower ()]
268+ if colors :
269+ return colors [config .color_scheme [name ].lower ()]
267270
268271
269- def get_colpair (config , name ) :
272+ def get_colpair (config : Config , name : str ) -> int :
270273 return curses .color_pair (get_color (config , name ) + 1 )
271274
272275
273- def make_colors (config ) :
276+ def make_colors (config : Config ) -> MutableMapping [ str , int ] :
274277 """Init all the colours in curses and bang them into a dictionary"""
275278
276279 # blacK, Red, Green, Yellow, Blue, Magenta, Cyan, White, Default:
0 commit comments