yapf icon indicating copy to clipboard operation
yapf copied to clipboard

Support 3.12 feature: PEP 695 Type Parameter Syntax

Open joshlam123 opened this issue 2 years ago • 2 comments

Issue

In PEP-695, running under 3.12 will cause exceptions in YAPF. For example the following two lines of code for testing:

type Testing = dict[int | str]
type Testing2 = dict[str: int]

The following error message happens:

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/home/josh.lam/.local/yapf/yapf/__main__.py", line 18, in <module>
    yapf.run_main()
  File "/home/josh.lam/local/.pyenv/versions/3.12.0/lib/python3.12/site-packages/yapf/__init__.py", line 377, in run_main
    sys.exit(main(sys.argv))
             ^^^^^^^^^^^^^^
  File "/home/josh.lam/.local/.pyenv/versions/3.12.0/lib/python3.12/site-packages/yapf/__init__.py", line 136, in main
    changed = FormatFiles(
              ^^^^^^^^^^^^
  File "/home/josh.lam/.local/.pyenv/versions/3.12.0/lib/python3.12/site-packages/yapf/__init__.py", line 214, in FormatFiles
    changed |= _FormatFile(filename, lines, style_config, no_local_style,
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/josh.lam/local/.pyenv/versions/3.12.0/lib/python3.12/site-packages/yapf/__init__.py", line 248, in _FormatFile
    raise errors.YapfError(errors.FormatErrorMsg(e))
                           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/josh.lam/.local/.pyenv/versions/3.12.0/lib/python3.12/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
>>>>> In file /home/josh.lam/testing.py <<<<<
1,2d0
< type Testing = dict[int | str]
< type Testing2 = dict[str: int]

Other Info

Yapf Configuration file:

[style]
based_on_style = google

column_limit = 120
split_before_arithmetic_operator = true

joshlam123 avatar Oct 12 '23 02:10 joshlam123

Note: Mypy 1.12 now supports PEP 695, but codebases using YAPF can't use it yet.

Garrett-R avatar Oct 27 '24 03:10 Garrett-R

#1258 is an opportunity to take on this issue.

Spitfire1900 avatar Jan 05 '25 00:01 Spitfire1900