Skip to content

Commit 363bd6b

Browse files
committed
Make the match_exclude_patterns logic os independent
Just convert windows paths to unix (a ilke) paths, so d:\My Documents\my_file.py becomes d:/My Documents/my_file.py Then you can reuse the configured exclude patterns for both windows and unix (a like) platforms. fds
1 parent 00226fa commit 363bd6b

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Configuration
4646
With this option, pylsp-mypy will report when mypy is running, given your editor supports LSP progress reporting. For small files this might produce annoying flashing in your editor, especially in with ``live_mode``. For large projects, enabling this can be helpful to assure yourself whether mypy is still running.
4747

4848
``exclude`` (default is ``[]``) A list of regular expressions which should be ignored.
49-
The ``mypy`` runner wil not be invoked when a document path is matched by one of the expressions. Note that this differs from the ``exclude`` directive of a ``mypy`` config which is only used for recursively discovering files when mypy is invoked on a whole directory.
49+
The ``mypy`` runner wil not be invoked when a document path is matched by one of the expressions. Note that this differs from the ``exclude`` directive of a ``mypy`` config which is only used for recursively discovering files when mypy is invoked on a whole directory. For both windows or unix platforms you should use forward slashes (``/``) to indicate paths.
5050

5151
This project supports the use of ``pyproject.toml`` for configuration. It is in fact the preferred way. Using that your configuration could look like this:
5252

pylsp_mypy/plugin.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,10 @@ def didSettingsChange(workspace: str, settings: Dict[str, Any]) -> None:
143143

144144

145145
def match_exclude_patterns(document_path: str, exclude_patterns: list) -> bool:
146-
for pattern in exclude_patterns:
147-
# This makes sure that \\ characters (and other unicode characters) are
148-
# escaped first so the regex matcher will be able to parse them correctly.
149-
# Especially useful for matching windows paths without any bad escape errors
150-
pattern = pattern.encode("unicode-escape").decode()
146+
"""Check if the current document path matches any of the configures exlude patterns."""
147+
document_path = document_path.replace(os.sep, "/")
151148

149+
for pattern in exclude_patterns:
152150
try:
153151
if re.search(pattern, document_path):
154152
log.debug(f"{document_path} matches " f"exclude pattern '{pattern}'")

test/test_plugin.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import sys
55
from pathlib import Path
66
from typing import Dict
7-
from unittest.mock import Mock
7+
from unittest.mock import Mock, patch
88

99
import pytest
1010
from mypy import api as mypy_api
@@ -331,29 +331,31 @@ def foo():
331331

332332

333333
@pytest.mark.parametrize(
334-
"document_path,pattern,pattern_matched",
334+
"document_path,pattern,os_sep,pattern_matched",
335335
(
336-
("/workspace/my-file.py", "/someting-else", False),
337-
("/workspace/my-file.py", "^/workspace$", False),
338-
("/workspace/my-file.py", "/workspace", True),
339-
("/workspace/my-file.py", "^/workspace(.*)$", True),
336+
("/workspace/my-file.py", "/someting-else", "/", False),
337+
("/workspace/my-file.py", "^/workspace$", "/", False),
338+
("/workspace/my-file.py", "/workspace", "/", True),
339+
("/workspace/my-file.py", "^/workspace(.*)$", "/", True),
340340
# This is a broken regex (missing ')'), but should not choke
341-
("/workspace/my-file.py", "/((workspace)", False),
341+
("/workspace/my-file.py", "/((workspace)", "/", False),
342342
# Windows paths are tricky with all those \\ and unintended escape,
343343
# characters but they should 'just' work
344-
("d:\\a\\my-file.py", "\\a", True),
344+
("d:\\a\\my-file.py", "/a", "\\", True),
345345
(
346346
"d:\\a\\pylsp-mypy\\pylsp-mypy\\test\\test_plugin.py",
347-
"d:\\a\\pylsp-mypy\\pylsp-mypy\\test\\test_plugin.py",
347+
"/a/pylsp-mypy/pylsp-mypy/test/test_plugin.py",
348+
"\\",
348349
True,
349350
),
350351
),
351352
)
352-
def test_match_exclude_patterns(document_path, pattern, pattern_matched):
353-
assert (
354-
plugin.match_exclude_patterns(document_path=document_path, exclude_patterns=[pattern])
355-
is pattern_matched
356-
)
353+
def test_match_exclude_patterns(document_path, pattern, os_sep, pattern_matched):
354+
with patch("os.sep", new=os_sep):
355+
assert (
356+
plugin.match_exclude_patterns(document_path=document_path, exclude_patterns=[pattern])
357+
is pattern_matched
358+
)
357359

358360

359361
def test_config_exclude(tmpdir, workspace):
@@ -365,6 +367,8 @@ def test_config_exclude(tmpdir, workspace):
365367
diags = plugin.pylsp_lint(workspace._config, workspace, doc, is_saved=False)
366368
assert diags[0]["message"] == TYPE_ERR_MSG
367369

368-
workspace.update_config({"pylsp": {"plugins": {"pylsp_mypy": {"exclude": [doc.path]}}}})
370+
# Add the path of our document to the exclude patterns
371+
exclude_path = doc.path.replace(os.sep, "/")
372+
workspace.update_config({"pylsp": {"plugins": {"pylsp_mypy": {"exclude": [exclude_path]}}}})
369373
diags = plugin.pylsp_lint(workspace._config, workspace, doc, is_saved=False)
370374
assert diags == []

0 commit comments

Comments
 (0)