python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.46k stars 2.83k forks source link

Error when displaying error that contains unicode characters in Windows #12692

Open aleloi opened 2 years ago

aleloi commented 2 years ago

Bug Report

When displaying a type error about e.g. a variable that contains unicode characters, mypy crashes.

To Reproduce

  1. Make a file file.py containing the line x=γ.
  2. Run mypy.exe --show-column-numbers file.py through flycheck (python-mypy) in Emacs

Expected Behavior An error message like file.py:1:5: error: Name "γ" is not defined

Actual Behavior It crashes and prints a stack trace:

File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "c:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\Scripts\mypy.exe\__main__.py", line 7, in <module>
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\site-packages\mypy\__main__.py", line 15, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "mypy\main.py", line 96, in main
  File "mypy\main.py", line 173, in run_build
  File "mypy\build.py", line 180, in build
  File "mypy\build.py", line 256, in _build
  File "mypy\build.py", line 2717, in dispatch
  File "mypy\build.py", line 3048, in process_graph
  File "mypy\build.py", line 3164, in process_stale_scc
  File "mypy\main.py", line 165, in flush_errors
  File "mypy\main.py", line 199, in show_messages
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u03b3' in position 33: character maps to <undefined>

I've fixed it locally by adding

sys.stdout.reconfigure(encoding='utf-8')
sys.stderr.reconfigure(encoding='utf-8')

in mypy/__main__.py. It works for me, but I don't know whether it's the right thing for mypy.

Your Environment

Python 3.9.7, mypy 0.931 on Windows

aleloi commented 2 years ago

My 'fix' doesn't really work perfectly. Something in Windows+emacs+flycheck doesn't decode the mypy output as unicode, and what I see in Emacs is file.py:1:5: error: Name "γ" is not defined. But that's probably not a mypy issue.

aleloi commented 2 years ago

Update: I tested this with updated mypy 0.950 in Windows and Ubuntu, and couldn't reproduce by calling mypy.exe --show-column-numbers file.py in the command line. The issue happens only in flycheck in Emacs. I guess that flycheck's python-mypy runs in a special environment where stderr and stdout are opened as TextIO buffers with a non-utf-8 encoding.