Skip to content

Commit

Permalink
Rename Notebook related symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed Aug 25, 2023
1 parent 281ce56 commit 9f44b81
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion crates/ruff/src/jupyter/notebook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
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
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(),
}
}
}
2 changes: 1 addition & 1 deletion crates/ruff/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ 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();
Expand Down
14 changes: 7 additions & 7 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,7 +308,7 @@ 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();
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

0 comments on commit 9f44b81

Please sign in to comment.