From fab65e5208ebde5fb2641b8f36ea717257045c71 Mon Sep 17 00:00:00 2001 From: Ilja Orlovs Date: Wed, 13 Oct 2021 11:22:06 +0100 Subject: [PATCH] satisfying the linter --- src/flake8/formatting/base.py | 15 ++++++++------- tests/unit/test_base_formatter.py | 5 +++-- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/flake8/formatting/base.py b/src/flake8/formatting/base.py index a13746d..f6c794b 100644 --- a/src/flake8/formatting/base.py +++ b/src/flake8/formatting/base.py @@ -179,24 +179,25 @@ class BaseFormatter: # one return f"{error.physical_line}{indent}^" - def _stdoutWriteHook(self, value: str) -> None: - """An alternative to print() function that handles output terminal encoding.""" + def _stdout_write_hook(self, value: str) -> None: + """sys.stdout.write() with fallbacks.""" try: sys.stdout.write(value) except UnicodeEncodeError: - byteValue = value.encode(sys.stdout.encoding, 'backslashreplace') + byte_value = value.encode(sys.stdout.encoding, 'backslashreplace') if hasattr(sys.stdout, 'buffer'): - sys.stdout.buffer.write(byteValue) + sys.stdout.buffer.write(byte_value) else: - sys.stdout.write(byteValue.decode(sys.stdout.encoding, 'strict')) + sys.stdout.write( + byte_value.decode(sys.stdout.encoding, 'strict')) def _write(self, output: str) -> None: """Handle logic of whether to use an output file or print().""" if self.output_fd is not None: self.output_fd.write(output + self.newline) if self.output_fd is None or self.options.tee: - self._stdoutWriteHook(output) - self._stdoutWriteHook(self.newline) + self._stdout_write_hook(output) + self._stdout_write_hook(self.newline) def write(self, line: Optional[str], source: Optional[str]) -> None: """Write the line either to the output file or stdout. diff --git a/tests/unit/test_base_formatter.py b/tests/unit/test_base_formatter.py index fbe84d8..9df3449 100644 --- a/tests/unit/test_base_formatter.py +++ b/tests/unit/test_base_formatter.py @@ -138,6 +138,7 @@ def test_write_produces_stdout(capsys): @pytest.mark.parametrize('buffered_stdout', [True, False]) def test_write_hook_fallbacks(buffered_stdout): + """Varify stdout.write() fallbacks.""" mock_line = mock.Mock(name='Mock Line') stdout_spec = ['write', 'encoding'] @@ -145,11 +146,11 @@ def test_write_hook_fallbacks(buffered_stdout): stdout_spec.append('buffer') with mock.patch('sys.stdout', spec=stdout_spec) as mock_stdout: - def _stdoutWriteEffect(value): + def _stdout_write_effect(value): if value is mock_line: raise UnicodeEncodeError('unittest-codec', u'', 42, 43, 'NOPE') return None - mock_stdout.write.side_effect = _stdoutWriteEffect + mock_stdout.write.side_effect = _stdout_write_effect mock_stdout.encoding = 'ascii'