Skip to content

Commit bf2c3ce

Browse files
committed
The 'match' option in a config file isn't ignored and small refactoring
1 parent 5a383df commit bf2c3ce

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

pylsp/plugins/pydocstyle_lint.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
import contextlib
55
import logging
6-
import os
76
import re
87
import sys
8+
from pathlib import Path
99

1010
import pydocstyle
1111

@@ -17,7 +17,6 @@
1717
pydocstyle_logger = logging.getLogger(pydocstyle.utils.__name__)
1818
pydocstyle_logger.setLevel(logging.INFO)
1919

20-
DEFAULT_MATCH_RE = pydocstyle.config.ConfigurationParser.DEFAULT_MATCH_RE
2120
DEFAULT_MATCH_DIR_RE = pydocstyle.config.ConfigurationParser.DEFAULT_MATCH_DIR_RE
2221

2322

@@ -33,30 +32,36 @@ def pylsp_lint(config, workspace, document):
3332
settings = config.plugin_settings("pydocstyle", document_path=document.path)
3433
log.debug("Got pydocstyle settings: %s", settings)
3534

36-
# Explicitly passing a path to pydocstyle means it doesn't respect the --match flag, so do it ourselves
37-
filename_match_re = re.compile(settings.get("match", DEFAULT_MATCH_RE) + "$")
38-
if not filename_match_re.match(os.path.basename(document.path)):
39-
return []
40-
41-
# Likewise with --match-dir
35+
# We explicitly pass the path to `pydocstyle`, so it ignores `--match-dir`. But
36+
# we can match the directory ourselves.
4237
dir_match_re = re.compile(settings.get("matchDir", DEFAULT_MATCH_DIR_RE) + "$")
43-
if not dir_match_re.match(os.path.basename(os.path.dirname(document.path))):
38+
if not dir_match_re.match(Path(document.path).parent.name):
4439
return []
4540

4641
args = [document.path]
4742

48-
if settings.get("convention"):
49-
args.append("--convention=" + settings["convention"])
43+
def append_if_exists(setting_name, arg_name=None):
44+
"""Append an argument if it exists in `settings`."""
45+
if setting_name not in settings:
46+
return False
47+
48+
if isinstance(settings[setting_name], str):
49+
value = settings[setting_name]
50+
else:
51+
value = ",".join(settings[setting_name])
52+
53+
args.append(f"--{arg_name or setting_name}={value}")
54+
return True
5055

51-
if settings.get("addSelect"):
52-
args.append("--add-select=" + ",".join(settings["addSelect"]))
53-
if settings.get("addIgnore"):
54-
args.append("--add-ignore=" + ",".join(settings["addIgnore"]))
56+
if append_if_exists("convention"):
57+
append_if_exists("addSelect", "add-select")
58+
append_if_exists("addIgnore", "add-ignore")
59+
elif append_if_exists("select"):
60+
pass
61+
else:
62+
append_if_exists("ignore")
5563

56-
elif settings.get("select"):
57-
args.append("--select=" + ",".join(settings["select"]))
58-
elif settings.get("ignore"):
59-
args.append("--ignore=" + ",".join(settings["ignore"]))
64+
append_if_exists("match")
6065

6166
log.info("Using pydocstyle args: %s", args)
6267

0 commit comments

Comments
 (0)