Skip to content

Commit

Permalink
refactor(*): handle pyo3 deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
mkniewallner committed Nov 27, 2024
1 parent b258b89 commit 622ad61
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 32 deletions.
8 changes: 4 additions & 4 deletions src/imports/ipynb.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use crate::file_utils;
use crate::location;

use super::shared;
use file_utils::read_file;
use location::Location;
use pyo3::exceptions::PySyntaxError;
use pyo3::prelude::*;
use pyo3::types::PyDict;
use rayon::prelude::*;
use std::collections::HashMap;

use super::shared;

/// Processes multiple Python files in parallel to extract import statements and their locations.
/// Accepts a list of file paths and returns a dictionary mapping module names to their import locations.
#[pyfunction]
pub fn get_imports_from_ipynb_files(py: Python, file_paths: Vec<String>) -> PyResult<PyObject> {
pub fn get_imports_from_ipynb_files(py: Python, file_paths: Vec<String>) -> Bound<'_, PyDict> {
let results: Vec<_> = file_paths
.par_iter()
.map(|path_str| {
Expand All @@ -28,7 +28,7 @@ pub fn get_imports_from_ipynb_files(py: Python, file_paths: Vec<String>) -> PyRe
let (all_imports, errors) = shared::merge_results_from_threads(results);
shared::log_python_errors_as_warnings(&errors);

shared::convert_to_python_dict(py, all_imports)
all_imports.into_pyobject(py).unwrap()
}

/// Core helper function that extracts import statements and their locations from a single .ipynb file.
Expand Down
9 changes: 5 additions & 4 deletions src/imports/py.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use crate::file_utils;
use crate::location;

use super::shared;
use file_utils::read_file;
use location::Location;
use pyo3::prelude::*;
use pyo3::types::PyDict;
use rayon::prelude::*;
use std::collections::HashMap;

use super::shared;

/// Processes multiple Python files in parallel to extract import statements and their locations.
/// Accepts a list of file paths and returns a dictionary mapping module names to their import locations.
#[pyfunction]
pub fn get_imports_from_py_files(py: Python, file_paths: Vec<String>) -> PyResult<PyObject> {
pub fn get_imports_from_py_files(py: Python, file_paths: Vec<String>) -> Bound<'_, PyDict> {
let results: Vec<_> = file_paths
.par_iter()
.map(|path_str| {
Expand All @@ -26,7 +26,8 @@ pub fn get_imports_from_py_files(py: Python, file_paths: Vec<String>) -> PyResul

let (all_imports, errors) = shared::merge_results_from_threads(results);
shared::log_python_errors_as_warnings(&errors);
shared::convert_to_python_dict(py, all_imports)

all_imports.into_pyobject(py).unwrap()
}

/// Core helper function that extracts import statements and their locations from the content of a single Python file.
Expand Down
20 changes: 0 additions & 20 deletions src/imports/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::visitor;
use location::Location;
use pyo3::exceptions::PySyntaxError;
use pyo3::prelude::*;
use pyo3::types::{PyDict, PyList};
use ruff_python_ast::visitor::Visitor;
use ruff_python_ast::{Mod, ModModule};
use ruff_python_parser::{parse, Mode, Parsed};
Expand Down Expand Up @@ -75,25 +74,6 @@ pub fn convert_imports_with_textranges_to_location_objects(
imports_with_locations
}

/// Transforms a Rust `HashMap` containing import data into a Python dictionary suitable for Python-side consumption.
pub fn convert_to_python_dict(
py: Python<'_>,
imports_with_locations: FileToImportsMap,
) -> PyResult<PyObject> {
let imports_dict = PyDict::new_bound(py);

for (module, locations) in imports_with_locations {
let py_locations: Vec<PyObject> = locations
.into_iter()
.map(|location| location.into_py(py))
.collect();
let locations_list = PyList::new_bound(py, &py_locations);
imports_dict.set_item(module, locations_list)?;
}

Ok(imports_dict.into())
}

// Shared logic for merging results from different threads.
pub fn merge_results_from_threads(results: Vec<ThreadResult>) -> (FileToImportsMap, ErrorList) {
let mut all_imports = HashMap::new();
Expand Down
7 changes: 3 additions & 4 deletions src/python_file_finder.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use ignore::types::{Types, TypesBuilder};
use ignore::{DirEntry, Walk, WalkBuilder};
use path_slash::PathExt;
use pyo3::types::PyList;
use pyo3::{pyfunction, PyObject, PyResult, Python};
use pyo3::{pyfunction, Bound, IntoPyObject, PyAny, Python};
use regex::Regex;
use std::path::PathBuf;

Expand All @@ -15,7 +14,7 @@ pub fn find_python_files(
extend_exclude: Vec<String>,
using_default_exclude: bool,
ignore_notebooks: bool,
) -> PyResult<PyObject> {
) -> Bound<'_, PyAny> {
let mut unique_directories = directories;
unique_directories.dedup();

Expand All @@ -37,7 +36,7 @@ pub fn find_python_files(
})
.collect();

Ok(PyList::new_bound(py, &python_files).into())
python_files.into_pyobject(py).unwrap()
}

fn build_walker(
Expand Down

0 comments on commit 622ad61

Please sign in to comment.