LibCST icon indicating copy to clipboard operation
LibCST copied to clipboard

PositionProvider does not provide position for `match` statements

Open Otto-AA opened this issue 9 months ago • 1 comments

I'm using a PositionProvider to get the lines of all nodes in a Visitor. My assumption would be that the metadata exists for all nodes, however when visiting a match statement this is not true.

The following program results in a KeyError: SimpleWhitespace(value='',) on the metadata access:

import libcst as cst
from libcst.metadata import PositionProvider, MetadataWrapper

def main():
    code = """
match status:
    case 400:
        return "Bad request"
    case 404:
        return "Not found"
    case 418:
        return "I'm a teapot"
    case _:
        return "Something's wrong with the internet"
"""
    module = cst.parse_module(code)

    metadata_wrapper = MetadataWrapper(module)
    visitor = Visitor()
    module = metadata_wrapper.visit(visitor)


class Visitor(cst.CSTVisitor):
    METADATA_DEPENDENCIES = (PositionProvider,)

    def on_visit(self, node):
        print(self.get_metadata(PositionProvider, node).start.line)
        return super().on_visit(node)



if __name__ == "__main__":
    main()

I'm using libcst version 1.7.0.

Otto-AA avatar Apr 07 '25 18:04 Otto-AA

Looks like this is a bug, PositionProvider needs to be updated with match and a few other new CST nodes

zsol avatar Apr 08 '25 09:04 zsol