Skip to content
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

Formatting with yapf failed if module file name is 'locale.py' #1622

Closed
fohrloop opened this issue May 5, 2018 · 2 comments
Closed

Formatting with yapf failed if module file name is 'locale.py' #1622

fohrloop opened this issue May 5, 2018 · 2 comments

Comments

@fohrloop
Copy link

fohrloop commented May 5, 2018

Issue is similar to #706

Environment data

  • VS Code version: 1.22.2
  • Extension version (available under the Extensions sidebar): 2018.4.0
  • OS and version: Windows 10, v.1803
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.6.5 (Anaconda, conda 4.5.1)
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): venv
  • Relevant/affected Python packages and their versions: yapf 0.21.0 (also 0.20.2)

Actual behavior

Format module failed when file name is locale.py.

Expected behavior

The file will be formatted, no errors in output.

Steps to reproduce:

  1. Make a file called locale.py
  2. Try to format it with yapf, in VS Code.

Logs

Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

Traceback (most recent call last):
  File "C:\Users\user\Miniconda3\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "C:\Users\user\Miniconda3\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Python\VirtualEnv\myproject\lib\site-packages\yapf\__main__.py", line 18, in <module>
    yapf.run_main()
  File "C:\Python\VirtualEnv\myproject\lib\site-packages\yapf\__init__.py", line 312, in run_main
    sys.exit(main(sys.argv))
  File "C:\Python\VirtualEnv\myproject\lib\site-packages\yapf\__init__.py", line 57, in main
    parser = argparse.ArgumentParser(description='Formatter for Python code.')
  File "C:\Users\user\Miniconda3\lib\argparse.py", line 1633, in __init__
    self._positionals = add_group(_('positional arguments'))
  File "C:\Users\user\Miniconda3\lib\gettext.py", line 612, in gettext
    return dgettext(_current_domain, message)
  File "C:\Users\user\Miniconda3\lib\gettext.py", line 575, in dgettext
    codeset=_localecodesets.get(domain))
  File "C:\Users\user\Miniconda3\lib\gettext.py", line 510, in translation
    mofiles = find(domain, localedir, languages, all=True)
  File "C:\Users\user\Miniconda3\lib\gettext.py", line 482, in find
    for nelang in _expand_lang(lang):
  File "C:\Users\user\Miniconda3\lib\gettext.py", line 206, in _expand_lang
    loc = locale.normalize(loc)

Output from Console under the Developer Tools panel (toggle Developer Tools on under Help)



Additional comments

  • As you can see, the python interpreter will import the locale.py you are formatting, not the locale from the standard library (in gettext.py) (can be easily confirmed by adding a function called normalize and making it log a critical message.)
  • Running yapf locale.py from the command line will not produce errors.
@DonJayamanne
Copy link

This is due to the fact that the locale.py file you have created is shadowing the native locale module in Python.
This will affect a number of the tools (such as pylint, etc), where they are run with the current workspace as the current working directory. Considering how python works, it will use the locale.py file in the current working directory when importing the locale module.

Try running yapf as a module and you'll get the same error:
python -m yapf locale.py.

Solution - Please rename your file from locale.py to something else.

@fohrloop
Copy link
Author

fohrloop commented May 7, 2018

Ok, thank you for your response.

It's true that running python -m yapf locale.py will give the same error. I was running python -m yapf ./subfolder/subsubfolder/locale.py without errors.

@lock lock bot locked as resolved and limited conversation to collaborators Jul 11, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants