File tree Expand file tree Collapse file tree 4 files changed +46
-1
lines changed
Expand file tree Collapse file tree 4 files changed +46
-1
lines changed Original file line number Diff line number Diff line change @@ -85,4 +85,4 @@ def _format_files(self, filepaths: List[Path]) -> None:
8585 is_changed = self ._format_file (file ) or is_changed
8686
8787 if not is_changed :
88- print ("Nothing to do! All docstrings are correct 🎉" )
88+ utils . _print_to_console ("Nothing to do! All docstrings are correct 🎉\n " )
Original file line number Diff line number Diff line change 1111from pydocstringformatter .utils .file_diference import _generate_diff
1212from pydocstringformatter .utils .find_docstrings import _is_docstring
1313from pydocstringformatter .utils .find_python_file import _find_python_files
14+ from pydocstringformatter .utils .output import _print_to_console
1415
1516__all__ = [
1617 "_find_python_files" ,
2223 "PydocstringFormatterError" ,
2324 "_register_arguments" ,
2425 "TomlParsingError" ,
26+ "_print_to_console" ,
2527]
Original file line number Diff line number Diff line change 1+ import sys
2+
3+
4+ def _encode_string (string : str ) -> bytes :
5+ """Encode a string to utf-8.
6+
7+ This can be used to circumvent the issue of the standard encoding
8+ of a windows console not being utf-8.
9+ See: https://github.com/DanielNoord/pydocstringformatter/issues/13
10+ """
11+ return string .encode ("utf-8" )
12+
13+
14+ def _print_to_console (string : str ) -> None :
15+ """Print a string to the console while handling edge cases.
16+
17+ This should be used instead of print() whenever we want to
18+ print emoji's or non-ASCII characters.
19+ """
20+ sys .stdout .buffer .write (_encode_string (string ))
Original file line number Diff line number Diff line change 1+ import sys
12import tokenize
23from pathlib import Path
34from typing import List , Tuple
45
6+ import pytest
7+
8+ import pydocstringformatter
59from pydocstringformatter .utils import _find_python_files , _is_docstring
610
711HERE = Path (__file__ )
@@ -133,3 +137,22 @@ def test_module_docstrings(self) -> None:
133137 docstrings .append ((tokeninfo .start , tokeninfo .end ))
134138
135139 assert docstrings == [((3 , 0 ), (4 , 3 ))]
140+
141+
142+ def test_encoding_of_console_messages (
143+ capsys : pytest .CaptureFixture [str ], test_file : str
144+ ) -> None :
145+ """Test that we can print emoji's to non utf-8 consoles.
146+
147+ Regression test for:
148+ https://github.com/DanielNoord/pydocstringformatter/issues/13
149+ """
150+ sys .stdout .reconfigure (encoding = "cp1252" ) # type: ignore[attr-defined]
151+ with open (test_file , "w" , encoding = "utf-8" ) as file :
152+ file .write ('"""A multi-line\n docstring\n """' )
153+
154+ pydocstringformatter .run_docstring_formatter ([test_file , "--write" ])
155+
156+ output = capsys .readouterr ()
157+ assert output .out == "Nothing to do! All docstrings are correct 🎉\n "
158+ assert not output .err
You can’t perform that action at this time.
0 commit comments