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

Unicode variable names crash yapf #403

Open
MrYann opened this issue May 4, 2017 · 10 comments
Open

Unicode variable names crash yapf #403

MrYann opened this issue May 4, 2017 · 10 comments

Comments

@MrYann
Copy link

MrYann commented May 4, 2017

Minimal working example :

def test(x):
	Δ=x+2
	print(Δ)

test(4)

this is legal python3, but yapf breaks and does nothing on a file with unicode variables. As can be seen here: the missing spaces in Δ=x+2 were not added.

The error comes from lib2to3 (lib2to3.pgen2.parse.ParseError: bad token: type=58, value='Δ', context=('', (2, 1))) but I can not say if it is a bug in 2to3, or in yapf.

@audiolion
Copy link

yapf really doesn't support python 3 fully, just what lib2to3 can handle, and this error is explicitly coming from lib2to3.

@MrYann
Copy link
Author

MrYann commented May 4, 2017 via email

@audiolion
Copy link

I just wanted to let you know that the author of yapf is a googler, google's python codebase is almost all py 2.7 and so there is little interest in developing tools or supporting python 3, so besides what can be gotten from lib2to3 there likely won't be much effort here

@bwendling
Copy link
Member

@MrYann You can add a bug report here: http://bugs.python.org/

@MrYann
Copy link
Author

MrYann commented May 7, 2017 via email

@bastih
Copy link
Contributor

bastih commented May 10, 2017

@MrYann it's lib2to3 not being completely up-to-date with the latest python version. So it's their bug.

@MrYann
Copy link
Author

MrYann commented May 13, 2017

I opened an issue regarding lib2to3.parse.classify at http://bugs.python.org/. Their reply was:

"Why? Unicode identifiers are not allowed in 2.x. I don't think lib2to3 is able or responsible to parse invalid syntax source codes"

So, maybe, could it be possible to patch the grammar invoked during the "python3" parsing so that parse.ParseError would not trigger in this case ?

@audiolion
Copy link

hmm, that does make sense, their library is for going from 2 to 3, not accepting 3 constructs, looks like yapf might have to ignore this error as you stated

@bwendling
Copy link
Member

I find their reasoning to be a bit disingenuous. They should certainly know that the lib2to3 parser is used outside of the 2to3 utility. Besides, if a Python file is only partially converted to Python 3, shouldn't the 2to3 utility still be able to convert any left over Python 2-isms?

@tttapa
Copy link

tttapa commented Jan 26, 2022

This still appears to be an issue in the latest version, and it looks like both the 2to3 parser and the yapf error handling fail.

For example:

 = 1.054571817e-34 # Js (Planck's constant over 2π)

(h̅ is U+0068 U+0305)

Yapf output:

Traceback (most recent call last):
  File "/home/user/.local/lib/python3.10/site-packages/yapf/yapflib/pytree_utils.py", line 119, in ParseCodeToTree
    tree = parser_driver.parse_string(code, debug=False)
  File "/home/user/.local/lib/python3.10/lib2to3/pgen2/driver.py", line 103, in parse_string
    return self.parse_tokens(tokens, debug)
  File "/home/user/.local/lib/python3.10/lib2to3/pgen2/driver.py", line 71, in parse_tokens
    if p.addtoken(type, value, (prefix, start)):
  File "/home/user/.local/lib/python3.10/lib2to3/pgen2/parse.py", line 119, in addtoken
    ilabel = self.classify(type, value, context)
  File "/home/user/.local/lib/python3.10/lib2to3/pgen2/parse.py", line 175, in classify
    raise ParseError("bad token", type, value, context)
lib2to3.pgen2.parse.ParseError: bad token: type=58, value='̅', context=('', (1, 1))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user/.local/lib/python3.10/site-packages/yapf/yapflib/yapf_api.py", line 183, in FormatCode
    tree = pytree_utils.ParseCodeToTree(unformatted_source)
  File "/home/user/.local/lib/python3.10/site-packages/yapf/yapflib/pytree_utils.py", line 125, in ParseCodeToTree
    tree = parser_driver.parse_string(code, debug=False)
  File "/home/user/.local/lib/python3.10/lib2to3/pgen2/driver.py", line 103, in parse_string
    return self.parse_tokens(tokens, debug)
  File "/home/user/.local/lib/python3.10/lib2to3/pgen2/driver.py", line 71, in parse_tokens
    if p.addtoken(type, value, (prefix, start)):
  File "/home/user/.local/lib/python3.10/lib2to3/pgen2/parse.py", line 119, in addtoken
    ilabel = self.classify(type, value, context)
  File "/home/user/.local/lib/python3.10/lib2to3/pgen2/parse.py", line 175, in classify
    raise ParseError("bad token", type, value, context)
lib2to3.pgen2.parse.ParseError: bad token: type=58, value='̅', context=('', (1, 1))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user/.local/lib/python3.10/site-packages/yapf/__init__.py", line 225, in _FormatFile
    reformatted_code, encoding, has_change = yapf_api.FormatFile(
  File "/home/user/.local/lib/python3.10/site-packages/yapf/yapflib/yapf_api.py", line 96, in FormatFile
    reformatted_source, changed = FormatCode(
  File "/home/user/.local/lib/python3.10/site-packages/yapf/yapflib/yapf_api.py", line 186, in FormatCode
    raise errors.YapfError(errors.FormatErrorMsg(e))
  File "/home/user/.local/lib/python3.10/site-packages/yapf/yapflib/errors.py", line 37, in FormatErrorMsg
    return '{}:{}:{}: {}'.format(e.args[1][0], e.args[1][1], e.args[1][2], e.msg)
IndexError: tuple index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user/.local/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/home/user/.local/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/home/user/.local/lib/python3.10/site-packages/yapf/__main__.py", line 18, in <module>
    yapf.run_main()
  File "/home/user/.local/lib/python3.10/site-packages/yapf/__init__.py", line 360, in run_main
    sys.exit(main(sys.argv))
  File "/home/user/.local/lib/python3.10/site-packages/yapf/__init__.py", line 124, in main
    changed = FormatFiles(
  File "/home/user/.local/lib/python3.10/site-packages/yapf/__init__.py", line 202, in FormatFiles
    changed |= _FormatFile(filename, lines, style_config, no_local_style,
  File "/home/user/.local/lib/python3.10/site-packages/yapf/__init__.py", line 236, in _FormatFile
    raise errors.YapfError(errors.FormatErrorMsg(e))
  File "/home/user/.local/lib/python3.10/site-packages/yapf/yapflib/errors.py", line 37, in FormatErrorMsg
    return '{}:{}:{}: {}'.format(e.args[1][0], e.args[1][1], e.args[1][2], e.msg)
IndexError: tuple index out of range

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants