Skip to content

Commit bd7e492

Browse files
[3.15] gh-151471: Colorize the soft keyword type in IDLE (GH-151700) (#157991)
gh-151471: Colorize the soft keyword `type` in IDLE (GH-151700) `type` is now colored as a keyword when it starts a statement and is followed by a name, as in `type Point = tuple[float, float]`. It stays a builtin elsewhere, for example in `type = type(1)`, `type(x)` or `type in (int, str)`. (cherry picked from commit 68ed7a5) Co-authored-by: Locked-chess-official <13140752715@163.com>
1 parent 0a4f255 commit bd7e492

3 files changed

Lines changed: 13 additions & 2 deletions

File tree

‎Lib/idlelib/colorizer.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ def make_pat():
4747
]) +
4848
r"))"
4949
)
50+
type_softkw = (
51+
r"^[ \t]*" + # at beginning of line + possible indentation
52+
r"(?P<TYPE_SOFTKW>type)" +
53+
r"(?=[ \t]+(?!(?:" + "|".join(keyword.kwlist) + r")\b)[^\W\d])"
54+
)
5055
lazy_softkw = ( # lazy new in 3.15 (+ 2 lines below).
5156
r"^[ \t]*" + # at beginning of line + possible indentation
5257
r"(?P<LAZY_SOFTKW>lazy)" +
@@ -64,7 +69,7 @@ def make_pat():
6469
dq3string = stringprefix + r'"""[^"\\]*((\\.|"(?!""))[^"\\]*)*(""")?'
6570
string = any("STRING", [sq3string, dq3string, sqstring, dqstring])
6671
prog = re.compile("|".join([
67-
builtin, comment, string, kw,
72+
type_softkw, builtin, comment, string, kw,
6873
match_softkw, case_default,
6974
case_softkw_and_pattern, lazy_softkw,
7075
any("SYNC", [r"\n"]),
@@ -80,6 +85,7 @@ def make_pat():
8085
"CASE_SOFTKW": "KEYWORD",
8186
"CASE_DEFAULT_UNDERSCORE": "KEYWORD",
8287
"CASE_SOFTKW2": "KEYWORD",
88+
"TYPE_SOFTKW": "KEYWORD",
8389
"LAZY_SOFTKW": "KEYWORD",
8490
}
8591

‎Lib/idlelib/idle_test/test_colorizer.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ async def f(): await g()
5252
'''
5353
case _:'''
5454
"match x:"
55+
type Point = tuple[float, float]
56+
type = type(1)
5557
""")
5658

5759

@@ -404,6 +406,8 @@ def test_recolorize_main(self, mock_notify):
404406
('28.25', ('STRING',)), ('28.38', ('STRING',)),
405407
('30.0', ('STRING',)),
406408
('31.1', ('STRING',)),
409+
('32.0', ('KEYWORD',)),
410+
('33.0', ('BUILTIN',)), ('33.1', ('BUILTIN',)),
407411
# SYNC at the end of every line.
408412
('1.55', ('SYNC',)), ('2.50', ('SYNC',)), ('3.34', ('SYNC',)),
409413
)
@@ -434,7 +438,7 @@ def test_recolorize_main(self, mock_notify):
434438
eq(text.tag_nextrange('STRING', '8.12'), ('8.14', '8.17'))
435439
eq(text.tag_nextrange('STRING', '8.17'), ('8.19', '8.26'))
436440
eq(text.tag_nextrange('SYNC', '8.0'), ('8.26', '9.0'))
437-
eq(text.tag_nextrange('SYNC', '31.0'), ('31.10', '33.0'))
441+
eq(text.tag_nextrange('SYNC', '31.0'), ('31.10', '32.0'))
438442

439443
def _assert_highlighting(self, source, tag_ranges):
440444
"""Check highlighting of a given piece of code.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Colorize the soft keyword ``type`` in IDLE.

0 commit comments

Comments
 (0)