Skip to content
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

Support for relative imports #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
43 changes: 40 additions & 3 deletions pyls_mypy/plugin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import re
import logging
import tempfile
import platform
import os
import contextlib
from pathlib import Path
from mypy import api as mypy_api
from pyls import hookimpl
from typing import IO, Tuple, Optional

line_pattern = r"([^:]+):(?:(\d+):)?(?:(\d+):)? (\w+): (.*)"

Expand Down Expand Up @@ -52,15 +58,45 @@ def parse_line(line, document=None):
return diag


def smart_tempfile(settings) -> Optional[Tuple[IO[bytes], Path]]:
"""
Returns a temporary file-like object opened in write mode and pointed to by
the returned path.
May fail if the configuration does not allow writing to disk.
"""
if platform.system() == "Linux":
p = Path("/proc") / str(os.getpid()) / "fd"
if p.exists():
try:
import memfd
fd = memfd.open("_", flags=0, mode="wb")
return fd, p / str(fd.fileno())
except IOError:
pass # fallback
if not settings.get("temporary_write", False):
return None
fd = tempfile.NamedTemporaryFile('wb')
return (fd, fd.name)



@hookimpl
def pyls_lint(config, workspace, document, is_saved):
settings = config.plugin_settings('pyls_mypy')
live_mode = settings.get('live_mode', True)
fd = contextlib.nullcontext(None)
if live_mode:
args = ['--incremental',
'--show-column-numbers',
'--follow-imports', 'silent',
'--command', document.source]
'--follow-imports', 'silent']
t = smart_tempfile(settings)
if t is None:
args += ['--command', document.source]
else:
(fd, path) = t
fd.write(document.source.encode("utf8"))
fd.flush()
args += ['--shadow-file', document.path, str(path), document.path]
elif is_saved:
args = ['--incremental',
'--show-column-numbers',
Expand All @@ -72,7 +108,8 @@ def pyls_lint(config, workspace, document, is_saved):
if settings.get('strict', False):
args.append('--strict')

report, errors, _ = mypy_api.run(args)
with fd:
report, errors, _ = mypy_api.run(args)

diagnostics = []
for line in report.splitlines():
Expand Down