MCPcopy Create free account
hub / github.com/astral-sh/ruff / parse_string_annotation

Function parse_string_annotation

crates/ruff_python_parser/src/lib.rs:219–233  ·  view source on GitHub ↗

Parses a Python expression from a string annotation. # Example Parsing a string annotation: ``` use ruff_python_parser::parse_string_annotation; use ruff_python_ast::{StringLiteral, StringLiteralFlags, AtomicNodeIndex}; use ruff_text_size::{TextRange, TextSize}; let string = StringLiteral { value: "'''\n int | str'''".to_string().into_boxed_str(), flags: StringLiteralFlags::empty(), range: Tex

(
    source: &str,
    string: &StringLiteral,
)

Source from the content-addressed store, hash-verified

217/// assert!(!parsed.is_ok());
218/// ```
219pub fn parse_string_annotation(
220 source: &str,
221 string: &StringLiteral,
222) -> Result<Parsed<ModExpression>, ParseError> {
223 let range = string
224 .range()
225 .add_start(string.flags.opener_len())
226 .sub_end(string.flags.closer_len());
227 let source = &source[..range.end().to_usize()];
228 if string.flags.is_triple_quoted() {
229 parse_parenthesized_expression_range(source, range)
230 } else {
231 parse_expression_range(source, range)
232 }
233}
234
235/// Parse the given Python source code using the specified [`ParseOptions`].
236///

Callers 2

parsed_string_annotationFunction · 0.50

Calls 10

parse_expression_rangeFunction · 0.85
sub_endMethod · 0.80
opener_lenMethod · 0.80
closer_lenMethod · 0.80
to_usizeMethod · 0.80
add_startMethod · 0.45
rangeMethod · 0.45
endMethod · 0.45
is_triple_quotedMethod · 0.45

Tested by

no test coverage detected