8000 Fix Pycodestyle linting with line endings other than LF by ccordoba12 · Pull Request #319 · python-lsp/python-lsp-server · GitHub
[go: up one dir, main page]

Skip to content

Fix Pycodestyle linting with line endings other than LF #319

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

Merged
merged 4 commits into from
Dec 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


10000
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ on:
branches:
- '*'

concurrency:
group: static-${{ github.ref }}
cancel-in-progress: true

jobs:
build:
name: Static code analysis
Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/test-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ on:
branches:
- '*'

concurrency:
group: test-linux-${{ github.ref }}
cancel-in-progress: true

jobs:
build:
name: Linux Py${{ matrix.PYTHON_VERSION }}
Expand Down Expand Up @@ -41,7 +45,7 @@ jobs:
- run: pip install -e .[all,test]
- name: Show test environment
run: pip list
- run: pytest -v test/
- run: pytest --color=yes -v test/
# Enable this if SSH debugging is required
# - name: Setup tmate session
# uses: mxschmitt/action-tmate@v3
Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/test-mac.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ on:
branches:
- '*'

concurrency:
group: test-mac-${{ github.ref }}
cancel-in-progress: true

jobs:
build:
name: Mac Py${{ matrix.PYTHON_VERSION }}
Expand Down Expand Up @@ -41,7 +45,7 @@ jobs:
- run: pip install -e .[all,test]
- name: Show test environment
run: pip list
- run: pytest -v test/
- run: pytest --color=yes -v test/
# Enable this if SSH debugging is required
# - name: Setup tmate session
# uses: mxschmitt/action-tmate@v3
Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/test-win.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ on:
branches:
- '*'

concurrency:
group: test-win-${{ github.ref }}
cancel-in-progress: true

jobs:
build:
name: Win Py${{ matrix.PYTHON_VERSION }}
Expand Down Expand Up @@ -37,4 +41,4 @@ jobs:
- run: pip install -e .[all,test]
- name: Show test environment
run: pip list
- run: pytest -v test/
- run: pytest --color=yes -v test/
15 changes: 14 additions & 1 deletion pylsp/plugins/pycodestyle_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
# Copyright 2021- Python Language Server Contributors.

import logging

import pycodestyle

from pylsp import hookimpl, lsp
from pylsp._utils import get_eol_chars

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

# Use LF to lint file because other line endings can give false positives.
# See spyder-ide/spyder#19565 for context.
source = document.source
eol_chars = get_eol_chars(source)
if eol_chars in ['\r', '\r\n']:
source = source.replace(eol_chars, '\n')
lines = source.splitlines(keepends=True)
else:
lines = document.lines

c = pycodestyle.Checker(
filename=document.uri, lines=document.lines, options=styleguide.options,
filename=document.path, lines=lines, options=styleguide.options,
report=PyCodeStyleDiagnosticReport(styleguide.options)
)
c.check_all()
Expand Down
22 changes: 22 additions & 0 deletions test/plugins/test_pycodestyle_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
# Copyright 2021- Python Language Server Contributors.

import os

import pytest

from pylsp import lsp, uris
from pylsp.workspace import Document
from pylsp.plugins import pycodestyle_lint
Expand Down Expand Up @@ -110,3 +113,22 @@ def test_pycodestyle_config(workspace):
assert not [d for d in diags if d['code'] == 'W191']
assert not [d for d in diags if d['code'] == 'E201']
assert [d for d in diags if d['code'] == 'W391']


@pytest.mark.parametrize('newline', ['\r\n', '\r'])
def test_line_endings(workspace, newline):
"""
Check that Pycodestyle doesn't generate false positives with line endings
other than LF.
"""
# Create simple source that should give false positives
source = f"try:{newline} 1/0{newline}except Exception:{newline} pass{newline}"

# Create document
doc = Document(DOC_URI, workspace, source)

# Get diagnostics
diags = pycodestyle_lint.pylsp_lint(workspace, doc)

# Assert no diagnostics were given
assert len(diags) == 0
0