Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename Notebook related symbols #6862

Merged
merged 2 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/ruff/src/jupyter/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32>,
/// Enter a row (1-based), get back the row in cell (1-based)
pub(super) row_to_row_in_cell: Vec<u32>,
}

impl JupyterIndex {
impl NotebookIndex {
/// Returns the cell number (1-based) for the given row (1-based).
pub fn cell(&self, row: usize) -> Option<u32> {
self.row_to_cell.get(row).copied()
Expand Down
22 changes: 11 additions & 11 deletions crates/ruff/src/jupyter/notebook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand All @@ -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<JupyterIndex>,
index: OnceCell<NotebookIndex>,
/// 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
Expand Down Expand Up @@ -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];

Expand All @@ -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,
}
Expand All @@ -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())
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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],
}
Expand Down Expand Up @@ -666,13 +666,13 @@ 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(),
&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)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff/src/message/grouped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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<'_> {
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff/src/message/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<'_> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions crates/ruff/src/source_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
}
Expand All @@ -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(),
}
}
}
6 changes: 3 additions & 3 deletions crates/ruff/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ pub(crate) fn test_notebook_path(
) -> Result<TestedNotebook> {
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();
let linted_notebook = transformed.into_owned().expect_ipy_notebook();

assert_eq!(
linted_notebook.cell_offsets(),
Expand All @@ -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,
})
}
Expand Down
16 changes: 8 additions & 8 deletions crates/ruff_cli/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
}
},
Expand All @@ -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()
Expand Down Expand Up @@ -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))?
Expand Down Expand Up @@ -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,
Expand All @@ -593,10 +593,10 @@ impl LintSources {
) -> Result<LintSources, SourceExtractionError> {
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,
Expand Down
8 changes: 4 additions & 4 deletions crates/ruff_python_ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub enum PySourceType {
#[default]
Python,
Stub,
Jupyter,
Ipynb,
}

impl PySourceType {
Expand All @@ -70,16 +70,16 @@ 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)
}
}

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,
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_python_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}
Expand Down
Loading