Skip to content

Commit 082585e

Browse files
authored
Suppress file completions in context of attributes/methods (ipython#15036)
### Fixes ipython#15031 ### Description Suppress `file_matcher` when matching `attribute` \\ `methods`.
2 parents d32e750 + 4a896b3 commit 082585e

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

IPython/core/completer.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2481,7 +2481,7 @@ def _jedi_matcher(self, context: CompletionContext) -> _JediMatcherResult:
24812481
return {
24822482
"completions": matches,
24832483
# static analysis should not suppress other matchers
2484-
"suppress": False,
2484+
"suppress": {_get_matcher_id(self.file_matcher)} if matches else False,
24852485
}
24862486

24872487
def _jedi_matches(
@@ -2741,9 +2741,12 @@ def python_matcher(self, context: CompletionContext) -> SimpleMatcherResult:
27412741
is None
27422742
)
27432743
matches = filter(no__name, matches)
2744-
return _convert_matcher_v1_result_to_v2(
2744+
matches = _convert_matcher_v1_result_to_v2(
27452745
matches, type="attribute", fragment=fragment
27462746
)
2747+
if matches["completions"]:
2748+
matches["suppress"] = {_get_matcher_id(self.file_matcher)}
2749+
return matches
27472750
except NameError:
27482751
# catches <undefined attributes>.<tab>
27492752
return SimpleMatcherResult(completions=[], suppress=False)

tests/test_completer.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2332,6 +2332,40 @@ def test_undefined_variables(use_jedi, evaluation, code, insert_text):
23322332
assert len(match) == 1, message_on_fail
23332333

23342334

2335+
@pytest.mark.parametrize(
2336+
"code",
2337+
[
2338+
"\n".join(
2339+
[
2340+
"def my_test() -> float:",
2341+
" return 1.1",
2342+
"my_test().",
2343+
]
2344+
),
2345+
"\n".join(
2346+
[
2347+
"class MyClass():",
2348+
" b: list[str]",
2349+
"x = MyClass()",
2350+
"x.b[0].",
2351+
]
2352+
),
2353+
],
2354+
)
2355+
def test_no_file_completions_in_attr_access(code):
2356+
"""Test that files are not suggested during attribute/method completion."""
2357+
with TemporaryWorkingDirectory():
2358+
open(".hidden", "w", encoding="utf-8").close()
2359+
offset = len(code)
2360+
for use_jedi in (True, False):
2361+
with provisionalcompleter(), jedi_status(use_jedi):
2362+
completions = list(ip.Completer.completions(text=code, offset=offset))
2363+
matches = [c for c in completions if c.text.lstrip(".") == "hidden"]
2364+
assert (
2365+
len(matches) == 0
2366+
), f"File '.hidden' should not appear in attribute completion"
2367+
2368+
23352369
@pytest.mark.parametrize(
23362370
"line,expected",
23372371
[

0 commit comments

Comments
 (0)