Line Operations¶
Work with specific lines for fine-grained control.
Line Targets¶
Single Line¶
Line Range¶
Code Block at Line¶
Find the enclosing structure:
block = file.block_at_line(42)
print(block.kind) # "class", "function", "method", "if", "for", "while", "try", "with"
print(block.name) # Name if applicable (class/function name)
print(block.start_line) # First line of the block
print(block.end_line) # Last line of the block
Linting Directives¶
Type Ignore (mypy)¶
line = file.line(42)
# Add type: ignore
line.add_type_ignore() # Generic
line.add_type_ignore("arg-type") # Specific code
line.add_type_ignore(["arg-type", "return-value"]) # Multiple codes
line.add_type_ignore("arg-type", reason="Legacy API") # With reason
# Remove
line.remove_type_ignore()
Noqa (flake8, ruff)¶
line.add_noqa() # Blanket noqa
line.add_noqa("E501") # Specific code
line.add_noqa(["E501", "F401"]) # Multiple codes
line.remove_noqa()
Pylint¶
Formatter (Black)¶
Coverage¶
Line Block Operations¶
Content Access¶
Moving Lines¶
# Move to another position in the same file
block.move_to(100)
# Move to another file (insert at the given line in the destination)
block.move_to_file("other.py", line_number=10)
Rewriting¶
Inserting¶
Deleting¶
Indentation¶
block.indent() # Indent by 1 level
block.indent(2) # Indent by 2 levels
block.dedent() # Dedent by 1 level
block.dedent(2) # Dedent by 2 levels
Directive Wrapping¶
Wrap a block of lines with directives:
block = file.lines(10, 20)
# Wrap with fmt: off/on
block.wrap_with_fmt_off()
# Wrap with pylint disable/enable
block.wrap_with_pylint_disable(["C0114", "C0115"])
# Wrap with pragma: no cover
block.wrap_with_no_cover()
Converting to Line Block¶
Code blocks can be converted to line blocks for raw operations:
code_block = file.block_at_line(42)
line_block = code_block.to_line_block()
# Now you can use line block operations
line_block.indent()
line_block.move_to(100)
Project-Wide Directive Operations¶
Finding Directives¶
# All type: ignore comments
rj.find_type_ignores()
# Bare ignores (without specific codes)
rj.find_bare_type_ignores()
# Other directive types
rj.find_noqa_comments()
rj.find_bare_noqa()
rj.find_pylint_disables()
rj.find_no_cover()
# Everything
rj.find_all_directives()
Cleaning Up¶
# Remove all directives of a given kind across the project
rj.remove_all_type_ignores()
rj.remove_all_noqa()
# Remove every linting directive
rj.remove_all_directives()
Auditing¶
# Counts by directive type
stats = rj.count_directives_by_type()
# Full audit report
rj.audit_directives()
Next Steps¶
- Batch Operations — Apply operations to multiple targets
- Error Handling — Handle failures gracefully