mirror of
https://github.com/pybricks/pybricks-api.git
synced 2026-09-11 17:14:41 +00:00
jedi: fix code completion for local _*
This fixes code completion for identifier names starting with "_" in
the local ("__main__") file.
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
|
||||
### Fixed
|
||||
- Fixed code completion for builtin types.
|
||||
- Fixed code completion for names starting with `_`.
|
||||
|
||||
## 1.6.0 - 2022-12-09
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -126,6 +126,7 @@ def test_empty_code():
|
||||
"yield",
|
||||
"ZeroDivisionError",
|
||||
"zip",
|
||||
"__name__",
|
||||
]
|
||||
|
||||
|
||||
|
||||
@@ -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__",
|
||||
]
|
||||
Reference in New Issue
Block a user