LibCST
LibCST copied to clipboard
PositionProvider does not provide position for `match` statements
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.
Looks like this is a bug, PositionProvider needs to be updated with match and a few other new CST nodes