pylint-dev / pylint

It's not just a linter that annoys you!
https://pylint.readthedocs.io/en/latest/
GNU General Public License v2.0
5.31k stars 1.14k forks source link

Undeserved unexpected-line-ending-format on Windows #9054

Open owillebo opened 1 year ago

owillebo commented 1 year ago

Bug description

pylint modifies source code that it reads from stdin.

See `...\pylint\lint\pylinter.py`

def _read_stdin() -> str:
    # See https://github.com/python/typeshed/pull/5623 for rationale behind assertion
    assert isinstance(sys.stdin, TextIOWrapper)
    sys.stdin = TextIOWrapper(sys.stdin.detach(), encoding="utf-8")
    return sys.stdin.read()

See TextIOWrapper https://docs.python.org/3/library/io.html#io.TextIOWrapper

if newline is None, universal newlines mode is enabled. Lines in the input can end in '\n', '\r', or '\r\n', and these are translated into '\n' before being returned to the caller.

Please add newline="" to TextIOWrapper constructor.

def _read_stdin() -> str:
    # See https://github.com/python/typeshed/pull/5623 for rationale behind assertion
    assert isinstance(sys.stdin, TextIOWrapper)
    sys.stdin = TextIOWrapper(sys.stdin.detach(), encoding="utf-8", newline="")
    return sys.stdin.read()

Configuration

No response

Command used

@echo off
REM ~ Clean up.
erase some_file.py
rmdir /S /Q .venv
REM ~ Generate a python file with CRLF line ending.
(
  echo """Some docstring"""
)>"some_file.py"
REM ~ Generate environment.
py -3.11-64 -m venv .venv
call .venv\Scripts\activate.bat
python -m pip install pylint > nul
@echo on

REM ~ Run pylint with and without --from-stdin.

REM ~ pylint does issues an unexpected-line-ending-format warning.
type some_file.py | pylint --expected-line-ending-format=CRLF --from-stdin some_file.py

REM ~ pylint issues no unexpected-line-ending-format warning.
pylint --expected-line-ending-format=CRLF some_file.py

Pylint output

************* Module some_file
some_file.py:1:0: C0328: Unexpected line ending format. There is 'LF' while it should be 'CRLF'. (unexpected-line-ending-format)

Expected behavior

No ouput

Pylint version

pylint 2.17.5
astroid 2.15.6
Python 3.11.5 (tags/v3.11.5:cce6ba9, Aug 24 2023, 14:38:34) [MSC v.1936 64 bit (AMD64)]

OS / Environment

Windows

Additional dependencies

astroid==2.15.6
colorama==0.4.6
dill==0.3.7
isort==5.12.0
lazy-object-proxy==1.9.0
mccabe==0.7.0
pip==23.2.1
platformdirs==3.10.0
pylint==2.17.5
setuptools==65.5.0
tomlkit==0.12.1
wrapt==1.15.0
Pierre-Sassoulas commented 1 year ago

Thank you for the bug report and in depth analysis ! Do you want to open the merge request yourself ? (Maybe also adding a failing test along the way)

owillebo commented 1 year ago

Then I must quote Pippi Longstocking: “I have never tried that before, so I think I should definitely be able to do that.”

I made a clone. I checked out the clone.

How do I run the tests? To which test should I add the test case.

Pierre-Sassoulas commented 1 year ago

Thank you for trying :) ! You can use pytest:

source venv/bin/activate
pip install -r requirements_test_min.txt
pytest

The contribution doc should be up to date for more details : https://pylint.readthedocs.io/en/stable/development_guide/contributor_guide/contribute.html#creating-a-pull-request, in particular https://pylint.readthedocs.io/en/stable/development_guide/contributor_guide/tests/launching_test.html

owillebo commented 1 year ago

It is more work than expected, so I cowardly have to bail out :-(