mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:24 +00:00
Skip serializing cell ID if it's None (#6851)
<!-- Thank you for contributing to Ruff! To help us out with reviewing, please consider the following: - Does this pull request include a summary of the change? (See below.) - Does this pull request include a descriptive title? - Does this pull request include references to any relevant issues? --> ## Summary <!-- What's the purpose of the change? What does it do, and why? --> Fix #6834 ## Test Plan <!-- How was it tested? --> Need tests? --------- Co-authored-by: Dhruv Manilawala <dhruvmanila@gmail.com>
This commit is contained in:
parent
1e7d1968b1
commit
948cd29b23
4 changed files with 60 additions and 2 deletions
37
crates/ruff/resources/test/fixtures/jupyter/no_cell_id.ipynb
vendored
Normal file
37
crates/ruff/resources/test/fixtures/jupyter/no_cell_id.ipynb
vendored
Normal file
|
@ -0,0 +1,37 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import math\n",
|
||||
"import os\n",
|
||||
"\n",
|
||||
"math.pi"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python (ruff)",
|
||||
"language": "python",
|
||||
"name": "ruff"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.11.3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
|
@ -477,8 +477,10 @@ mod tests {
|
|||
use crate::jupyter::schema::Cell;
|
||||
use crate::jupyter::Notebook;
|
||||
use crate::registry::Rule;
|
||||
use crate::source_kind::SourceKind;
|
||||
use crate::test::{
|
||||
read_jupyter_notebook, test_notebook_path, test_resource_path, TestedNotebook,
|
||||
read_jupyter_notebook, test_contents, test_notebook_path, test_resource_path,
|
||||
TestedNotebook,
|
||||
};
|
||||
use crate::{assert_messages, settings};
|
||||
|
||||
|
@ -659,4 +661,22 @@ print("after empty cells")
|
|||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
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 (_, transformed) = test_contents(
|
||||
&source_kind,
|
||||
path.as_ref(),
|
||||
&settings::Settings::for_rule(Rule::UnusedImport),
|
||||
);
|
||||
let linted_notebook = transformed.into_owned().expect_jupyter();
|
||||
let mut writer = Vec::new();
|
||||
linted_notebook.write_inner(&mut writer)?;
|
||||
let actual = String::from_utf8(writer)?;
|
||||
assert!(!actual.contains(r#""id":"#));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -150,6 +150,7 @@ pub struct CodeCell {
|
|||
/// Technically, id isn't required (it's not even present) in schema v4.0 through v4.4, but
|
||||
/// it's required in v4.5. Main issue is that pycharm creates notebooks without an id
|
||||
/// <https://youtrack.jetbrains.com/issue/PY-59438/Jupyter-notebooks-created-with-PyCharm-are-missing-the-id-field-in-cells-in-the-.ipynb-json>
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub id: Option<String>,
|
||||
/// Cell-level metadata.
|
||||
pub metadata: Value,
|
||||
|
|
|
@ -112,7 +112,7 @@ pub(crate) fn max_iterations() -> usize {
|
|||
|
||||
/// A convenient wrapper around [`check_path`], that additionally
|
||||
/// asserts that autofixes converge after a fixed number of iterations.
|
||||
fn test_contents<'a>(
|
||||
pub(crate) fn test_contents<'a>(
|
||||
source_kind: &'a SourceKind,
|
||||
path: &Path,
|
||||
settings: &Settings,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue