Skip to content

Commit b2f9f6c

Browse files
committed
fix: address comments
Signed-off-by: yihong0618 <[email protected]>
1 parent 3fa01f2 commit b2f9f6c

File tree

4 files changed

+88
-17
lines changed

4 files changed

+88
-17
lines changed

Lib/_pyrepl/console.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ class Event:
4747

4848
@dataclass
4949
class Console(ABC):
50-
posxy: tuple[int, int] = field(default=(0, 0), repr=False)
51-
screen: list[str] = field(default_factory=list, repr=False)
50+
posxy: tuple[int, int]
51+
screen: list[str] = field(default_factory=list)
5252
height: int = 25
5353
width: int = 80
5454

Lib/_pyrepl/reader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ class Reader:
186186
that we're done.
187187
"""
188188

189-
console: console.Console
189+
console: console.Console = field(repr=False)
190190

191191
## state
192192
buffer: list[str] = field(default_factory=list)

Lib/test/test_pyrepl/test_pyrepl.py

Lines changed: 83 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
multiline_input,
2727
code_to_events,
2828
)
29-
from _pyrepl.console import Event
29+
from typing import IO
30+
31+
from _pyrepl.console import Console, Event
3032
from _pyrepl._module_completer import (
3133
ImportParser,
3234
ModuleCompleter,
@@ -1416,22 +1418,91 @@ def test_dumb_terminal_exits_cleanly(self):
14161418

14171419
class TestConsoleRepr(TestCase):
14181420

1419-
def test_console_repr_with_missing_attributes(self):
1420-
from _pyrepl.unix_console import UnixConsole
1421-
console = UnixConsole()
1421+
class _StubConsole(Console):
1422+
def __init__(
1423+
self,
1424+
f_in: int | IO[bytes] = 0,
1425+
f_out: int | IO[bytes] = 1,
1426+
term: str = "",
1427+
encoding: str = "",
1428+
) -> None:
1429+
super().__init__(f_in, f_out, term, encoding)
1430+
self.height = 25
1431+
self.width = 80
1432+
self.screen = []
1433+
1434+
def refresh(self, screen: list[str], xy: tuple[int, int]) -> None:
1435+
pass
1436+
1437+
def prepare(self) -> None:
1438+
pass
1439+
1440+
def restore(self) -> None:
1441+
pass
1442+
1443+
def move_cursor(self, x: int, y: int) -> None:
1444+
pass
1445+
1446+
def set_cursor_vis(self, visible: bool) -> None:
1447+
pass
1448+
1449+
def getheightwidth(self) -> tuple[int, int]:
1450+
return self.height, self.width
1451+
1452+
def get_event(self, block: bool = True) -> Event | None:
1453+
return None
1454+
1455+
def push_char(self, char: int | bytes) -> None:
1456+
pass
1457+
1458+
def beep(self) -> None:
1459+
pass
1460+
1461+
def clear(self) -> None:
1462+
pass
1463+
1464+
def finish(self) -> None:
1465+
pass
1466+
1467+
def flushoutput(self) -> None:
1468+
pass
1469+
1470+
def forgetinput(self) -> None:
1471+
pass
1472+
1473+
def getpending(self) -> Event:
1474+
return Event("key", "", b"")
1475+
1476+
def wait(self, timeout: float | None = None) -> bool:
1477+
return False
1478+
1479+
def repaint(self) -> None:
1480+
pass
1481+
1482+
@property
1483+
def input_hook(self):
1484+
return None
1485+
1486+
def test_reader_repr_avoids_console_field(self):
1487+
console = self._StubConsole()
1488+
config = ReadlineConfig(readline_completer=None)
1489+
1490+
reader = ReadlineAlikeReader(console=console, config=config)
14221491

1423-
repr_str = repr(console)
1492+
repr_str = repr(reader)
14241493

14251494
self.assertIsInstance(repr_str, str)
1426-
self.assertIn("UnixConsole", repr_str)
1495+
self.assertNotIn("console=", repr_str)
14271496

1428-
def test_readline_wrapper_repr_after_import(self):
1429-
import _pyrepl.readline
1497+
def test_wrapper_reader_repr_before_prepare(self):
1498+
with patch("_pyrepl.readline.Console", self._StubConsole), patch(
1499+
"_pyrepl.readline.os.dup",
1500+
side_effect=lambda fd: fd,
1501+
):
1502+
wrapper = _ReadlineWrapper()
1503+
reader = wrapper.get_reader()
14301504

1431-
wrapper = _pyrepl.readline._wrapper
1432-
if wrapper is not None and wrapper.reader is not None:
1433-
repr_str = repr(wrapper.reader)
1434-
self.assertIsInstance(repr_str, str)
1505+
self.assertIsInstance(repr(reader), str)
14351506

14361507

14371508
@skipUnless(pty, "requires pty")
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Fix: make dataclass Console init right to avoid has no attribute 'posxy'
2-
error
1+
Avoid an ``AttributeError`` when calling :func:`repr` on
2+
``_pyrepl.readline._wrapper.reader`` before the console has been prepared.

0 commit comments

Comments
 (0)