Skip to content

Rename to pylsp prefix #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
include README.rst
include mypy_ls/_version.py
14 changes: 7 additions & 7 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Mypy plugin for PYLS
Mypy plugin for PYLSP
======================

.. image:: https://badge.fury.io/py/mypy-ls.svg
:target: https://badge.fury.io/py/mypy-ls
.. image:: https://badge.fury.io/py/pylsp-mypy.svg
:target: https://badge.fury.io/py/pylsp-mypy

.. image:: https://github.com/Richardk2n/pyls-mypy/workflows/Python%20package/badge.svg?branch=master
:target: https://github.com/Richardk2n/pyls-mypy/
.. image:: https://github.com/Richardk2n/pylsp-mypy/workflows/Python%20package/badge.svg?branch=master
:target: https://github.com/Richardk2n/pylsp-mypy/

This is a plugin for the `Python LSP Server`_.

Expand All @@ -19,7 +19,7 @@ Installation

Install into the same virtualenv as python-lsp-server itself.

``pip install mypy-ls``
``pip install pylsp-mypy``

Configuration
-------------
Expand All @@ -33,7 +33,7 @@ Configuration
``strict`` (default is False) refers to the ``strict`` option of ``mypy``.
This option often is too strict to be useful.

Depending on your editor, the configuration (found in a file called mypy-ls.cfg in your workspace or a parent directory) should be roughly like this for a standard configuration:
Depending on your editor, the configuration (found in a file called pylsp-mypy.cfg in your workspace or a parent directory) should be roughly like this for a standard configuration:

::

Expand Down
8 changes: 8 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[mypy]
python_version = 3.6

[mypy-pylsp.*]
ignore_missing_imports = True

[mypy-pylsp_mypy.plugin]
disallow_untyped_decorators = False
1 change: 0 additions & 1 deletion mypy_ls/_version.py

This file was deleted.

File renamed without changes.
1 change: 1 addition & 0 deletions pylsp_mypy/_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "0.5.0"
96 changes: 50 additions & 46 deletions mypy_ls/plugin.py → pylsp_mypy/plugin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
"""
File that contains the python-lsp-server plugin mypy-ls.
File that contains the python-lsp-server plugin pylsp-mypy.

Created on Fri Jul 10 09:53:57 2020

Expand All @@ -10,6 +10,7 @@
import tempfile
import os
import os.path
from pathlib import Path
import logging
from mypy import api as mypy_api
from pylsp import hookimpl
Expand All @@ -24,7 +25,7 @@
log = logging.getLogger(__name__)

# A mapping from workspace path to config file path
mypyConfigFileMap: Dict[str, Optional[str]] = dict()
mypyConfigFileMap: Dict[str, Optional[str]] = {}

tmpFile: Optional[IO[str]] = None

Expand All @@ -33,12 +34,10 @@
# so store a cache of last diagnostics for each file a-la the pylint plugin,
# so we can return some potentially-stale diagnostics.
# https://github.com/python-lsp/python-lsp-server/blob/v1.0.1/pylsp/plugins/pylint_lint.py#L55-L62
last_diagnostics: Dict[str, List] = collections.defaultdict(list)
last_diagnostics: Dict[str, List[Dict[str, Any]]] = collections.defaultdict(list)


def parse_line(
line: str, document: Optional[Document] = None
) -> Optional[Dict[str, Any]]:
def parse_line(line: str, document: Optional[Document] = None) -> Optional[Dict[str, Any]]:
"""
Return a language-server diagnostic from a line of the Mypy error report.

