mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-09 13:18:18 +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
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -2249,8 +2249,6 @@ dependencies = [
|
||||||
"bitflags 2.5.0",
|
"bitflags 2.5.0",
|
||||||
"bstr",
|
"bstr",
|
||||||
"insta",
|
"insta",
|
||||||
"is-macro",
|
|
||||||
"itertools 0.13.0",
|
|
||||||
"memchr",
|
"memchr",
|
||||||
"ruff_python_ast",
|
"ruff_python_ast",
|
||||||
"ruff_python_trivia",
|
"ruff_python_trivia",
|
||||||
|
|
|
@ -17,11 +17,8 @@ ruff_python_ast = { workspace = true }
|
||||||
ruff_python_trivia = { workspace = true }
|
ruff_python_trivia = { workspace = true }
|
||||||
ruff_text_size = { workspace = true }
|
ruff_text_size = { workspace = true }
|
||||||
|
|
||||||
anyhow = { workspace = true }
|
|
||||||
bitflags = { workspace = true }
|
bitflags = { workspace = true }
|
||||||
bstr = { workspace = true }
|
bstr = { workspace = true }
|
||||||
is-macro = { workspace = true }
|
|
||||||
itertools = { workspace = true }
|
|
||||||
memchr = { workspace = true }
|
memchr = { workspace = true }
|
||||||
rustc-hash = { workspace = true }
|
rustc-hash = { workspace = true }
|
||||||
static_assertions = { workspace = true }
|
static_assertions = { workspace = true }
|
||||||
|
@ -33,6 +30,7 @@ unicode-normalization = { workspace = true }
|
||||||
ruff_source_file = { workspace = true }
|
ruff_source_file = { workspace = true }
|
||||||
|
|
||||||
annotate-snippets = { workspace = true }
|
annotate-snippets = { workspace = true }
|
||||||
|
anyhow = { workspace = true }
|
||||||
insta = { workspace = true, features = ["glob"] }
|
insta = { workspace = true, features = ["glob"] }
|
||||||
walkdir = { workspace = true }
|
walkdir = { workspace = true }
|
||||||
|
|
||||||
|
|
|
@ -6,16 +6,17 @@
|
||||||
//!
|
//!
|
||||||
//! [Lexical analysis]: https://docs.python.org/3/reference/lexical_analysis.html
|
//! [Lexical analysis]: https://docs.python.org/3/reference/lexical_analysis.html
|
||||||
|
|
||||||
use std::{char, cmp::Ordering, str::FromStr};
|
use std::cmp::Ordering;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
use bitflags::bitflags;
|
use bitflags::bitflags;
|
||||||
|
use unicode_ident::{is_xid_continue, is_xid_start};
|
||||||
|
use unicode_normalization::UnicodeNormalization;
|
||||||
|
|
||||||
use ruff_python_ast::str::Quote;
|
use ruff_python_ast::str::Quote;
|
||||||
use ruff_python_ast::str_prefix::{
|
use ruff_python_ast::str_prefix::{
|
||||||
AnyStringPrefix, ByteStringPrefix, FStringPrefix, StringLiteralPrefix,
|
AnyStringPrefix, ByteStringPrefix, FStringPrefix, StringLiteralPrefix,
|
||||||
};
|
};
|
||||||
use unicode_ident::{is_xid_continue, is_xid_start};
|
|
||||||
use unicode_normalization::UnicodeNormalization;
|
|
||||||
|
|
||||||
use ruff_python_ast::{AnyStringFlags, Int, IpyEscapeKind, StringFlags};
|
use ruff_python_ast::{AnyStringFlags, Int, IpyEscapeKind, StringFlags};
|
||||||
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};
|
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};
|
||||||
|
|
||||||
|
|
|
@ -73,7 +73,6 @@ pub use crate::token::TokenKind;
|
||||||
|
|
||||||
use crate::parser::Parser;
|
use crate::parser::Parser;
|
||||||
|
|
||||||
use itertools::Itertools;
|
|
||||||
use ruff_python_ast::{Expr, Mod, ModExpression, ModModule, PySourceType, Suite};
|
use ruff_python_ast::{Expr, Mod, ModExpression, ModModule, PySourceType, Suite};
|
||||||
use ruff_python_trivia::CommentRanges;
|
use ruff_python_trivia::CommentRanges;
|
||||||
use ruff_text_size::{Ranged, TextRange, TextSize};
|
use ruff_text_size::{Ranged, TextRange, TextSize};
|
||||||
|
@ -388,9 +387,8 @@ impl Tokens {
|
||||||
let end = *self.first_unknown_or_len.get_or_init(|| {
|
let end = *self.first_unknown_or_len.get_or_init(|| {
|
||||||
self.raw
|
self.raw
|
||||||
.iter()
|
.iter()
|
||||||
.find_position(|token| token.kind() == TokenKind::Unknown)
|
.position(|token| token.kind() == TokenKind::Unknown)
|
||||||
.map(|(idx, _)| idx)
|
.unwrap_or(self.raw.len())
|
||||||
.unwrap_or_else(|| self.raw.len())
|
|
||||||
});
|
});
|
||||||
&self.raw[..end]
|
&self.raw[..end]
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +1,13 @@
|
||||||
//! This module takes care of parsing a type annotation.
|
//! This module takes care of parsing a type annotation.
|
||||||
|
|
||||||
use anyhow::Result;
|
|
||||||
|
|
||||||
use ruff_python_ast::relocate::relocate_expr;
|
use ruff_python_ast::relocate::relocate_expr;
|
||||||
use ruff_python_ast::str::raw_contents;
|
use ruff_python_ast::str::raw_contents;
|
||||||
use ruff_python_ast::{Expr, ExprStringLiteral, StringFlags, StringLiteral};
|
use ruff_python_ast::{Expr, ExprStringLiteral, StringFlags, StringLiteral};
|
||||||
use ruff_text_size::Ranged;
|
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 {
|
pub enum AnnotationKind {
|
||||||
/// The annotation is defined as part a simple string literal,
|
/// The annotation is defined as part a simple string literal,
|
||||||
/// e.g. `x: "List[int]" = []`. Annotations within simple literals
|
/// e.g. `x: "List[int]" = []`. Annotations within simple literals
|
||||||
|
@ -24,12 +22,19 @@ pub enum AnnotationKind {
|
||||||
Complex,
|
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
|
/// Parses the given string expression node as a type annotation. The given `source` is the entire
|
||||||
/// source code.
|
/// source code.
|
||||||
pub fn parse_type_annotation(
|
pub fn parse_type_annotation(
|
||||||
string_expr: &ExprStringLiteral,
|
string_expr: &ExprStringLiteral,
|
||||||
source: &str,
|
source: &str,
|
||||||
) -> Result<(Expr, AnnotationKind)> {
|
) -> Result<(Expr, AnnotationKind), ParseError> {
|
||||||
let expr_text = &source[string_expr.range()];
|
let expr_text = &source[string_expr.range()];
|
||||||
|
|
||||||
if let [string_literal] = string_expr.value.as_slice() {
|
if let [string_literal] = string_expr.value.as_slice() {
|
||||||
|
@ -53,7 +58,7 @@ pub fn parse_type_annotation(
|
||||||
fn parse_simple_type_annotation(
|
fn parse_simple_type_annotation(
|
||||||
string_literal: &StringLiteral,
|
string_literal: &StringLiteral,
|
||||||
source: &str,
|
source: &str,
|
||||||
) -> Result<(Expr, AnnotationKind)> {
|
) -> Result<(Expr, AnnotationKind), ParseError> {
|
||||||
Ok((
|
Ok((
|
||||||
parse_expression_range(
|
parse_expression_range(
|
||||||
source,
|
source,
|
||||||
|
@ -69,7 +74,7 @@ fn parse_simple_type_annotation(
|
||||||
|
|
||||||
fn parse_complex_type_annotation(
|
fn parse_complex_type_annotation(
|
||||||
string_expr: &ExprStringLiteral,
|
string_expr: &ExprStringLiteral,
|
||||||
) -> Result<(Expr, AnnotationKind)> {
|
) -> Result<(Expr, AnnotationKind), ParseError> {
|
||||||
let mut parsed = parse_expression(string_expr.value.to_str())?.into_expr();
|
let mut parsed = parse_expression(string_expr.value.to_str())?.into_expr();
|
||||||
relocate_expr(&mut parsed, string_expr.range());
|
relocate_expr(&mut parsed, string_expr.range());
|
||||||
Ok((parsed, AnnotationKind::Complex))
|
Ok((parsed, AnnotationKind::Complex))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue