From f006938857a24ab63b1122c67018676531521a77 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Fri, 25 Aug 2023 08:43:37 +0530 Subject: [PATCH 1/2] Rename Notebook related symbols --- crates/ruff/src/jupyter/notebook.rs | 2 +- crates/ruff/src/linter.rs | 2 +- .../pyflakes/rules/yield_outside_function.rs | 2 +- crates/ruff/src/source_kind.rs | 12 ++++++------ crates/ruff/src/test.rs | 2 +- crates/ruff_cli/src/diagnostics.rs | 16 ++++++++-------- crates/ruff_python_ast/src/lib.rs | 8 ++++---- crates/ruff_python_parser/src/lib.rs | 2 +- 8 files changed, 23 insertions(+), 23 deletions(-) diff --git a/crates/ruff/src/jupyter/notebook.rs b/crates/ruff/src/jupyter/notebook.rs index 144fa948c4149..5117e29896722 100644 --- a/crates/ruff/src/jupyter/notebook.rs +++ b/crates/ruff/src/jupyter/notebook.rs @@ -666,7 +666,7 @@ print("after empty cells") fn test_no_cell_id() -> Result<()> { let path = "no_cell_id.ipynb".to_string(); let source_notebook = read_jupyter_notebook(path.as_ref())?; - let source_kind = SourceKind::Jupyter(source_notebook); + let source_kind = SourceKind::IpyNotebook(source_notebook); let (_, transformed) = test_contents( &source_kind, path.as_ref(), diff --git a/crates/ruff/src/linter.rs b/crates/ruff/src/linter.rs index a7df255d5a845..98316154836e8 100644 --- a/crates/ruff/src/linter.rs +++ b/crates/ruff/src/linter.rs @@ -147,7 +147,7 @@ pub fn check_path( match ruff_python_parser::parse_program_tokens( tokens, &path.to_string_lossy(), - source_type.is_jupyter(), + source_type.is_ipynb(), ) { Ok(python_ast) => { if use_ast { diff --git a/crates/ruff/src/rules/pyflakes/rules/yield_outside_function.rs b/crates/ruff/src/rules/pyflakes/rules/yield_outside_function.rs index c5a7c5bc42237..c58400e5819ee 100644 --- a/crates/ruff/src/rules/pyflakes/rules/yield_outside_function.rs +++ b/crates/ruff/src/rules/pyflakes/rules/yield_outside_function.rs @@ -70,7 +70,7 @@ pub(crate) fn yield_outside_function(checker: &mut Checker, expr: &Expr) { // `await` is allowed at the top level of a Jupyter notebook. // See: https://ipython.readthedocs.io/en/stable/interactive/autoawait.html. if scope.kind.is_module() - && checker.source_type.is_jupyter() + && checker.source_type.is_ipynb() && keyword == DeferralKeyword::Await { return; diff --git a/crates/ruff/src/source_kind.rs b/crates/ruff/src/source_kind.rs index 293b312de6023..222a64847ceff 100644 --- a/crates/ruff/src/source_kind.rs +++ b/crates/ruff/src/source_kind.rs @@ -4,13 +4,13 @@ use crate::jupyter::Notebook; #[derive(Clone, Debug, PartialEq, is_macro::Is)] pub enum SourceKind { Python(String), - Jupyter(Notebook), + IpyNotebook(Notebook), } impl SourceKind { - /// Return the [`Notebook`] if the source kind is [`SourceKind::Jupyter`]. + /// Return the [`Notebook`] if the source kind is [`SourceKind::IpyNotebook`]. pub fn notebook(&self) -> Option<&Notebook> { - if let Self::Jupyter(notebook) = self { + if let Self::IpyNotebook(notebook) = self { Some(notebook) } else { None @@ -20,10 +20,10 @@ impl SourceKind { #[must_use] pub(crate) fn updated(&self, new_source: String, source_map: &SourceMap) -> Self { match self { - SourceKind::Jupyter(notebook) => { + SourceKind::IpyNotebook(notebook) => { let mut cloned = notebook.clone(); cloned.update(source_map, new_source); - SourceKind::Jupyter(cloned) + SourceKind::IpyNotebook(cloned) } SourceKind::Python(_) => SourceKind::Python(new_source), } @@ -32,7 +32,7 @@ impl SourceKind { pub fn source_code(&self) -> &str { match self { SourceKind::Python(source) => source, - SourceKind::Jupyter(notebook) => notebook.source_code(), + SourceKind::IpyNotebook(notebook) => notebook.source_code(), } } } diff --git a/crates/ruff/src/test.rs b/crates/ruff/src/test.rs index 5fa67c4eb74da..fd2b8c3f24c18 100644 --- a/crates/ruff/src/test.rs +++ b/crates/ruff/src/test.rs @@ -69,7 +69,7 @@ pub(crate) fn test_notebook_path( ) -> Result { let source_notebook = read_jupyter_notebook(path.as_ref())?; - let source_kind = SourceKind::Jupyter(source_notebook); + let source_kind = SourceKind::IpyNotebook(source_notebook); let (messages, transformed) = test_contents(&source_kind, path.as_ref(), settings); let expected_notebook = read_jupyter_notebook(expected.as_ref())?; let linted_notebook = transformed.into_owned().expect_jupyter(); diff --git a/crates/ruff_cli/src/diagnostics.rs b/crates/ruff_cli/src/diagnostics.rs index 9cb9632e41586..2d2f8ad974877 100644 --- a/crates/ruff_cli/src/diagnostics.rs +++ b/crates/ruff_cli/src/diagnostics.rs @@ -293,7 +293,7 @@ pub(crate) fn lint_path( SourceKind::Python(transformed) => { write(path, transformed.as_bytes())?; } - SourceKind::Jupyter(notebook) => { + SourceKind::IpyNotebook(notebook) => { notebook.write(path)?; } }, @@ -308,10 +308,10 @@ pub(crate) fn lint_path( stdout.write_all(b"\n")?; stdout.flush()?; } - SourceKind::Jupyter(dest_notebook) => { + SourceKind::IpyNotebook(dest_notebook) => { // We need to load the notebook again, since we might've // mutated it. - let src_notebook = source_kind.as_jupyter().unwrap(); + let src_notebook = source_kind.as_ipy_notebook().unwrap(); let mut stdout = io::stdout().lock(); for ((idx, src_cell), dest_cell) in src_notebook .cells() @@ -409,7 +409,7 @@ pub(crate) fn lint_path( ); } - let notebooks = if let SourceKind::Jupyter(notebook) = source_kind { + let notebooks = if let SourceKind::IpyNotebook(notebook) = source_kind { FxHashMap::from_iter([( path.to_str() .ok_or_else(|| anyhow!("Unable to parse filename: {:?}", path))? @@ -567,9 +567,9 @@ impl LintSources { let source_type = PySourceType::from(path); // Read the file from disk. - if source_type.is_jupyter() { + if source_type.is_ipynb() { let notebook = notebook_from_path(path).map_err(SourceExtractionError::Diagnostics)?; - let source_kind = SourceKind::Jupyter(notebook); + let source_kind = SourceKind::IpyNotebook(notebook); Ok(LintSources { source_type, source_kind, @@ -593,10 +593,10 @@ impl LintSources { ) -> Result { let source_type = path.map(PySourceType::from).unwrap_or_default(); - if source_type.is_jupyter() { + if source_type.is_ipynb() { let notebook = notebook_from_source_code(&source_code, path) .map_err(SourceExtractionError::Diagnostics)?; - let source_kind = SourceKind::Jupyter(notebook); + let source_kind = SourceKind::IpyNotebook(notebook); Ok(LintSources { source_type, source_kind, diff --git a/crates/ruff_python_ast/src/lib.rs b/crates/ruff_python_ast/src/lib.rs index d28f459dd4af4..8f8a5ad4f2ce1 100644 --- a/crates/ruff_python_ast/src/lib.rs +++ b/crates/ruff_python_ast/src/lib.rs @@ -58,7 +58,7 @@ pub enum PySourceType { #[default] Python, Stub, - Jupyter, + Ipynb, } impl PySourceType { @@ -70,8 +70,8 @@ impl PySourceType { matches!(self, PySourceType::Stub) } - pub const fn is_jupyter(&self) -> bool { - matches!(self, PySourceType::Jupyter) + pub const fn is_ipynb(&self) -> bool { + matches!(self, PySourceType::Ipynb) } } @@ -79,7 +79,7 @@ impl From<&Path> for PySourceType { fn from(path: &Path) -> Self { match path.extension() { Some(ext) if ext == "pyi" => PySourceType::Stub, - Some(ext) if ext == "ipynb" => PySourceType::Jupyter, + Some(ext) if ext == "ipynb" => PySourceType::Ipynb, _ => PySourceType::Python, } } diff --git a/crates/ruff_python_parser/src/lib.rs b/crates/ruff_python_parser/src/lib.rs index dc9396393e7ee..8cb1774d29bb1 100644 --- a/crates/ruff_python_parser/src/lib.rs +++ b/crates/ruff_python_parser/src/lib.rs @@ -313,7 +313,7 @@ impl AsMode for PySourceType { fn as_mode(&self) -> Mode { match self { PySourceType::Python | PySourceType::Stub => Mode::Module, - PySourceType::Jupyter => Mode::Jupyter, + PySourceType::Ipynb => Mode::Jupyter, } } } From 8143a918c886301afcfa6f9917c387e02d16a0aa Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Fri, 25 Aug 2023 09:14:02 +0530 Subject: [PATCH 2/2] Rename `JupyterIndex` -> `NotebookIndex` --- crates/ruff/src/jupyter/index.rs | 4 ++-- crates/ruff/src/jupyter/notebook.rs | 20 ++++++++++---------- crates/ruff/src/message/grouped.rs | 4 ++-- crates/ruff/src/message/text.rs | 4 ++-- crates/ruff/src/test.rs | 4 ++-- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/crates/ruff/src/jupyter/index.rs b/crates/ruff/src/jupyter/index.rs index 6a46d4da315c4..f5150d01f7ddb 100644 --- a/crates/ruff/src/jupyter/index.rs +++ b/crates/ruff/src/jupyter/index.rs @@ -3,14 +3,14 @@ /// When we lint a jupyter notebook, we have to translate the row/column based on /// [`ruff_text_size::TextSize`] to jupyter notebook cell/row/column. #[derive(Clone, Debug, Eq, PartialEq)] -pub struct JupyterIndex { +pub struct NotebookIndex { /// Enter a row (1-based), get back the cell (1-based) pub(super) row_to_cell: Vec, /// Enter a row (1-based), get back the row in cell (1-based) pub(super) row_to_row_in_cell: Vec, } -impl JupyterIndex { +impl NotebookIndex { /// Returns the cell number (1-based) for the given row (1-based). pub fn cell(&self, row: usize) -> Option { self.row_to_cell.get(row).copied() diff --git a/crates/ruff/src/jupyter/notebook.rs b/crates/ruff/src/jupyter/notebook.rs index 5117e29896722..8cfff728cf29d 100644 --- a/crates/ruff/src/jupyter/notebook.rs +++ b/crates/ruff/src/jupyter/notebook.rs @@ -17,7 +17,7 @@ use ruff_source_file::{NewlineWithTrailingNewline, UniversalNewlineIterator}; use ruff_text_size::{TextRange, TextSize}; use crate::autofix::source_map::{SourceMap, SourceMarker}; -use crate::jupyter::index::JupyterIndex; +use crate::jupyter::index::NotebookIndex; use crate::jupyter::schema::{Cell, RawNotebook, SortAlphabetically, SourceValue}; use crate::rules::pycodestyle::rules::SyntaxError; use crate::IOError; @@ -82,8 +82,8 @@ impl Cell { Cell::Code(cell) => &cell.source, _ => return false, }; - // Ignore cells containing cell magic. This is different from line magic - // which is allowed and ignored by the parser. + // Ignore cells containing cell magic as they act on the entire cell + // as compared to line magic which acts on a single line. !match source { SourceValue::String(string) => string .lines() @@ -106,7 +106,7 @@ pub struct Notebook { source_code: String, /// The index of the notebook. This is used to map between the concatenated /// source code and the original notebook. - index: OnceCell, + index: OnceCell, /// The raw notebook i.e., the deserialized version of JSON string. raw: RawNotebook, /// The offsets of each cell in the concatenated source code. This includes @@ -368,7 +368,7 @@ impl Notebook { /// /// The index building is expensive as it needs to go through the content of /// every valid code cell. - fn build_index(&self) -> JupyterIndex { + fn build_index(&self) -> NotebookIndex { let mut row_to_cell = vec![0]; let mut row_to_row_in_cell = vec![0]; @@ -395,7 +395,7 @@ impl Notebook { row_to_row_in_cell.extend(1..=line_count); } - JupyterIndex { + NotebookIndex { row_to_cell, row_to_row_in_cell, } @@ -413,7 +413,7 @@ impl Notebook { /// The index is built only once when required. This is only used to /// report diagnostics, so by that time all of the autofixes must have /// been applied if `--fix` was passed. - pub(crate) fn index(&self) -> &JupyterIndex { + pub(crate) fn index(&self) -> &NotebookIndex { self.index.get_or_init(|| self.build_index()) } @@ -473,7 +473,7 @@ mod tests { use anyhow::Result; use test_case::test_case; - use crate::jupyter::index::JupyterIndex; + use crate::jupyter::index::NotebookIndex; use crate::jupyter::schema::Cell; use crate::jupyter::Notebook; use crate::registry::Rule; @@ -561,7 +561,7 @@ print("after empty cells") ); assert_eq!( notebook.index(), - &JupyterIndex { + &NotebookIndex { row_to_cell: vec![0, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 5, 7, 7, 8], row_to_row_in_cell: vec![0, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 1, 1, 2, 1], } @@ -672,7 +672,7 @@ print("after empty cells") path.as_ref(), &settings::Settings::for_rule(Rule::UnusedImport), ); - let linted_notebook = transformed.into_owned().expect_jupyter(); + let linted_notebook = transformed.into_owned().expect_ipy_notebook(); let mut writer = Vec::new(); linted_notebook.write_inner(&mut writer)?; let actual = String::from_utf8(writer)?; diff --git a/crates/ruff/src/message/grouped.rs b/crates/ruff/src/message/grouped.rs index 6dc1a91aa7c67..467a5b54665f9 100644 --- a/crates/ruff/src/message/grouped.rs +++ b/crates/ruff/src/message/grouped.rs @@ -7,7 +7,7 @@ use colored::Colorize; use ruff_source_file::OneIndexed; use crate::fs::relativize_path; -use crate::jupyter::{JupyterIndex, Notebook}; +use crate::jupyter::{Notebook, NotebookIndex}; use crate::message::diff::calculate_print_width; use crate::message::text::{MessageCodeFrame, RuleCodeAndBody}; use crate::message::{ @@ -92,7 +92,7 @@ struct DisplayGroupedMessage<'a> { show_source: bool, row_length: NonZeroUsize, column_length: NonZeroUsize, - jupyter_index: Option<&'a JupyterIndex>, + jupyter_index: Option<&'a NotebookIndex>, } impl Display for DisplayGroupedMessage<'_> { diff --git a/crates/ruff/src/message/text.rs b/crates/ruff/src/message/text.rs index 7a16c756a4aed..f0f0df6f169cf 100644 --- a/crates/ruff/src/message/text.rs +++ b/crates/ruff/src/message/text.rs @@ -11,7 +11,7 @@ use ruff_source_file::{OneIndexed, SourceLocation}; use ruff_text_size::{TextRange, TextSize}; use crate::fs::relativize_path; -use crate::jupyter::{JupyterIndex, Notebook}; +use crate::jupyter::{Notebook, NotebookIndex}; use crate::line_width::{LineWidth, TabSize}; use crate::message::diff::Diff; use crate::message::{Emitter, EmitterContext, Message}; @@ -161,7 +161,7 @@ impl Display for RuleCodeAndBody<'_> { pub(super) struct MessageCodeFrame<'a> { pub(crate) message: &'a Message, - pub(crate) jupyter_index: Option<&'a JupyterIndex>, + pub(crate) jupyter_index: Option<&'a NotebookIndex>, } impl Display for MessageCodeFrame<'_> { diff --git a/crates/ruff/src/test.rs b/crates/ruff/src/test.rs index fd2b8c3f24c18..4c17d19e08c5d 100644 --- a/crates/ruff/src/test.rs +++ b/crates/ruff/src/test.rs @@ -72,7 +72,7 @@ pub(crate) fn test_notebook_path( let source_kind = SourceKind::IpyNotebook(source_notebook); let (messages, transformed) = test_contents(&source_kind, path.as_ref(), settings); let expected_notebook = read_jupyter_notebook(expected.as_ref())?; - let linted_notebook = transformed.into_owned().expect_jupyter(); + let linted_notebook = transformed.into_owned().expect_ipy_notebook(); assert_eq!( linted_notebook.cell_offsets(), @@ -86,7 +86,7 @@ pub(crate) fn test_notebook_path( Ok(TestedNotebook { messages, - source_notebook: source_kind.expect_jupyter(), + source_notebook: source_kind.expect_ipy_notebook(), linted_notebook, }) }