Rename Notebook related symbols (#6862)

This PR renames the following symbols:

* `PySourceType::Jupyter` -> `PySourceType::Ipynb`
* `SourceKind::Jupyter` -> `SourceKind::IpyNotebook`
* `JupyterIndex` -> `NotebookIndex`
This commit is contained in:
Dhruv Manilawala 2023-08-25 11:40:54 +05:30 committed by GitHub
parent 61b2ffa8e8
commit d1f07008f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 41 additions and 41 deletions

View file

@ -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()

View file

@ -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<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
@ -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],
}
@ -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)?;

View file

@ -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 {

View file

@ -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<'_> {

View file

@ -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<'_> {

View file

@ -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;

View file

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

View file

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

View file

@ -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<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,

View file

@ -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,
}
}

View file

@ -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,
}
}
}