Skip to content

Commit 26cfb17

Browse files
00ll00picnixz
andauthored
gh-138239: Fix incorrect highlighting of "type" in type statements in the REPL (GH-138241)
Co-authored-by: Bénédikt Tran <[email protected]>
1 parent 07d0b95 commit 26cfb17

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

Lib/_pyrepl/utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,12 @@ def is_soft_keyword_used(*tokens: TI | None) -> bool:
263263
return True
264264
case (TI(string="case"), TI(string="_"), TI(string=":")):
265265
return True
266+
case (
267+
None | TI(T.NEWLINE) | TI(T.INDENT) | TI(T.DEDENT) | TI(string=":"),
268+
TI(string="type"),
269+
TI(T.NAME, string=s)
270+
):
271+
return not keyword.iskeyword(s)
266272
case _:
267273
return False
268274

Lib/test/test_pyrepl/test_reader.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ def funct(case: str = sys.platform) -> None:
378378
case "ios" | "android":
379379
print("on the phone")
380380
case _: print('arms around', match.group(1))
381+
type type = type[type]
381382
"""
382383
)
383384
expected = dedent(
@@ -397,21 +398,22 @@ def funct(case: str = sys.platform) -> None:
397398
{K}case{z} {s}"ios"{z} {o}|{z} {s}"android"{z}{o}:{z}
398399
{b}print{z}{o}({z}{s}"on the phone"{z}{o}){z}
399400
{K}case{z} {K}_{z}{o}:{z} {b}print{z}{o}({z}{s}'arms around'{z}{o},{z} match{o}.{z}group{o}({z}{n}1{z}{o}){z}{o}){z}
401+
{K}type{z} {b}type{z} {o}={z} {b}type{z}{o}[{z}{b}type{z}{o}]{z}
400402
"""
401403
)
402404
expected_sync = expected.format(a="", **colors)
403405
events = code_to_events(code)
404406
reader, _ = handle_all_events(events)
405407
self.assert_screen_equal(reader, code, clean=True)
406408
self.assert_screen_equal(reader, expected_sync)
407-
self.assertEqual(reader.pos, 396)
408-
self.assertEqual(reader.cxy, (0, 15))
409+
self.assertEqual(reader.pos, 419)
410+
self.assertEqual(reader.cxy, (0, 16))
409411

410412
async_msg = "{k}async{z} ".format(**colors)
411413
expected_async = expected.format(a=async_msg, **colors)
412414
more_events = itertools.chain(
413415
code_to_events(code),
414-
[Event(evt="key", data="up", raw=bytearray(b"\x1bOA"))] * 14,
416+
[Event(evt="key", data="up", raw=bytearray(b"\x1bOA"))] * 15,
415417
code_to_events("async "),
416418
)
417419
reader, _ = handle_all_events(more_events)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The REPL now highlights :keyword:`type` as a soft keyword
2+
in :ref:`type statements <type>`.

0 commit comments

Comments
 (0)