Skip to content

Indented Block Position Bug #1380

Description

@AryazE

When using PositionProvider on an IndentedBlock I expect to get the code range of the whole indented block.
However, if the indented block starts with a FunctionDef which has decorators, the indented block's code range also starts after the decorators, where the function definition starts.
I have added the following test to libcst/metadata/tests/test_position_provider.py under class PositionProviderTest and it fails.

    def test_indented_block_starting_with_decorated_function_def(self) -> None:
        """
        Tests that the position provider correctly computes positions in an indented block
        starting with a decorated function definition.
        """
        test = self

        class IndentedBlockVisitor(CSTVisitor):
            METADATA_DEPENDENCIES = (PositionProvider,)

            def visit_IndentedBlock(self, node: cst.IndentedBlock) -> None:
                test.assertEqual(
                    self.get_metadata(PositionProvider, node),
                    CodeRange((2, 4), (4, 15)),
                )

        wrapper = MetadataWrapper(
            parse_module("def foo():\n    @decorator\n    def func(): return 42\n    return func")
        )
        wrapper.visit(IndentedBlockVisitor())

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions