Skip to content
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

fix --aggressive raises UnicodeDecodeError when reading a file and doesn't print the file path #129

Open
bkeryan opened this issue May 1, 2023 · 0 comments

Comments

@bkeryan
Copy link
Contributor

bkeryan commented May 1, 2023

Running poetry run ni-python-styleguide fix --aggressive on https://github.com/ni/measurementlink-python produces this error:

Traceback (most recent call last):
  File "C:\Users\bkeryan\.pyenv\pyenv-win\versions\3.9.13\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\bkeryan\.pyenv\pyenv-win\versions\3.9.13\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\dev\measurementlink-python\.venv\Scripts\ni-python-styleguide.exe\__main__.py", line 7, in <module>
  File "C:\dev\measurementlink-python\.venv\lib\site-packages\click\core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "C:\dev\measurementlink-python\.venv\lib\site-packages\click\core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "C:\dev\measurementlink-python\.venv\lib\site-packages\click\core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "C:\dev\measurementlink-python\.venv\lib\site-packages\click\core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "C:\dev\measurementlink-python\.venv\lib\site-packages\click\core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "C:\dev\measurementlink-python\.venv\lib\site-packages\click\decorators.py", line 38, in new_func
    return f(get_current_context().obj, *args, **kwargs)
  File "C:\dev\measurementlink-python\.venv\lib\site-packages\ni_python_styleguide\_cli.py", line 186, in fix
    _fix.fix(
  File "C:\dev\measurementlink-python\.venv\lib\site-packages\ni_python_styleguide\_fix.py", line 110, in fix
    _acknowledge_existing_errors.remove_auto_suppressions_from_file(file)
  File "C:\dev\measurementlink-python\.venv\lib\site-packages\ni_python_styleguide\_acknowledge_existing_errors\__init__.py", line 138, in remove_auto_suppressions_from_file
    lines = file.read_text(encoding=_utils.DEFAULT_ENCODING).splitlines()
  File "C:\Users\bkeryan\.pyenv\pyenv-win\versions\3.9.13\lib\pathlib.py", line 1267, in read_text
    return f.read()
  File "C:\Users\bkeryan\.pyenv\pyenv-win\versions\3.9.13\lib\codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf6 in position 122: invalid start byte

For debugging, I added calls to print (but logging is more appropriate):

def remove_auto_suppressions_from_file(file: pathlib.Path):
    """Removes auto-suppressions from file."""
    print(str(file))
    lines = file.read_text(encoding=_utils.DEFAULT_ENCODING).splitlines()
    stripped_lines = [_filter_suppresion_from_line(line) for line in lines]
    file.write_text("\n".join(stripped_lines) + "\n", encoding=_utils.DEFAULT_ENCODING)


def _suppress_errors_in_file(bad_file, errors_in_file, encoding):
    path = pathlib.Path(bad_file)
    print(str(path))
    lines = path.read_text(encoding=encoding).splitlines(keepends=True)

This shows that fix --aggressive is reading files from .venv, even though they should be excluded.

C:\dev\measurementlink-python\.venv\Lib\site-packages\win32com\demos\excelAddin.py
C:\dev\measurementlink-python\.venv\Lib\site-packages\win32com\demos\excelRTDServer.py
C:\dev\measurementlink-python\.venv\Lib\site-packages\win32com\demos\iebutton.py
Traceback (most recent call last):
  File "C:\Users\bkeryan\.pyenv\pyenv-win\versions\3.9.13\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\bkeryan\.pyenv\pyenv-win\versions\3.9.13\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant