11import sys
2- from typing import Any , Dict , Optional
2+ from codeop import CommandCompiler
3+ from typing import Any , Dict , Iterable , Optional , Tuple , Union
34
45from pygments .token import Generic , Token , Keyword , Name , Comment , String
56from pygments .token import Error , Literal , Number , Operator , Punctuation
6- from pygments .token import Whitespace
7+ from pygments .token import Whitespace , _TokenType
78from pygments .formatter import Formatter
89from pygments .lexers import get_lexer_by_name
10+ from curtsies .formatstring import FmtStr
911
1012from ..curtsiesfrontend .parse import parse
1113from ..repl import Interpreter as ReplInterpreter
@@ -43,7 +45,11 @@ class BPythonFormatter(Formatter):
4345 See the Pygments source for more info; it's pretty
4446 straightforward."""
4547
46- def __init__ (self , color_scheme , ** options ):
48+ def __init__ (
49+ self ,
50+ color_scheme : Dict [_TokenType , str ],
51+ ** options : Union [str , bool , None ],
52+ ) -> None :
4753 self .f_strings = {k : f"\x01 { v } " for k , v in color_scheme .items ()}
4854 super ().__init__ (** options )
4955
@@ -71,7 +77,7 @@ def __init__(
7177
7278 # typically changed after being instantiated
7379 # but used when interpreter used corresponding REPL
74- def write (err_line ) :
80+ def write (err_line : Union [ str , FmtStr ]) -> None :
7581 """Default stderr handler for tracebacks
7682
7783 Accepts FmtStrs so interpreters can output them"""
@@ -80,13 +86,14 @@ def write(err_line):
8086 self .write = write # type: ignore
8187 self .outfile = self
8288
83- def writetb (self , lines ) :
89+ def writetb (self , lines : Iterable [ str ]) -> None :
8490 tbtext = "" .join (lines )
8591 lexer = get_lexer_by_name ("pytb" )
8692 self .format (tbtext , lexer )
8793 # TODO for tracebacks get_lexer_by_name("pytb", stripall=True)
8894
89- def format (self , tbtext , lexer ):
95+ def format (self , tbtext : str , lexer : Any ) -> None :
96+ # FIXME: lexer should is a Lexer
9097 traceback_informative_formatter = BPythonFormatter (default_colors )
9198 traceback_code_formatter = BPythonFormatter ({Token : ("d" )})
9299 tokens = list (lexer .get_tokens (tbtext ))
@@ -112,7 +119,9 @@ def format(self, tbtext, lexer):
112119 assert cur_line == [], cur_line
113120
114121
115- def code_finished_will_parse (s , compiler ):
122+ def code_finished_will_parse (
123+ s : str , compiler : CommandCompiler
124+ ) -> Tuple [bool , bool ]:
116125 """Returns a tuple of whether the buffer could be complete and whether it
117126 will parse
118127
0 commit comments