Skip to content

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

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

Closed
tadeu opened this issue Aug 26, 2021 · 1 comment
Closed
Labels
bug mypy got something wrong topic-stubgen

Comments

@tadeu
Copy link
Contributor

tadeu commented Aug 26, 2021

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?

@sterliakov
Copy link
Collaborator

sterliakov commented May 12, 2025

Doesn't reproduce on current master, stubgen handles that case now and generates the correct stub file (as of 1.15.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-stubgen
Projects
None yet
Development

No branches or pull requests

3 participants