Skip to content

Commit 735b44b

Browse files
committed
fix: reformat
1 parent 80ac427 commit 735b44b

File tree

7 files changed

+177
-60
lines changed

7 files changed

+177
-60
lines changed

pylsp/plugins/hover.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ def pylsp_hover(config, document, position):
3030
supported_markup_kinds = hover_capabilities.get("contentFormat", ["markdown"])
3131
preferred_markup_kind = _utils.choose_markup_kind(supported_markup_kinds)
3232

33-
doc = _utils.format_docstring(definition.docstring(raw=True), preferred_markup_kind)["value"]
33+
doc = _utils.format_docstring(
34+
definition.docstring(raw=True), preferred_markup_kind
35+
)["value"]
3436

3537
# Find first exact matching signature
3638
signature = next(
@@ -44,14 +46,14 @@ def pylsp_hover(config, document, position):
4446

4547
contents = []
4648
if signature:
47-
contents.append({
48-
'language': 'python',
49-
'value': signature,
50-
})
49+
contents.append(
50+
{
51+
"language": "python",
52+
"value": signature,
53+
}
54+
)
5155

5256
if doc:
5357
contents.append(doc)
5458

55-
return {
56-
"contents": contents or ''
57-
}
59+
return {"contents": contents or ""}

pylsp/plugins/jedi_completion.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -141,22 +141,24 @@ def pylsp_completions(config, document, position):
141141

142142
return ready_completions or None
143143

144+
144145
@hookimpl
145146
def pylsp_completion_detail(config, item):
146147
d = COMPLETION_CACHE.get(item)
147148
if d:
148-
completion = {
149-
'label': '', #_label(d),
150-
'kind': _TYPE_MAP.get(d.type),
151-
'detail': '', #_detail(d),
152-
'documentation': _utils.format_docstring(d.docstring()),
153-
'sortText': '', #_sort_text(d),
154-
'insertText': d.name
155-
}
156-
return completion
149+
completion = {
150+
"label": "", # _label(d),
151+
"kind": _TYPE_MAP.get(d.type),
152+
"detail": "", # _detail(d),
153+
"documentation": _utils.format_docstring(d.docstring()),
154+
"sortText": "", # _sort_text(d),
155+
"insertText": d.name,
156+
}
157+
return completion
157158
else:
158-
log.info('Completion missing')
159-
return None
159+
log.info("Completion missing")
160+
return None
161+
160162

161163
@hookimpl
162164
def pylsp_completion_item_resolve(

pylsp/plugins/preload_imports.py

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,45 @@
88
log = logging.getLogger(__name__)
99

1010
MODULES = [
11-
"numpy", "tensorflow", "sklearn", "array", "binascii", "cmath", "collections",
12-
"datetime", "errno", "exceptions", "gc", "imageop", "imp", "itertools",
13-
"marshal", "math", "matplotlib", "mmap", "mpmath", "msvcrt", "networkx", "nose", "nt",
14-
"operator", "os", "os.path", "pandas", "parser", "scipy", "signal",
15-
"skimage", "statsmodels", "strop", "sympy", "sys", "thread", "time", "wx", "zlib"
11+
"numpy",
12+
"tensorflow",
13+
"sklearn",
14+
"array",
15+
"binascii",
16+
"cmath",
17+
"collections",
18+
"datetime",
19+
"errno",
20+
"exceptions",
21+
"gc",
22+
"imageop",
23+
"imp",
24+
"itertools",
25+
"marshal",
26+
"math",
27+
"matplotlib",
28+
"mmap",
29+
"mpmath",
30+
"msvcrt",
31+
"networkx",
32+
"nose",
33+
"nt",
34+
"operator",
35+
"os",
36+
"os.path",
37+
"pandas",
38+
"parser",
39+
"scipy",
40+
"signal",
41+
"skimage",
42+
"statsmodels",
43+
"strop",
44+
"sympy",
45+
"sys",
46+
"thread",
47+
"time",
48+
"wx",
49+
"zlib",
1650
]
1751

1852

pylsp/python_lsp.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,29 +49,29 @@ def handle(self) -> None:
4949
self.SHUTDOWN_CALL()
5050

5151
def auth(self, cb):
52-
token = ''
52+
token = ""
5353
if "JUPYTER_TOKEN" in os.environ:
54-
token = os.environ["JUPYTER_TOKEN"]
54+
token = os.environ["JUPYTER_TOKEN"]
5555
else:
56-
log.warn('! Missing jupyter token !')
56+
log.warn("! Missing jupyter token !")
5757

5858
data = self.rfile.readline()
5959
try:
60-
auth_req = json.loads(data.decode().split('\n')[0])
60+
auth_req = json.loads(data.decode().split("\n")[0])
6161
except:
62-
log.error('Error parsing authentication message')
63-
auth_error_msg = { 'msg': 'AUTH_ERROR' }
62+
log.error("Error parsing authentication message")
63+
auth_error_msg = {"msg": "AUTH_ERROR"}
6464
self.wfile.write(json.dumps(auth_error_msg).encode())
6565
return
6666

6767
hashed_token = sha256(token.encode()).hexdigest()
68-
if auth_req.get('token') == hashed_token:
69-
auth_success_msg = { 'msg': 'AUTH_SUCCESS' }
68+
if auth_req.get("token") == hashed_token:
69+
auth_success_msg = {"msg": "AUTH_SUCCESS"}
7070
self.wfile.write(json.dumps(auth_success_msg).encode())
7171
cb()
7272
else:
73-
log.info('Failed to authenticate: invalid credentials')
74-
auth_invalid_msg = { 'msg': 'AUTH_INVALID_CRED' }
73+
log.info("Failed to authenticate: invalid credentials")
74+
auth_invalid_msg = {"msg": "AUTH_INVALID_CRED"}
7575
self.wfile.write(json.dumps(auth_invalid_msg).encode())
7676

7777

@@ -434,7 +434,7 @@ def completions(self, doc_uri, position):
434434
return {"isIncomplete": True, "items": flatten(completions)}
435435

436436
def completion_detail(self, item):
437-
detail = self._hook('pylsp_completion_detail', item=item)
437+
detail = self._hook("pylsp_completion_detail", item=item)
438438
return detail
439439

440440
def completion_item_resolve(self, completion_item):
@@ -580,7 +580,7 @@ def folding(self, doc_uri):
580580
return flatten(self._hook("pylsp_folding_range", doc_uri))
581581

582582
def m_completion_item__resolve(self, **completionItem):
583-
return self.completion_detail(completionItem.get('label'))
583+
return self.completion_detail(completionItem.get("label"))
584584

585585
def m_notebook_document__did_open(
586586
self, notebookDocument=None, cellTextDocuments=None, **_kwargs

pylsp/workspace.py

Lines changed: 74 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -394,17 +394,69 @@ def close(self) -> None:
394394

395395

396396
class Document:
397-
DO_NOT_PRELOAD_MODULES = ['attrs', 'backcall', 'bleach', 'certifi', 'chardet', 'cycler', 'decorator', 'defusedxml',
398-
'docopt', 'entrypoints', 'idna', 'importlib-metadata', 'ipykernel', 'ipython-genutils',
399-
'ipython', 'ipywidgets', 'jedi', 'jinja2', 'joblib', 'jsonschema', 'jupyter-client',
400-
'jupyter-core', 'markupsafe', 'mistune', 'nbconvert', 'nbformat', 'notebook', 'packaging',
401-
'pandocfilters', 'parso', 'pexpect', 'pickleshare', 'pip', 'pipreqs', 'pluggy',
402-
'prometheus-client', 'prompt-toolkit', 'ptyprocess', 'pygments', 'pyparsing',
403-
'pyrsistent', 'python-dateutil', 'python-jsonrpc-server', 'python-language-server',
404-
'pytz', 'pyzmq', 'send2trash', 'setuptools', 'six', 'terminado', 'testpath',
405-
'threadpoolctl', 'tornado', 'traitlets', 'ujson', 'wcwidth', 'webencodings', 'wheel',
406-
'widgetsnbextension', 'yarg', 'zipp']
407-
397+
DO_NOT_PRELOAD_MODULES = [
398+
"attrs",
399+
"backcall",
400+
"bleach",
401+
"certifi",
402+
"chardet",
403+
"cycler",
404+
"decorator",
405+
"defusedxml",
406+
"docopt",
407+
"entrypoints",
408+
"idna",
409+
"importlib-metadata",
410+
"ipykernel",
411+
"ipython-genutils",
412+
"ipython",
413+
"ipywidgets",
414+
"jedi",
415+
"jinja2",
416+
"joblib",
417+
"jsonschema",
418+
"jupyter-client",
419+
"jupyter-core",
420+
"markupsafe",
421+
"mistune",
422+
"nbconvert",
423+
"nbformat",
424+
"notebook",
425+
"packaging",
426+
"pandocfilters",
427+
"parso",
428+
"pexpect",
429+
"pickleshare",
430+
"pip",
431+
"pipreqs",
432+
"pluggy",
433+
"prometheus-client",
434+
"prompt-toolkit",
435+
"ptyprocess",
436+
"pygments",
437+
"pyparsing",
438+
"pyrsistent",
439+
"python-dateutil",
440+
"python-jsonrpc-server",
441+
"python-language-server",
442+
"pytz",
443+
"pyzmq",
444+
"send2trash",
445+
"setuptools",
446+
"six",
447+
"terminado",
448+
"testpath",
449+
"threadpoolctl",
450+
"tornado",
451+
"traitlets",
452+
"ujson",
453+
"wcwidth",
454+
"webencodings",
455+
"wheel",
456+
"widgetsnbextension",
457+
"yarg",
458+
"zipp",
459+
]
408460

409461
def __init__(
410462
self,
@@ -431,14 +483,20 @@ def __init__(
431483
self._rope_project_builder = rope_project_builder
432484
self._lock = RLock()
433485

434-
jedi.settings.cache_directory = '.cache/jedi/'
486+
jedi.settings.cache_directory = ".cache/jedi/"
435487
jedi.settings.use_filesystem_cache = True
436488
jedi.settings.auto_import_modules = self._get_auto_import_modules()
437489

438490
def _get_auto_import_modules(self):
439-
installed_packages_list = [dist.metadata['Name'] for dist in importlib.metadata.distributions()]
440-
auto_import_modules = [pkg for pkg in installed_packages_list if pkg not in self.DO_NOT_PRELOAD_MODULES]
441-
return auto_import_modules
491+
installed_packages_list = [
492+
dist.metadata["Name"] for dist in importlib.metadata.distributions()
493+
]
494+
auto_import_modules = [
495+
pkg
496+
for pkg in installed_packages_list
497+
if pkg not in self.DO_NOT_PRELOAD_MODULES
498+
]
499+
return auto_import_modules
442500

443501
def __str__(self):
444502
return str(self.uri)
@@ -584,7 +642,7 @@ def jedi_script(self, position=None, use_document_path=False):
584642
kwargs = {
585643
"code": self.source,
586644
"path": self.path,
587-
'namespaces': [__main__.__dict__]
645+
"namespaces": [__main__.__dict__],
588646
}
589647

590648
if position:

test/plugins/test_hover.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,28 @@ def get_hover_text(result):
4949
return contents
5050

5151
contents = "NumPy\n=====\n\nProvides\n"
52-
assert contents in get_hover_text(pylsp_hover(doc._config, doc, numpy_hov_position_1))
52+
assert contents in get_hover_text(
53+
pylsp_hover(doc._config, doc, numpy_hov_position_1)
54+
)
5355

5456
contents = "NumPy\n=====\n\nProvides\n"
55-
assert contents in get_hover_text(pylsp_hover(doc._config, doc, numpy_hov_position_2))
57+
assert contents in get_hover_text(
58+
pylsp_hover(doc._config, doc, numpy_hov_position_2)
59+
)
5660

5761
contents = "NumPy\n=====\n\nProvides\n"
58-
assert contents in get_hover_text(pylsp_hover(doc._config, doc, numpy_hov_position_3))
62+
assert contents in get_hover_text(
63+
pylsp_hover(doc._config, doc, numpy_hov_position_3)
64+
)
5965

6066
# https://github.com/davidhalter/jedi/issues/1746
6167
import numpy as np
6268

6369
if np.lib.NumpyVersion(np.__version__) < "1.20.0":
6470
contents = "Trigonometric sine, element-wise.\n\n"
65-
assert contents in get_hover_text(pylsp_hover(doc._config, doc, numpy_sin_hov_position))
71+
assert contents in get_hover_text(
72+
pylsp_hover(doc._config, doc, numpy_sin_hov_position)
73+
)
6674

6775

6876
def test_hover(workspace) -> None:
@@ -78,7 +86,10 @@ def test_hover(workspace) -> None:
7886
assert isinstance(result["contents"], list)
7987
assert len(result["contents"]) == 2
8088
# First item is the signature code block
81-
assert result["contents"][0] == {"language": "python", "value": "main(a: float, b: float)"}
89+
assert result["contents"][0] == {
90+
"language": "python",
91+
"value": "main(a: float, b: float)",
92+
}
8293
# Second item is the docstring
8394
assert "hello world" in result["contents"][1]
8495

@@ -99,7 +110,10 @@ def test_hover_signature_formatting(workspace) -> None:
99110
assert len(result["contents"]) == 2
100111
# Due to changes in our fork, hover no longer applies signature formatting
101112
# It just returns the raw signature from Jedi
102-
assert result["contents"][0] == {"language": "python", "value": "main(a: float, b: float)"}
113+
assert result["contents"][0] == {
114+
"language": "python",
115+
"value": "main(a: float, b: float)",
116+
}
103117
# Second item is the docstring
104118
assert "hello world" in result["contents"][1]
105119

@@ -116,7 +130,10 @@ def test_hover_signature_formatting_opt_out(workspace) -> None:
116130
assert isinstance(result["contents"], list)
117131
assert len(result["contents"]) == 2
118132
# First item is the signature code block without multiline formatting
119-
assert result["contents"][0] == {"language": "python", "value": "main(a: float, b: float)"}
133+
assert result["contents"][0] == {
134+
"language": "python",
135+
"value": "main(a: float, b: float)",
136+
}
120137
# Second item is the docstring
121138
assert "hello world" in result["contents"][1]
122139

@@ -148,7 +165,10 @@ def foo():
148165
# The result should be either a list with signature and/or docstring, or empty string
149166
if isinstance(contents, list) and len(contents) > 0:
150167
# Convert list to string for checking
151-
contents_str = ' '.join(str(item) if not isinstance(item, dict) else item.get('value', '') for item in contents)
168+
contents_str = " ".join(
169+
str(item) if not isinstance(item, dict) else item.get("value", "")
170+
for item in contents
171+
)
152172
assert "A docstring for foo." in contents_str
153173
else:
154174
# If Jedi can't resolve the definition (e.g., in test environment), the hover may be empty

test/plugins/test_jedi_rename.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def tmp_workspace(temp_workspace_factory):
3232
{DOC_NAME: DOC, DOC_NAME_EXTRA: DOC_EXTRA, DOC_NAME_SIMPLE: DOC_SIMPLE}
3333
)
3434

35+
3536
@pytest.mark.skip(reason="Does not work with jedi.Interpreter mode (commit cc0efee)")
3637
def test_jedi_rename(tmp_workspace, config) -> None:
3738
# rename the `Test1` class

0 commit comments

Comments
 (0)