mirror of
https://github.com/python/cpython.git
synced 2025-11-01 14:11:41 +00:00
gh-138318, PyREPL: builtins should not be highlighted when used as attribute names (#138319)
This commit is contained in:
parent
921f61bd82
commit
7a3bca50e0
3 changed files with 30 additions and 2 deletions
|
|
@ -208,7 +208,10 @@ def gen_colors_from_token_stream(
|
|||
):
|
||||
span = Span.from_token(token, line_lengths)
|
||||
yield ColorSpan(span, "soft_keyword")
|
||||
elif token.string in BUILTINS:
|
||||
elif (
|
||||
token.string in BUILTINS
|
||||
and not (prev_token and prev_token.exact_type == T.DOT)
|
||||
):
|
||||
span = Span.from_token(token, line_lengths)
|
||||
yield ColorSpan(span, "builtin")
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
from unittest import TestCase
|
||||
|
||||
from _pyrepl.utils import str_width, wlen, prev_next_window
|
||||
from _pyrepl.utils import str_width, wlen, prev_next_window, gen_colors
|
||||
|
||||
|
||||
class TestUtils(TestCase):
|
||||
|
|
@ -60,3 +60,25 @@ def gen_raise():
|
|||
self.assertEqual(next(pnw), (3, 4, None))
|
||||
with self.assertRaises(ZeroDivisionError):
|
||||
next(pnw)
|
||||
|
||||
def test_gen_colors_keyword_highlighting(self):
|
||||
cases = [
|
||||
# no highlights
|
||||
("a.set", [(".", "op")]),
|
||||
("obj.list", [(".", "op")]),
|
||||
("obj.match", [(".", "op")]),
|
||||
("b. \\\n format", [(".", "op")]),
|
||||
# highlights
|
||||
("set", [("set", "builtin")]),
|
||||
("list", [("list", "builtin")]),
|
||||
(" \n dict", [("dict", "builtin")]),
|
||||
]
|
||||
for code, expected_highlights in cases:
|
||||
with self.subTest(code=code):
|
||||
colors = list(gen_colors(code))
|
||||
# Extract (text, tag) pairs for comparison
|
||||
actual_highlights = []
|
||||
for color in colors:
|
||||
span_text = code[color.span.start:color.span.end + 1]
|
||||
actual_highlights.append((span_text, color.tag))
|
||||
self.assertEqual(actual_highlights, expected_highlights)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,3 @@
|
|||
The default REPL now avoids highlighting built-in names (for instance :class:`set`
|
||||
or :func:`format`) when they are used as attribute names (for instance in ``value.set``
|
||||
or ``text.format``).
|
||||
Loading…
Add table
Add a link
Reference in a new issue