Expand Down Expand Up @@ -66,9 +65,7 @@ def parse_line(
# results from other files can be included, but we cannot return
# them.
if document and document.path and not document.path.endswith(file_path):
log.warning(
"discarding result for %s against %s", file_path, document.path
)
log.warning("discarding result for %s against %s", file_path, document.path)
return None

lineno = int(linenoStr or 1) - 1 # 0-based line number
Expand All @@ -91,9 +88,7 @@ def parse_line(
# can make a good guess by highlighting the word that Mypy flagged
word = document.word_at_position(diag["range"]["start"])
if word:
diag["range"]["end"]["character"] = diag["range"]["start"][
"character"
] + len(word)
diag["range"]["end"]["character"] = diag["range"]["start"]["character"] + len(word)

return diag
return None
Expand Down Expand Up @@ -123,7 +118,22 @@ def pylsp_lint(
List of the linting data.

"""
settings = config.plugin_settings("mypy-ls")
settings = config.plugin_settings("pylsp_mypy")
oldSettings1 = config.plugin_settings("mypy-ls")
if oldSettings1 != {}:
raise DeprecationWarning(
"Your configuration uses the namespace mypy-ls, this should be changed to pylsp_mypy"
)
oldSettings2 = config.plugin_settings("mypy_ls")
if oldSettings2 != {}:
raise DeprecationWarning(
"Your configuration uses the namespace mypy_ls, this should be changed to pylsp_mypy"
)
if settings == {}:
settings = oldSettings1
if settings == {}:
settings = oldSettings2

log.info(
"lint settings = %s document.path = %s is_saved = %s",
settings,
Expand All @@ -143,9 +153,12 @@ def pylsp_lint(
args = ["--show-column-numbers"]

global tmpFile
if live_mode and not is_saved and tmpFile:
log.info("live_mode tmpFile = %s", live_mode)
tmpFile = open(tmpFile.name, "w")
if live_mode and not is_saved:
if tmpFile:
tmpFile = open(tmpFile.name, "w")
else:
tmpFile = tempfile.NamedTemporaryFile("w", delete=False)
log.info("live_mode tmpFile = %s", tmpFile.name)
tmpFile.write(document.source)
tmpFile.close()
args.extend(["--shadow-file", document.path, tmpFile.name])
Expand Down Expand Up @@ -181,9 +194,7 @@ def pylsp_lint(
# In either case, reset to fresh state
_, _err, _status = mypy_api.run_dmypy(["status"])
if _status != 0:
log.info(
"restarting dmypy from status: %s message: %s", _status, _err.strip()
)
log.info("restarting dmypy from status: %s message: %s", _status, _err.strip())
mypy_api.run_dmypy(["kill"])

# run to use existing daemon or restart if required
Expand All @@ -201,7 +212,7 @@ def pylsp_lint(
if diag:
diagnostics.append(diag)

log.info("mypy-ls len(diagnostics) = %s", len(diagnostics))
log.info("pylsp-mypy len(diagnostics) = %s", len(diagnostics))

last_diagnostics[document.path] = diagnostics
return diagnostics
Expand All @@ -224,7 +235,7 @@ def pylsp_settings(config: Config) -> Dict[str, Dict[str, Dict[str, str]]]:

"""
configuration = init(config._root_path)
return {"plugins": {"mypy-ls": configuration}}
return {"plugins": {"pylsp_mypy": configuration}}


def init(workspace: str) -> Dict[str, str]:
Expand All @@ -242,33 +253,22 @@ def init(workspace: str) -> Dict[str, str]:
The plugin config dict.

"""
# On windows the path contains \\ on linux it contains / all the code works with /
log.info("init workspace = %s", workspace)
workspace = workspace.replace("\\", "/")

configuration = {}
path = findConfigFile(workspace, "mypy-ls.cfg")
path = findConfigFile(workspace, ["pylsp-mypy.cfg", "mypy-ls.cfg", "mypy_ls.cfg"])
if path:
with open(path) as file:
configuration = eval(file.read())

mypyConfigFile = findConfigFile(workspace, "mypy.ini")
if not mypyConfigFile:
mypyConfigFile = findConfigFile(workspace, ".mypy.ini")
mypyConfigFile = findConfigFile(workspace, ["mypy.ini", ".mypy.ini"])
mypyConfigFileMap[workspace] = mypyConfigFile

if ("enabled" not in configuration or configuration["enabled"]) and (
"live_mode" not in configuration or configuration["live_mode"]
):
global tmpFile
tmpFile = tempfile.NamedTemporaryFile("w", delete=False)
tmpFile.close()

log.info("mypyConfigFile = %s configuration = %s", mypyConfigFile, configuration)
return configuration


def findConfigFile(path: str, name: str) -> Optional[str]:
def findConfigFile(path: str, names: List[str]) -> Optional[str]:
"""
Search for a config file.

Expand All @@ -279,24 +279,28 @@ def findConfigFile(path: str, name: str) -> Optional[str]:
----------
path : str
The path where the search starts.
name : str
The file to be found.
names : List[str]
The file to be found (or alternative names).

Returns
-------
Optional[str]
The path where the file has been found or None if no matching file has been found.

"""
while True:
p = f"{path}/{name}"
if os.path.isfile(p):
return p
else:
loc = path.rfind("/")
if loc == -1:
return None
path = path[:loc]
start = Path(path).joinpath(names[0]) # the join causes the parents to include path
for parent in start.parents:
for name in names:
file = parent.joinpath(name)
if file.is_file():
if file.name in ["mypy-ls.cfg", "mypy_ls.cfg"]:
raise DeprecationWarning(
f"{str(file)}: {file.name} is no longer supported, you should rename your "
"config file to pylsp-mypy.cfg"
)
return str(file)

return None


@atexit.register
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tool.black]
line-length = 90
line-length = 100
include = '\.pyi?$'
exclude = '''
/(
Expand Down
6 changes: 3 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[metadata]
name = mypy-ls
name = pylsp-mypy
author = Tom van Ommeren, Richard Kellnberger
description = Mypy linter for the Python LSP Server
url = https://github.com/Richardk2n/pyls-mypy
url = https://github.com/Richardk2n/pylsp-mypy
long_description = file: README.rst
license='MIT'
classifiers =
Expand All @@ -24,7 +24,7 @@ install_requires =


[options.entry_points]
pylsp = mypy_ls = mypy_ls.plugin
pylsp = pylsp_mypy = pylsp_mypy.plugin

[options.extras_require]
test =
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
from setuptools import setup
from mypy_ls import _version
from pylsp_mypy import _version

if __name__ == "__main__":
setup(version=_version.__version__, long_description_content_type="text/x-rst")
4 changes: 2 additions & 2 deletions test/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pylsp.config.config import Config
from pylsp import uris
from mock import Mock
from mypy_ls import plugin
from pylsp_mypy import plugin

DOC_URI = __file__
DOC_TYPE_ERR = """{}.append(3)
Expand Down Expand Up @@ -35,7 +35,7 @@ def plugin_settings(self, plugin, document_path=None):
def test_settings():
config = FakeConfig()
settings = plugin.pylsp_settings(config)
assert settings == {"plugins": {"mypy-ls": {}}}
assert settings == {"plugins": {"pylsp_mypy": {}}}


def test_plugin(workspace):
Expand Down