mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-15 23:05:40 +00:00
Maintain consistency when deserializing to JSON (#5114)
## Summary Maintain consistency while deserializing Jupyter notebook to JSON. The following changes were made: 1. Use string array to store the source value as that's the default (5781720423/nbformat/v4/nbjson.py (L56-L57)
) 2. Remove unused structs and enums 3. Reorder the keys in alphabetical order as that's the default. (5781720423/nbformat/v4/nbjson.py (L51)
) ### Side effect Removing the `preserve_order` feature means that the order of keys in JSON output (`--format json`) will be in alphabetical order. This is because the value is represented using `serde_json::Value` which internally is a `BTreeMap`, thus sorting it as per the string key. For posterity if this turns out to be not ideal, then we could define a struct representing the JSON object and the order of struct fields will determine the order in the JSON string. ## Test Plan Add a test case to assert the raw JSON string.
This commit is contained in:
parent
94abf7f088
commit
48f4f2d63d
15 changed files with 346 additions and 370 deletions
|
@ -236,93 +236,3 @@ with the relevant file contents, the `pyproject.toml` settings, and the followin
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[cfg(feature = "jupyter_notebook")]
|
||||
mod test {
|
||||
use std::path::PathBuf;
|
||||
use std::str::FromStr;
|
||||
|
||||
use anyhow::Result;
|
||||
use path_absolutize::Absolutize;
|
||||
|
||||
use ruff::logging::LogLevel;
|
||||
use ruff::resolver::{PyprojectConfig, PyprojectDiscoveryStrategy};
|
||||
use ruff::settings::configuration::{Configuration, RuleSelection};
|
||||
use ruff::settings::flags::FixMode;
|
||||
use ruff::settings::flags::{Cache, Noqa};
|
||||
use ruff::settings::types::SerializationFormat;
|
||||
use ruff::settings::AllSettings;
|
||||
use ruff::RuleSelector;
|
||||
|
||||
use crate::args::Overrides;
|
||||
use crate::printer::{Flags, Printer};
|
||||
|
||||
use super::run;
|
||||
|
||||
#[test]
|
||||
fn test_jupyter_notebook_integration() -> Result<()> {
|
||||
let overrides: Overrides = Overrides {
|
||||
select: Some(vec![
|
||||
RuleSelector::from_str("B")?,
|
||||
RuleSelector::from_str("F")?,
|
||||
]),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let mut configuration = Configuration::default();
|
||||
configuration.rule_selections.push(RuleSelection {
|
||||
select: Some(vec![
|
||||
RuleSelector::from_str("B")?,
|
||||
RuleSelector::from_str("F")?,
|
||||
]),
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
let root_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("..")
|
||||
.join("ruff")
|
||||
.join("resources")
|
||||
.join("test")
|
||||
.join("fixtures")
|
||||
.join("jupyter");
|
||||
|
||||
let diagnostics = run(
|
||||
&[root_path.join("valid.ipynb")],
|
||||
&PyprojectConfig::new(
|
||||
PyprojectDiscoveryStrategy::Fixed,
|
||||
AllSettings::from_configuration(configuration, &root_path)?,
|
||||
None,
|
||||
),
|
||||
&overrides,
|
||||
Cache::Disabled,
|
||||
Noqa::Enabled,
|
||||
FixMode::Generate,
|
||||
)?;
|
||||
|
||||
let printer = Printer::new(
|
||||
SerializationFormat::Text,
|
||||
LogLevel::Default,
|
||||
FixMode::Generate,
|
||||
Flags::SHOW_VIOLATIONS,
|
||||
);
|
||||
let mut writer: Vec<u8> = Vec::new();
|
||||
// Mute the terminal color codes.
|
||||
colored::control::set_override(false);
|
||||
printer.write_once(&diagnostics, &mut writer)?;
|
||||
// TODO(konstin): Set jupyter notebooks as none-fixable for now
|
||||
// TODO(konstin): Make jupyter notebooks fixable
|
||||
let expected = format!(
|
||||
"{valid_ipynb}:cell 1:2:5: F841 [*] Local variable `x` is assigned to but never used
|
||||
{valid_ipynb}:cell 3:1:24: B006 Do not use mutable data structures for argument defaults
|
||||
Found 2 errors.
|
||||
[*] 1 potentially fixable with the --fix option.
|
||||
",
|
||||
valid_ipynb = root_path.join("valid.ipynb").absolutize()?.display()
|
||||
);
|
||||
|
||||
assert_eq!(expected, String::from_utf8(writer)?);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue