mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 20:09:22 +00:00
Workaround Even Better TOML crash related to allOf
(#15992)
## Summary Fixes https://github.com/astral-sh/ruff/issues/15978 Even Better TOML doesn't support `allOf` well. In fact, it just crashes. This PR works around this limitation by avoid using `allOf` in the automatically derived schema for the docstring formatting setting. ### Alternatives schemars introduces `allOf` whenver it sees a `$ref` alongside other object properties because this is no longer valid according to Draft 7. We could replace the visitor performing the rewrite but I prefer not to because replacing `allOf` with `oneOf` is only valid for objects that don't have any other `oneOf` or `anyOf` schema. ## Test Plan https://github.com/user-attachments/assets/25d73b2a-fee1-4ba6-9ffe-869b2c3bc64e
This commit is contained in:
parent
b66cc94f9b
commit
7cac0da44d
2 changed files with 40 additions and 27 deletions
|
@ -374,12 +374,15 @@ impl fmt::Display for DocstringCode {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Default, Eq, PartialEq, CacheKey)]
|
#[derive(Copy, Clone, Default, Eq, PartialEq, CacheKey)]
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(
|
||||||
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
|
feature = "serde",
|
||||||
#[cfg_attr(feature = "serde", serde(untagged))]
|
derive(serde::Serialize, serde::Deserialize),
|
||||||
|
serde(untagged, rename_all = "lowercase")
|
||||||
|
)]
|
||||||
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
pub enum DocstringCodeLineWidth {
|
pub enum DocstringCodeLineWidth {
|
||||||
/// Wrap docstring code examples at a fixed line width.
|
/// Wrap docstring code examples at a fixed line width.
|
||||||
|
#[cfg_attr(feature = "schemars", schemars(schema_with = "schema::fixed"))]
|
||||||
Fixed(LineWidth),
|
Fixed(LineWidth),
|
||||||
|
|
||||||
/// Respect the line length limit setting for the surrounding Python code.
|
/// Respect the line length limit setting for the surrounding Python code.
|
||||||
|
@ -388,27 +391,45 @@ pub enum DocstringCodeLineWidth {
|
||||||
feature = "serde",
|
feature = "serde",
|
||||||
serde(deserialize_with = "deserialize_docstring_code_line_width_dynamic")
|
serde(deserialize_with = "deserialize_docstring_code_line_width_dynamic")
|
||||||
)]
|
)]
|
||||||
#[cfg_attr(feature = "schemars", schemars(with = "DynamicSchema"))]
|
#[cfg_attr(feature = "schemars", schemars(schema_with = "schema::dynamic"))]
|
||||||
Dynamic,
|
Dynamic,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A dummy type that is used to generate a schema for `DocstringCodeLineWidth::Dynamic`.
|
|
||||||
#[cfg(feature = "schemars")]
|
#[cfg(feature = "schemars")]
|
||||||
struct DynamicSchema;
|
mod schema {
|
||||||
|
use ruff_formatter::LineWidth;
|
||||||
|
use schemars::gen::SchemaGenerator;
|
||||||
|
use schemars::schema::{Metadata, Schema, SubschemaValidation};
|
||||||
|
|
||||||
#[cfg(feature = "schemars")]
|
/// A dummy type that is used to generate a schema for `DocstringCodeLineWidth::Dynamic`.
|
||||||
impl schemars::JsonSchema for DynamicSchema {
|
pub(super) fn dynamic(_: &mut SchemaGenerator) -> Schema {
|
||||||
fn schema_name() -> String {
|
Schema::Object(schemars::schema::SchemaObject {
|
||||||
"Dynamic".to_string()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn json_schema(_: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
|
|
||||||
schemars::schema::SchemaObject {
|
|
||||||
instance_type: Some(schemars::schema::InstanceType::String.into()),
|
|
||||||
const_value: Some("dynamic".to_string().into()),
|
const_value: Some("dynamic".to_string().into()),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
.into()
|
|
||||||
|
// We use a manual schema for `fixed` even thought it isn't strictly necessary according to the
|
||||||
|
// JSON schema specification to work around a bug in Even Better TOML with `allOf`.
|
||||||
|
// https://github.com/astral-sh/ruff/issues/15978#issuecomment-2639547101
|
||||||
|
//
|
||||||
|
// The only difference to the automatically derived schema is that we use `oneOf` instead of
|
||||||
|
// `allOf`. There's no semantic difference between `allOf` and `oneOf` for single element lists.
|
||||||
|
pub(super) fn fixed(gen: &mut SchemaGenerator) -> Schema {
|
||||||
|
let schema = gen.subschema_for::<LineWidth>();
|
||||||
|
Schema::Object(schemars::schema::SchemaObject {
|
||||||
|
metadata: Some(Box::new(Metadata {
|
||||||
|
description: Some(
|
||||||
|
"Wrap docstring code examples at a fixed line width.".to_string(),
|
||||||
|
),
|
||||||
|
..Metadata::default()
|
||||||
|
})),
|
||||||
|
subschemas: Some(Box::new(SubschemaValidation {
|
||||||
|
one_of: Some(vec![schema]),
|
||||||
|
..SubschemaValidation::default()
|
||||||
|
})),
|
||||||
|
..Default::default()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
14
ruff.schema.json
generated
14
ruff.schema.json
generated
|
@ -879,7 +879,7 @@
|
||||||
"anyOf": [
|
"anyOf": [
|
||||||
{
|
{
|
||||||
"description": "Wrap docstring code examples at a fixed line width.",
|
"description": "Wrap docstring code examples at a fixed line width.",
|
||||||
"allOf": [
|
"oneOf": [
|
||||||
{
|
{
|
||||||
"$ref": "#/definitions/LineWidth"
|
"$ref": "#/definitions/LineWidth"
|
||||||
}
|
}
|
||||||
|
@ -887,17 +887,9 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"description": "Respect the line length limit setting for the surrounding Python code.",
|
"description": "Respect the line length limit setting for the surrounding Python code.",
|
||||||
"allOf": [
|
|
||||||
{
|
|
||||||
"$ref": "#/definitions/Dynamic"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"Dynamic": {
|
|
||||||
"type": "string",
|
|
||||||
"const": "dynamic"
|
"const": "dynamic"
|
||||||
|
}
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"Flake8AnnotationsOptions": {
|
"Flake8AnnotationsOptions": {
|
||||||
"description": "Options for the `flake8-annotations` plugin.",
|
"description": "Options for the `flake8-annotations` plugin.",
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue