Skip to content

Commit 7d4f80d

Browse files
thomasballingersebastinas
authored andcommitted
good-enough solutions
1 parent 2de72b3 commit 7d4f80d

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

bpython/cli.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,12 @@ def print_line(
11011101

11021102
if self.highlighted_paren is not None:
11031103
# Clear previous highlighted paren
1104-
self.reprint_line(*self.highlighted_paren)
1104+
1105+
lineno = self.highlighted_paren[0]
1106+
tokens = self.highlighted_paren[1]
1107+
# mypy thinks tokens is List[Tuple[_TokenType, str]]
1108+
# but it is supposed to be MutableMapping[_TokenType, str]
1109+
self.reprint_line(lineno, tokens)
11051110
self.highlighted_paren = None
11061111

11071112
if self.config.syntax and (not self.paste_mode or newline):
@@ -1221,7 +1226,7 @@ def repl(self) -> Tuple[Any, ...]:
12211226
return self.exit_value
12221227

12231228
def reprint_line(
1224-
self, lineno: int, tokens: MutableMapping[_TokenType, str]
1229+
self, lineno: int, tokens: List[Tuple[_TokenType, str]]
12251230
) -> None:
12261231
"""Helper function for paren highlighting: Reprint line at offset
12271232
`lineno` in current input buffer."""

bpython/curtsiesfrontend/repl.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
import unicodedata
1414
from enum import Enum
1515
from types import TracebackType
16-
from typing import Dict, Any, List, Optional, Tuple, Union, cast, Type
16+
from typing import Dict, Any, List, Optional, Tuple, Union, cast, Type, TYPE_CHECKING
1717
from .._typing_compat import Literal
1818

19+
if TYPE_CHECKING:
20+
from ..repl import Interpreter
21+
1922
import blessings
2023
import greenlet
2124
from curtsies import (
@@ -357,7 +360,9 @@ def __init__(
357360
)
358361
self.edit_keys = edit_keys.mapping_with_config(config, key_dispatch)
359362
logger.debug("starting parent init")
360-
super().__init__(interp, config)
363+
# interp is a subclass of repl.Interpreter, so it definitely,
364+
# implements the methods of Interpreter!
365+
super().__init__(cast('Interpreter', interp), config)
361366

362367
self.formatter = BPythonFormatter(config.color_scheme)
363368

bpython/repl.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
MutableMapping,
4949
Callable,
5050
Dict,
51+
TYPE_CHECKING,
5152
)
5253
from ._typing_compat import Literal
5354

@@ -61,7 +62,10 @@
6162
have_pyperclip = False
6263

6364
from . import autocomplete, inspection, simpleeval
64-
from .cli import Statusbar
65+
66+
if TYPE_CHECKING:
67+
from .cli import Statusbar
68+
6569
from .config import getpreferredencoding, Config
6670
from .formatter import Parenthesis
6771
from .history import History
@@ -365,7 +369,7 @@ def clear(self) -> None:
365369

366370

367371
class Interaction:
368-
def __init__(self, config: Config, statusbar: Optional[Statusbar] = None):
372+
def __init__(self, config: Config, statusbar: Optional['Statusbar'] = None):
369373
self.config = config
370374

371375
if statusbar:
@@ -418,14 +422,36 @@ class Repl:
418422
XXX Subclasses should implement echo, current_line, cw
419423
"""
420424

425+
@abstractmethod
426+
@property
427+
def current_line(self):
428+
pass
429+
430+
@abstractmethod
431+
@property
432+
def cursor_offset(self):
433+
pass
434+
435+
@abstractmethod
436+
def reevaluate(self):
437+
pass
438+
439+
@abstractmethod
440+
def reprint_line(
441+
self, lineno: int, tokens: List[Tuple[_TokenType, str]]
442+
) -> None:
443+
pass
444+
445+
# not actually defined, subclasses must define
446+
cpos: int
447+
421448
def __init__(self, interp: Interpreter, config: Config):
422449
"""Initialise the repl.
423450
424451
interp is a Python code.InteractiveInterpreter instance
425452
426453
config is a populated bpython.config.Struct.
427454
"""
428-
429455
self.config = config
430456
self.cut_buffer = ""
431457
self.buffer: List[str] = []

0 commit comments

Comments
 (0)