Skip to content

Yapf breaks with type variables #1277

@svartkanin

Description

@svartkanin

Given the following example

class Test[ValueT]:
    def __init__(self, value: ValueT) -> None:
        self.value = value


t = Test[int](1)
print(t)

and running yapf -i test.py it throws an exception

(.venv) /tmp/test $ yapf -i test.py 
Traceback (most recent call last):
  File "/tmp/test/.venv/lib/python3.13/site-packages/yapf/yapflib/yapf_api.py", line 198, in FormatCode
    tree = pytree_utils.ParseCodeToTree(unformatted_source)
  File "/tmp/test/.venv/lib/python3.13/site-packages/yapf/pytree/pytree_utils.py", line 113, in ParseCodeToTree
    tree = parser_driver.parse_string(code, debug=False)
  File "/tmp/test/.venv/lib/python3.13/site-packages/yapf_third_party/_ylib2to3/pgen2/driver.py", line 187, in parse_string
    return self.parse_tokens(tokens, debug)
           ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^
  File "/tmp/test/.venv/lib/python3.13/site-packages/yapf_third_party/_ylib2to3/pgen2/driver.py", line 156, in parse_tokens
    if p.addtoken(type, value, (prefix, start)):
       ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/test/.venv/lib/python3.13/site-packages/yapf_third_party/_ylib2to3/pgen2/parse.py", line 230, in addtoken
    return self._addtoken(ilabel, type, value, context)
           ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/test/.venv/lib/python3.13/site-packages/yapf_third_party/_ylib2to3/pgen2/parse.py", line 313, in _addtoken
    raise ParseError('bad input', type, value, context)
yapf_third_party._ylib2to3.pgen2.parse.ParseError: bad input: type=9, value='[', context=('', (1, 10))

yapf version: 0.43.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions