Skip to content

Commit

Permalink
chore: use 0.4.9 for Ruff parser (#734)
Browse files Browse the repository at this point in the history
* chore: use 0.4.9 for Ruff parser

* fix(visitor): `level` is now an `u32`

* fix(imports): `parse` now returns `Parsed<Mod>`

* refactor(imports): use source code for naming

---------

Co-authored-by: Florian Maas <fpgmaas@gmail.com>
  • Loading branch information
mkniewallner and fpgmaas committed Jun 20, 2024
1 parent 0f0a1c6 commit 973c14c
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 36 deletions.
24 changes: 8 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ pyo3 = { version = "0.21.2", features = ["abi3-py38"] }
pyo3-log = "0.10.0"
rayon = "1.10.0"
regex = "1.10.5"
ruff_python_ast = { git = "https://github.com/astral-sh/ruff", tag = "v0.4.2" }
ruff_python_parser = { git = "https://github.com/astral-sh/ruff", tag = "v0.4.2" }
ruff_source_file = { git = "https://github.com/astral-sh/ruff", tag = "v0.4.2" }
ruff_text_size = { git = "https://github.com/astral-sh/ruff", tag = "v0.4.2" }
ruff_python_ast = { git = "https://github.com/astral-sh/ruff", tag = "v0.4.9" }
ruff_python_parser = { git = "https://github.com/astral-sh/ruff", tag = "v0.4.9" }
ruff_source_file = { git = "https://github.com/astral-sh/ruff", tag = "v0.4.9" }
ruff_text_size = { git = "https://github.com/astral-sh/ruff", tag = "v0.4.9" }
serde_json = "1.0.117"

[profile.release]
Expand Down
4 changes: 2 additions & 2 deletions src/imports/ipynb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ fn _get_imports_from_ipynb_file(path_str: &str) -> PyResult<HashMap<String, Vec<
.ok_or_else(|| PySyntaxError::new_err("Expected 'cells' to be an array"))?;
let python_code = _extract_code_from_notebook_cells(cells);

let ast = shared::get_ast_from_file_content(&python_code)?;
let imported_modules = shared::extract_imports_from_ast(ast);
let parsed = shared::parse_file_content(&python_code)?;
let imported_modules = shared::extract_imports_from_parsed_file_content(parsed);

Ok(shared::convert_imports_with_textranges_to_location_objects(
imported_modules,
Expand Down
4 changes: 2 additions & 2 deletions src/imports/py.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ pub fn get_imports_from_py_files(py: Python, file_paths: Vec<&PyString>) -> PyRe
/// Used internally by both parallel and single file processing functions.
fn _get_imports_from_py_file(path_str: &str) -> PyResult<HashMap<String, Vec<Location>>> {
let file_content = read_file(path_str)?;
let ast = shared::get_ast_from_file_content(&file_content)?;
let imported_modules = shared::extract_imports_from_ast(ast);
let ast = shared::parse_file_content(&file_content)?;
let imported_modules = shared::extract_imports_from_parsed_file_content(ast);
Ok(shared::convert_imports_with_textranges_to_location_objects(
imported_modules,
path_str,
Expand Down
24 changes: 13 additions & 11 deletions src/imports/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use pyo3::exceptions::PySyntaxError;
use pyo3::prelude::*;
use pyo3::types::{PyDict, PyList};
use ruff_python_ast::visitor::Visitor;
use ruff_python_ast::Mod;
use ruff_python_parser::{parse, Mode};
use ruff_python_ast::{Mod, ModModule};
use ruff_python_parser::{parse, Mode, Parsed};
use ruff_source_file::LineIndex;
use ruff_text_size::TextRange;
use std::collections::HashMap;
Expand All @@ -21,20 +21,22 @@ pub struct ThreadResult {
pub result: PyResult<FileToImportsMap>,
}

/// Parses the content of a Python file into an abstract syntax tree (AST).
pub fn get_ast_from_file_content(file_content: &str) -> PyResult<Mod> {
let ast =
/// Parses the content of a Python file into a parsed source code.
pub fn parse_file_content(file_content: &str) -> PyResult<Parsed<Mod>> {
let parsed =
parse(file_content, Mode::Module).map_err(|e| PySyntaxError::new_err(e.to_string()))?;
Ok(ast)
Ok(parsed)
}

/// Iterates through an AST to identify and collect import statements, and returns them together with their
/// respective `TextRange` for each occurrence.
pub fn extract_imports_from_ast(ast: Mod) -> HashMap<String, Vec<TextRange>> {
/// Iterates through a parsed source code to identify and collect import statements, and returns them
/// together with their respective `TextRange` for each occurrence.
pub fn extract_imports_from_parsed_file_content(
parsed: Parsed<Mod>,
) -> HashMap<String, Vec<TextRange>> {
let mut visitor = ImportVisitor::new();

if let Mod::Module(module) = ast {
for stmt in module.body {
if let Mod::Module(ModModule { body, .. }) = parsed.into_syntax() {
for stmt in body {
visitor.visit_stmt(&stmt);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl<'a> Visitor<'a> for ImportVisitor {
}
Stmt::ImportFrom(import_from_stmt) => {
if let Some(module) = &import_from_stmt.module {
if import_from_stmt.level == Some(0) {
if import_from_stmt.level == 0 {
self.imports
.entry(get_top_level_module_name(module.as_str()))
.or_default()
Expand Down

0 comments on commit 973c14c

Please sign in to comment.