mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-25 11:30:58 +00:00
Remove less used parser dependencies (#11718)
## Summary This PR removes the following dependencies from the `ruff_python_parser` crate: * `anyhow` (moved to dev dependencies) * `is-macro` * `itertools` The main motivation is that they aren't used much. Additionally, it updates the return type of `parse_type_annotation` to use a more specific `ParseError` instead of the generic `anyhow::Error`. ## Test Plan `cargo insta test`
This commit is contained in:
parent
f4e23d2dff
commit
a58bde6958
5 changed files with 20 additions and 20 deletions
|
@ -1,15 +1,13 @@
|
|||
//! This module takes care of parsing a type annotation.
|
||||
|
||||
use anyhow::Result;
|
||||
|
||||
use ruff_python_ast::relocate::relocate_expr;
|
||||
use ruff_python_ast::str::raw_contents;
|
||||
use ruff_python_ast::{Expr, ExprStringLiteral, StringFlags, StringLiteral};
|
||||
use ruff_text_size::Ranged;
|
||||
|
||||
use crate::{parse_expression, parse_expression_range};
|
||||
use crate::{parse_expression, parse_expression_range, ParseError};
|
||||
|
||||
#[derive(is_macro::Is, Copy, Clone, Debug)]
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub enum AnnotationKind {
|
||||
/// The annotation is defined as part a simple string literal,
|
||||
/// e.g. `x: "List[int]" = []`. Annotations within simple literals
|
||||
|
@ -24,12 +22,19 @@ pub enum AnnotationKind {
|
|||
Complex,
|
||||
}
|
||||
|
||||
impl AnnotationKind {
|
||||
/// Returns `true` if the annotation kind is simple.
|
||||
pub const fn is_simple(self) -> bool {
|
||||
matches!(self, AnnotationKind::Simple)
|
||||
}
|
||||
}
|
||||
|
||||
/// Parses the given string expression node as a type annotation. The given `source` is the entire
|
||||
/// source code.
|
||||
pub fn parse_type_annotation(
|
||||
string_expr: &ExprStringLiteral,
|
||||
source: &str,
|
||||
) -> Result<(Expr, AnnotationKind)> {
|
||||
) -> Result<(Expr, AnnotationKind), ParseError> {
|
||||
let expr_text = &source[string_expr.range()];
|
||||
|
||||
if let [string_literal] = string_expr.value.as_slice() {
|
||||
|
@ -53,7 +58,7 @@ pub fn parse_type_annotation(
|
|||
fn parse_simple_type_annotation(
|
||||
string_literal: &StringLiteral,
|
||||
source: &str,
|
||||
) -> Result<(Expr, AnnotationKind)> {
|
||||
) -> Result<(Expr, AnnotationKind), ParseError> {
|
||||
Ok((
|
||||
parse_expression_range(
|
||||
source,
|
||||
|
@ -69,7 +74,7 @@ fn parse_simple_type_annotation(
|
|||
|
||||
fn parse_complex_type_annotation(
|
||||
string_expr: &ExprStringLiteral,
|
||||
) -> Result<(Expr, AnnotationKind)> {
|
||||
) -> Result<(Expr, AnnotationKind), ParseError> {
|
||||
let mut parsed = parse_expression(string_expr.value.to_str())?.into_expr();
|
||||
relocate_expr(&mut parsed, string_expr.range());
|
||||
Ok((parsed, AnnotationKind::Complex))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue