diff --git a/jedi/CHANGELOG.md b/jedi/CHANGELOG.md index 1b1f97c..f5ebe75 100644 --- a/jedi/CHANGELOG.md +++ b/jedi/CHANGELOG.md @@ -6,6 +6,7 @@ ### Fixed - Fixed code completion for builtin types. +- Fixed code completion for names starting with `_`. ## 1.6.0 - 2022-12-09 diff --git a/jedi/src/pybricks_jedi/__init__.py b/jedi/src/pybricks_jedi/__init__.py index ba1c168..105c89b 100644 --- a/jedi/src/pybricks_jedi/__init__.py +++ b/jedi/src/pybricks_jedi/__init__.py @@ -174,6 +174,8 @@ PYBRICKS_TYPING = { "typing.Mapping.get", } +MICROPY_NOT_SUPPORTED_DUNDER = {"__doc__", "__package__"} + # Types from monaco editor @@ -302,10 +304,14 @@ class SignatureHelp(TypedDict): def _is_pybricks(c: Completion) -> bool: # filter all "private" names (leading underscore) - if (isinstance(c.name, str)) and c.name.startswith("_"): - return False + if c.name is not None: + if c.name.startswith("_") and c.module_name != "__main__": + return False - if isinstance(c.full_name, str): + if c.name in MICROPY_NOT_SUPPORTED_DUNDER: + return False + + if c.full_name is not None: # this catches things like `from __future__ import annotations` if c.full_name.startswith("_") and c.module_name != "__main__": return False diff --git a/jedi/tests/test_complete_builtins.py b/jedi/tests/test_complete_builtins.py index 36fa707..c585963 100644 --- a/jedi/tests/test_complete_builtins.py +++ b/jedi/tests/test_complete_builtins.py @@ -126,6 +126,7 @@ def test_empty_code(): "yield", "ZeroDivisionError", "zip", + "__name__", ] diff --git a/jedi/tests/test_complete_local_private.py b/jedi/tests/test_complete_local_private.py new file mode 100644 index 0000000..9d29650 --- /dev/null +++ b/jedi/tests/test_complete_local_private.py @@ -0,0 +1,32 @@ +import json +from pybricks_jedi import CompletionItem, complete + + +def test_get_completion_for_private_globals(): + code = """ +_X = 0 + +_ +""" + completions: list[CompletionItem] = json.loads(complete(code, 4, 2)) + assert [c["insertText"] for c in completions] == ["_X", "__name__"] + + +def test_get_completion_for_private_attributes(): + code = """ +class X: + def __init__(self): + self.public = 0 + self._protected = 0 + self.__private = 0 + +x = X() + +x. +""" + completions: list[CompletionItem] = json.loads(complete(code, 10, 3)) + assert [c["insertText"] for c in completions] == [ + "public", + "_protected", + "__init__", + ]