Skip to content

stubgen produces UnicodeEncodeError and doesn't allow setting an encoding #11031

Closed
@tadeu

Description

@tadeu

Bug Report

Can't use stubgen in Windows if the source contains special characters not belonging to the current "code page".

To Reproduce

  1. In Windows, create a file example.py with UTF-8 encoding and the following contents:
def get_π():
    return 3
  1. Execute stubgen -m example -o .

Expected Behavior

Should execute normally:

Processed 1 modules
Generated .\example.pyi

Actual Behavior

Error:

Traceback (most recent call last):
  File "<env>\Scripts\stubgen-script.py", line 10, in <module>
    sys.exit(main())
  File "mypy\stubgen.py", line 1655, in main
  File "mypy\stubgen.py", line 1533, in generate_stubs
  File "mypy\stubgen.py", line 1489, in generate_stub_from_ast
  File "<env>\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 '\u03c0' in position 8: character maps to <undefined>

Your Environment

  • Mypy version used: 0.910

Notes

The problem seems to be this open here, without an encoding argument. Perhaps the encoding could be passed via command line argument, or via an environment variable?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions