Indented Block Position Bug
- Dominant language
- Python
- Stars
- 1.9k
- Forks
- 229
- PR merge metrics
- No merged PRs in 30d
Description
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.
```python
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())
```
Contributor guide
Assessment
This issue has not been assessed yet.