Skip to content

Commit a79c171

Browse files
authored
Fix Pycodestyle linting with line endings other than LF (#319)
1 parent fba2e7e commit a79c171

File tree

6 files changed

+55
-4
lines changed

6 files changed

+55
-4
lines changed

Diff for: .github/workflows/static.yml

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ on:
99
branches:
1010
- '*'
1111

12+
concurrency:
13+
group: static-${{ github.ref }}
14+
cancel-in-progress: true
15+
1216
jobs:
1317
build:
1418
name: Static code analysis

Diff for: .github/workflows/test-linux.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ on:
99
branches:
1010
- '*'
1111

12+
concurrency:
13+
group: test-linux-${{ github.ref }}
14+
cancel-in-progress: true
15+
1216
jobs:
1317
build:
1418
name: Linux Py${{ matrix.PYTHON_VERSION }}
@@ -41,7 +45,7 @@ jobs:
4145
- run: pip install -e .[all,test]
4246
- name: Show test environment
4347
run: pip list
44-
- run: pytest -v test/
48+
- run: pytest --color=yes -v test/
4549
# Enable this if SSH debugging is required
4650
# - name: Setup tmate session
4751
# uses: mxschmitt/action-tmate@v3

Diff for: .github/workflows/test-mac.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ on:
99
branches:
1010
- '*'
1111

12+
concurrency:
13+
group: test-mac-${{ github.ref }}
14+
cancel-in-progress: true
15+
1216
jobs:
1317
build:
1418
name: Mac Py${{ matrix.PYTHON_VERSION }}
@@ -41,7 +45,7 @@ jobs:
4145
- run: pip install -e .[all,test]
4246
- name: Show test environment
4347
run: pip list
44-
- run: pytest -v test/
48+
- run: pytest --color=yes -v test/
4549
# Enable this if SSH debugging is required
4650
# - name: Setup tmate session
4751
# uses: mxschmitt/action-tmate@v3

Diff for: .github/workflows/test-win.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ on:
99
branches:
1010
- '*'
1111

12+
concurrency:
13+
group: test-win-${{ github.ref }}
14+
cancel-in-progress: true
15+
1216
jobs:
1317
build:
1418
name: Win Py${{ matrix.PYTHON_VERSION }}
@@ -37,4 +41,4 @@ jobs:
3741
- run: pip install -e .[all,test]
3842
- name: Show test environment
3943
run: pip list
40-
- run: pytest -v test/
44+
- run: pytest --color=yes -v test/

Diff for: pylsp/plugins/pycodestyle_lint.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
# Copyright 2021- Python Language Server Contributors.
33

44
import logging
5+
56
import pycodestyle
7+
68
from pylsp import hookimpl, lsp
9+
from pylsp._utils import get_eol_chars
710

811
try:
912
from autopep8 import continued_indentation as autopep8_c_i
@@ -39,8 +42,18 @@ def pylsp_lint(workspace, document):
3942
kwargs = {k: v for k, v in opts.items() if v}
4043
styleguide = pycodestyle.StyleGuide(kwargs)
4144

45+
# Use LF to lint file because other line endings can give false positives.
46+
# See spyder-ide/spyder#19565 for context.
47+
source = document.source
48+
eol_chars = get_eol_chars(source)
49+
if eol_chars in ['\r', '\r\n']:
50+
source = source.replace(eol_chars, '\n')
51+
lines = source.splitlines(keepends=True)
52+
else:
53+
lines = document.lines
54+
4255
c = pycodestyle.Checker(
43-
filename=document.uri, lines=document.lines, options=styleguide.options,
56+
filename=document.path, lines=lines, options=styleguide.options,
4457
report=PyCodeStyleDiagnosticReport(styleguide.options)
4558
)
4659
c.check_all()

Diff for: test/plugins/test_pycodestyle_lint.py

+22
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
# Copyright 2021- Python Language Server Contributors.
33

44
import os
5+
6+
import pytest
7+
58
from pylsp import lsp, uris
69
from pylsp.workspace import Document
710
from pylsp.plugins import pycodestyle_lint
@@ -110,3 +113,22 @@ def test_pycodestyle_config(workspace):
110113
assert not [d for d in diags if d['code'] == 'W191']
111114
assert not [d for d in diags if d['code'] == 'E201']
112115
assert [d for d in diags if d['code'] == 'W391']
116+
117+
118+
@pytest.mark.parametrize('newline', ['\r\n', '\r'])
119+
def test_line_endings(workspace, newline):
120+
"""
121+
Check that Pycodestyle doesn't generate false positives with line endings
122+
other than LF.
123+
"""
124+
# Create simple source that should give false positives
125+
source = f"try:{newline} 1/0{newline}except Exception:{newline} pass{newline}"
126+
127+
# Create document
128+
doc = Document(DOC_URI, workspace, source)
129+
130+
# Get diagnostics
131+
diags = pycodestyle_lint.pylsp_lint(workspace, doc)
132+
133+
# Assert no diagnostics were given
134+
assert len(diags) == 0

0 commit comments

Comments
 (0)