Repository: palantir/python-language-server

Issue:
Custom pycodestyle configuration via xcode/Atom settings
It would be really great to configure the ignored error codes and the maximum line length of `pycodestyle` via the settings if no configuration file is found on the system.

This is the most requested issue on the Atom package for `pyls`: https://github.com/lgeiger/ide-python/issues/9

Comments:

Patch:
diff --git a/pyls/plugins/pycodestyle_lint.py b/pyls/plugins/pycodestyle_lint.py
index 668096d..2a5baab 100644
--- a/pyls/plugins/pycodestyle_lint.py
+++ b/pyls/plugins/pycodestyle_lint.py
@@ -21,12 +21,29 @@ def pyls_lint(config, document):
     conf_to_use = pycodestyle_conf if pycodestyle_conf else pep8_conf
 
     conf = {k.replace("-", "_"): v for k, v in conf_to_use.items()}
-    log.debug("Got pycodestyle config: %s", conf)
 
     # Grab the pycodestyle parser and set the defaults based on the config we found
     parser = pycodestyle.get_parser()
     parser.set_defaults(**conf)
-    opts, _args = parser.parse_args([])
+
+    # Override with any options set in the language server config
+    argv = []
+    ls_conf = config.plugin_settings('pycodestyle')
+    if ls_conf.get('exclude') is not None:
+        argv.extend(['--exclude', ','.join(ls_conf['exclude'])])
+    if ls_conf.get('filename') is not None:
+        argv.extend(['--filename', ','.join(ls_conf['filename'])])
+    if ls_conf.get('select') is not None:
+        argv.extend(['--select', ','.join(ls_conf['select'])])
+    if ls_conf.get('ignore') is not None:
+        argv.extend(['--ignore', ','.join(ls_conf['ignore'])])
+    if ls_conf.get('maxLineLength') is not None:
+        argv.extend(['--max-line-length', str(ls_conf['maxLineLength'])])
+    if ls_conf.get('hangClosing'):
+        argv.extend(['--hang-closing'])
+
+    opts, _args = parser.parse_args(argv)
+    log.debug("Got pycodestyle config: %s", opts)
     styleguide = pycodestyle.StyleGuide(vars(opts))
 
     c = pycodestyle.Checker(
diff --git a/vscode-client/package.json b/vscode-client/package.json
index b75ce25..5c8b285 100644
--- a/vscode-client/package.json
+++ b/vscode-client/package.json
@@ -70,6 +70,52 @@
                     "default": true,
                     "description": "Enable or disable the plugin."
                 },
+                "pyls.plugins.pycodestyle.exclude": {
+                    "type": "array",
+                    "default": null,
+                    "items": {
+                        "type": "string"
+                    },
+                    "uniqueItems": true,
+                    "description": "Exclude files or directories which match these patterns."
+                },
+                "pyls.plugins.pycodestyle.filename": {
+                    "type": "array",
+                    "default": null,
+                    "items": {
+                        "type": "string"
+                    },
+                    "uniqueItems": true,
+                    "description": "When parsing directories, only check filenames matching these patterns."
+                },
+                "pyls.plugins.pycodestyle.select": {
+                    "type": "array",
+                    "default": null,
+                    "items": {
+                        "type": "string"
+                    },
+                    "uniqueItems": true,
+                    "description": "Select errors and warnings"
+                },
+                "pyls.plugins.pycodestyle.ignore": {
+                    "type": "array",
+                    "default": null,
+                    "items": {
+                        "type": "string"
+                    },
+                    "uniqueItems": true,
+                    "description": "Ignore errors and warnings"
+                },
+                "pyls.plugins.pycodestyle.hangClosing": {
+                    "type": "boolean",
+                    "default": null,
+                    "description": "Hang closing bracket instead of matching indentation of opening bracket's line."
+                },
+                "pyls.plugins.pycodestyle.maxLineLength": {
+                    "type": "number",
+                    "default": null,
+                    "description": "Set maximum allowed line length."
+                },
                 "pyls.plugins.pydocstyle.enabled": {
                     "type": "boolean",
                     "default": false,


Test patch:
diff --git a/test/plugins/test_lint.py b/test/plugins/test_lint.py
index a760c68..a02d992 100644
--- a/test/plugins/test_lint.py
+++ b/test/plugins/test_lint.py
@@ -96,6 +96,12 @@ def test_pycodestyle_config(workspace):
 
         os.unlink(os.path.join(workspace.root_path, conf_file))
 
+    # Make sure we can ignore via the PYLS config as well
+    config.update({'plugins': {'pycodestyle': {'ignore': ['W191']}}})
+    # And make sure we don't get any warnings
+    diags = pycodestyle_lint.pyls_lint(config, doc)
+    assert not [d for d in diags if d['code'] == 'W191']
+
 
 def test_pydocstyle():
     doc = Document(DOC_URI, DOC)