| title | PyCodeBlock |
|---|---|
| sidebarTitle | PyCodeBlock |
| icon | |
| description | Extends CodeBlock for Python codebases. |
import {Parameter} from '/snippets/Parameter.mdx'; import {ParameterWrapper} from '/snippets/ParameterWrapper.mdx'; import {Return} from '/snippets/Return.mdx'; import {HorizontalDivider} from '/snippets/HorizontalDivider.mdx'; import {GithubLinkNote} from '/snippets/GithubLinkNote.mdx'; import {Attribute} from '/snippets/Attribute.mdx';
CodeBlock, Expression, Editable
### assignment_statements list[ PyAssignmentStatement [ PyCodeBlock , PyAssignment ]] } description="Returns list of top level assignment statements in the code block." /> list[ PyAssignment [ Editable , PyCodeBlock ]] } description="Returns all assignments in the code block across all nesting levels." /> list[ PyAttribute [ Editable , PyCodeBlock ]] } description="Returns a list of top level class attribute statements in the code block." /> list[ PyComment [ Editable , PyCodeBlock ]] } description="Gets list of top level comments in the code block." /> SymbolGroup } description="Returns a SymbolGroup of all extended nodes associated with this element." /> str } description="Returns the source text representation of all extended nodes." /> PyFile } description="The file object that this Editable instance belongs to." /> str } description="The file path of the file that this Editable instance belongs to." /> list[ FunctionCall ] } description="Returns a list of all function calls in the code block." /> list[ PyIfBlockStatement [ PyCodeBlock ]] } description="Returns a list of top level if statements in the code block." /> int } description="The indentation level of the code block." /> list[ PyAssignment [ Editable , PyCodeBlock ]] } description="Returns all local variable assignment in the code block, for all nest levels." /> Editable } description="The parent node of this Editable instance." /> PyCodeBlock | None } description="The parent code block containing this block, or None if it is a top-level block." /> PyClass | None } description="Find the class this node is contained in" /> PyFunction | None } description="Find the function this node is contained in" /> Statement | None } description="Find the statement this node is contained in" /> Expression | list[ Expression ] } description="Returns the resolved type of an Expression." /> list[ ReturnStatement [ PyCodeBlock ]] } description="Gets all return statements at the top level of the code block." /> str } description="Text representation of the Editable instance." /> MultiLineCollection [ Statement , PyCodeBlock ] } description="Gets a view of the top-level statements in the code block." /> list[ SymbolStatement ] } description="Returns list of top level symbol statements in the code block." /> list[ Editable ] } description="Returns Editables for all TreeSitter node instances of variable usages within this node's" /> list[ WithStatement ] } description="Returns a list of all 'with' statements within the code block." /> ### ancestors Find all ancestors of the node of the given type. Does not return itself<Return return_type={ <>list[ <a href="/api-reference/core/Editable" style={ {fontWeight: "inherit", fontSize: "inherit"} }>Editable ]</> } description=""/>
Replace the source of this Editable with new_src.
<Return return_type={ None } description=""/>
Find and return matching nodes or substrings within an Editable instance.
Union[list[str], str] } description="One or more strings to search for." defaultValue="" /> bool } description="If True, only return nodes whose source exactly matches one of the strings_to_match." defaultValue="False" /><Return return_type={ <>list[ <a href="/api-reference/core/Editable" style={ {fontWeight: "inherit", fontSize: "inherit"} }>Editable ]</> } description="A list of Editable instances that match the search criteria."/>
Returns a list of string literals within this node's source that match any of the given
list[str] } description="A list of strings to search for in string literals." defaultValue="" /> bool } description="If True, matches substrings within string literals. If False, only matches exact strings. Defaults to False." defaultValue="False" /><Return return_type={ <>list[ <a href="/api-reference/core/Editable" style={ {fontWeight: "inherit", fontSize: "inherit"} }>Editable [ <a href="/api-reference/python/PyCodeBlock" style={ {fontWeight: "inherit", fontSize: "inherit"} }>PyCodeBlock ]]</> } description="A list of Editable objects representing the matching string literals."/>
Adds a visual flag comment to the end of this Editable's source text.
<Return return_type={ <>CodeFlag[ <a href="/api-reference/python/PyCodeBlock" style={ {fontWeight: "inherit", fontSize: "inherit"} }>PyCodeBlock ]</> } description=""/>
Returns a list of assignments with the specified variable name.
str } description="The name of the variable to find assignments for." defaultValue="" /><Return return_type={ <>list[ <a href="/api-reference/python/PyAssignment" style={ {fontWeight: "inherit", fontSize: "inherit"} }>PyAssignment [ <a href="/api-reference/core/Editable" style={ {fontWeight: "inherit", fontSize: "inherit"} }>Editable , <a href="/api-reference/python/PyCodeBlock" style={ {fontWeight: "inherit", fontSize: "inherit"} }>PyCodeBlock ]]</> } description="A list of Assignment objects that match the variable name."/>
Returns attributes from the code block, with the option to include or exclude private
bool } description="Whether to include private attributes in the returned list. If True, returns both private and" defaultValue="" /><Return return_type={ <>list[ <a href="/api-reference/python/PyAttribute" style={ {fontWeight: "inherit", fontSize: "inherit"} }>PyAttribute [ <a href="/api-reference/core/Editable" style={ {fontWeight: "inherit", fontSize: "inherit"} }>Editable , <a href="/api-reference/python/PyCodeBlock" style={ {fontWeight: "inherit", fontSize: "inherit"} }>PyCodeBlock ]]</> } description="A list of attribute statements matching the privacy criteria."/>
Gets the first comment statement containing a specific text string.
str } description="The text string to search for within comment statements." defaultValue="" /><Return return_type={ <><a href="/api-reference/python/PyComment" style={ {fontWeight: "inherit", fontSize: "inherit"} }>PyComment [ <a href="/api-reference/core/Editable" style={ {fontWeight: "inherit", fontSize: "inherit"} }>Editable , <a href="/api-reference/python/PyCodeBlock" style={ {fontWeight: "inherit", fontSize: "inherit"} }>PyCodeBlock ] | None</> } description="The first comment statement containing the search text, or None if no matching comment is found."/>
Returns the first code statement that assigns a local variable with the specified name.
str } description="The name of the local variable to search for." defaultValue="" /><Return return_type={ <><a href="/api-reference/python/PyAssignment" style={ {fontWeight: "inherit", fontSize: "inherit"} }>PyAssignment [ <a href="/api-reference/core/Editable" style={ {fontWeight: "inherit", fontSize: "inherit"} }>Editable , <a href="/api-reference/python/PyCodeBlock" style={ {fontWeight: "inherit", fontSize: "inherit"} }>PyCodeBlock ] | None</> } description="The first matching local variable assignment, or None if no match is found."/>
Returns all instances of local variable assignments that match the specified variable
str } description="The name of the local variable to search for." defaultValue="" /> bool, optional } description="If True, matches variables whose names contain var_name." defaultValue="False" /><Return return_type={ <>list[ <a href="/api-reference/python/PyAssignment" style={ {fontWeight: "inherit", fontSize: "inherit"} }>PyAssignment [ <a href="/api-reference/core/Editable" style={ {fontWeight: "inherit", fontSize: "inherit"} }>Editable , <a href="/api-reference/python/PyCodeBlock" style={ {fontWeight: "inherit", fontSize: "inherit"} }>PyCodeBlock ]]</> } description="List of Assignment objects representing local variable assignments that match the specified name criteria."/>
Returns all statements of a given type up to the specified block level.
StatementType | None } description="The type of statements to return. If None, returns all statement types." defaultValue="None" /> int | None } description="The maximum block depth level to search. If None, searches all levels." defaultValue="None" /><Return return_type={ <>list[ <a href="/api-reference/core/Statement" style={ {fontWeight: "inherit", fontSize: "inherit"} }>Statement [ <a href="/api-reference/python/PyCodeBlock" style={ {fontWeight: "inherit", fontSize: "inherit"} }>PyCodeBlock ]]</> } description=""/>
Returns all instances of variable usages in a code block.
str } description="The name of the variable to search for." defaultValue="" /> bool } description="When True, matches on variable names that contain var_name. When False (default), only matches exact variable names." defaultValue="False" /><Return return_type={ <>list[ <a href="/api-reference/core/Editable" style={ {fontWeight: "inherit", fontSize: "inherit"} }>Editable [ <a href="/api-reference/python/PyCodeBlock" style={ {fontWeight: "inherit", fontSize: "inherit"} }>PyCodeBlock ]]</> } description="A sorted list of variable usage instances as Editable objects."/>
Gets with statements at a specific block level.
int } description="The block level to filter by. 0 represents the top level." defaultValue="" /><Return return_type={ <>list[ <a href="/api-reference/python/WithStatement" style={ {fontWeight: "inherit", fontSize: "inherit"} }>WithStatement ]</> } description="A list of WithStatement objects at the specified block level."/>
Adjusts the indentation level of the entire code block.
int } description="The number of indentation levels to adjust. Positive values indent right, negative values indent left." defaultValue="" /><Return return_type={ None } description=""/>
Inserts source code at the bottom of the code block.
str } description="The source code to insert." defaultValue="" /> bool } description="Whether to fix the indentation of the inserted code. Defaults to True." defaultValue="True" /> bool } description="Whether to add a newline before the inserted code. Defaults to True." defaultValue="True" /><Return return_type={ None } description=""/>
Inserts new source code at the top of the code block.
str } description="The source code to insert at the top of the code block." defaultValue="" /><Return return_type={ None } description=""/>
Checks if this node is a descendant of the given editable instance in the AST.
<Return return_type={ bool } description=""/>
Check if this node is contained another node of the given class
<Return return_type={ bool } description=""/>
Find the first ancestor of the node of the given type. Does not return itself
<Return return_type={ <><a href="/api-reference/core/Editable" style={ {fontWeight: "inherit", fontSize: "inherit"} }>Editable | None</> } description=""/>
Find the first ancestor of the node of the given type. Does not return itself
<Return return_type={ <><a href="/api-reference/core/Editable" style={ {fontWeight: "inherit", fontSize: "inherit"} }>Editable | None</> } description=""/>
Reduces an editable to the following condition
<Return return_type={ None } description=""/>
Deletes this Node and its related extended nodes (e.g. decorators, comments).
bool } description="Whether to delete surrounding whitespace and formatting. Defaults to True." defaultValue="True" /> int } description="Priority of the removal transaction. Higher priority transactions are executed first. Defaults to 0." defaultValue="0" /> bool } description="Whether to deduplicate removal transactions at the same location. Defaults to True." defaultValue="True" /><Return return_type={ None } description=""/>
Renames all instances of variable usages in the code block.
str } description="The current name of the variable to rename." defaultValue="" /> str } description="The new name to give the variable." defaultValue="" /> bool } description="When True, matches variables containing old_var_name. When False, only exact matches. Defaults to False." defaultValue="False" /><Return return_type={ None } description="This method mutates the code block in place."/>
Search and replace occurrences of text within this node's source and its extended nodes.
str } description="The text or pattern to search for." defaultValue="" /> str } description="The text to replace matches with." defaultValue="" /> int, optional } description="Maximum number of replacements to make. Defaults to -1 (replace all)." defaultValue="-1" /> bool, optional } description="Whether to treat 'old' as a regex pattern. Defaults to False." defaultValue="False" /> int, optional } description="Priority of the replacement operation. Defaults to 0." defaultValue="0" /><Return return_type={ int } description="The total number of replacements made."/>
Returns a list of all regex match of regex_pattern, similar to python's re.search().
<Return return_type={ <>list[ <a href="/api-reference/core/Editable" style={ {fontWeight: "inherit", fontSize: "inherit"} }>Editable ]</> } description="A list of Editable objects corresponding to the matches found."/>
Extracts a code block from its parent wrapper container by removing the wrapping
<Return return_type={ None } description=""/>
Wraps a code block with a statement and indents it.
str } description="The source code to insert before the block." defaultValue="" /> str } description="The source code to insert after the block. Defaults to an empty string." defaultValue="""" /><Return return_type={ None } description=""/>