Skip to content

Commit e0d57fc

Browse files
[3.14] gh-151471: Colorize the soft keyword type in IDLE (GH-151700) (GH-157992)
`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 5f508b9 commit e0d57fc

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
@@ -42,6 +42,11 @@ def make_pat():
4242
]) +
4343
r"))"
4444
)
45+
type_softkw = (
46+
r"^[ \t]*" + # at beginning of line + possible indentation
47+
r"(?P<TYPE_SOFTKW>type)" +
48+
r"(?=[ \t]+(?!(?:" + "|".join(keyword.kwlist) + r")\b)[^\W\d])"
49+
)
4550
builtinlist = [str(name) for name in dir(builtins)
4651
if not name.startswith('_') and
4752
name not in keyword.kwlist]
@@ -54,7 +59,7 @@ def make_pat():
5459
dq3string = stringprefix + r'"""[^"\\]*((\\.|"(?!""))[^"\\]*)*(""")?'
5560
string = any("STRING", [sq3string, dq3string, sqstring, dqstring])
5661
prog = re.compile("|".join([
57-
builtin, comment, string, kw,
62+
type_softkw, builtin, comment, string, kw,
5863
match_softkw, case_default,
5964
case_softkw_and_pattern,
6065
any("SYNC", [r"\n"]),
@@ -70,6 +75,7 @@ def make_pat():
7075
"CASE_SOFTKW": "KEYWORD",
7176
"CASE_DEFAULT_UNDERSCORE": "KEYWORD",
7277
"CASE_SOFTKW2": "KEYWORD",
78+
"TYPE_SOFTKW": "KEYWORD",
7379
}
7480

7581

‎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)