Skip to content

Commit 4d71bcf

Browse files
authored
Fix completions for methods starting with _ (#15106)
### Fixes #15104 ### Description The current regex pattern only matches attribute access where the attribute name starts with a letter ([a-zA-Z]). This PR modifies the pattern to handle attributes starting with `_` too.
2 parents 11e1f40 + 28d83b9 commit 4d71bcf

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

IPython/core/completer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2608,8 +2608,8 @@ def _determine_completion_context(self, line):
26082608
if re.search(r"(?<!\w)(?<!\d\.)([-+]?\d+\.(\d+)?)(?!\w)$", line):
26092609
return self._CompletionContextType.GLOBAL
26102610

2611-
# Handle all other attribute matches np.ran, d[0].k, (a,b).count
2612-
chain_match = re.search(r".*(.+(?<!\s)\.(?:[a-zA-Z]\w*)?)$", line)
2611+
# Handle all other attribute matches np.ran, d[0].k, (a,b).count, obj._private
2612+
chain_match = re.search(r".*(.+(?<!\s)\.(?:[a-zA-Z_]\w*)?)$", line)
26132613
if chain_match:
26142614
return self._CompletionContextType.ATTRIBUTE
26152615

tests/test_completer.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,6 +2068,27 @@ def _(expected):
20682068
a_matcher.matcher_priority = 3
20692069
_(["completion_a"])
20702070

2071+
def test_private_attr_completions(self):
2072+
ip = get_ipython()
2073+
ip.user_ns["Test"] = type("Test", (), {"_test1": 1, "__test2": 2})
2074+
ip.user_ns["t"] = ip.user_ns["Test"]()
2075+
ip.Completer.use_jedi = False
2076+
2077+
try:
2078+
with provisionalcompleter():
2079+
completions = list(ip.Completer.completions(text="t._", offset=3))
2080+
completion_texts = [c.text for c in completions]
2081+
2082+
assert (
2083+
"._test1" in completion_texts
2084+
), f"_test1 not found in {completion_texts}"
2085+
assert (
2086+
".__test2" in completion_texts
2087+
), f"__test2 not found in {completion_texts}"
2088+
finally:
2089+
del ip.user_ns["Test"]
2090+
del ip.user_ns["t"]
2091+
20712092

20722093
@pytest.mark.parametrize(
20732094
"use_jedi,evaluation",
@@ -2739,6 +2760,8 @@ def test_no_file_completions_in_attr_access(code):
27392760
("dict_with_dots = {'key.with.dots': value.attr", "attribute"),
27402761
("d[f'{a}']['{a.", "global"),
27412762
("ls .", "global"),
2763+
("t._", "attribute"),
2764+
("t.__a", "attribute"),
27422765
],
27432766
)
27442767
def test_completion_context(line, expected):

0 commit comments

Comments
 (0)