mirror of https://github.com/astral-sh/ruff
Generate custom JSON schema for dynamic setting (#9632)
## Summary If you paste in the TOML for our default configuration (from the docs), it's rejected by our JSON Schema:  It seems like the issue is with: ```toml # Set the line length limit used when formatting code snippets in # docstrings. # # This only has an effect when the `docstring-code-format` setting is # enabled. docstring-code-line-length = "dynamic" ``` Specifically, since that value uses a custom Serde implementation, I guess Schemars bails out? This PR adds a custom representation to allow `"dynamic"` (but no other strings):  This seems like it should work but I don't have a great way to test it. Closes https://github.com/astral-sh/ruff/issues/9630.
This commit is contained in:
parent
87821252d7
commit
b0d6fd7343
|
|
@ -1,10 +1,11 @@
|
||||||
|
use std::fmt;
|
||||||
|
use std::path::Path;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
use ruff_formatter::printer::{LineEnding, PrinterOptions, SourceMapGeneration};
|
use ruff_formatter::printer::{LineEnding, PrinterOptions, SourceMapGeneration};
|
||||||
use ruff_formatter::{FormatOptions, IndentStyle, IndentWidth, LineWidth};
|
use ruff_formatter::{FormatOptions, IndentStyle, IndentWidth, LineWidth};
|
||||||
use ruff_macros::CacheKey;
|
use ruff_macros::CacheKey;
|
||||||
use ruff_python_ast::PySourceType;
|
use ruff_python_ast::PySourceType;
|
||||||
use std::fmt;
|
|
||||||
use std::path::Path;
|
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
/// Resolved options for formatting one individual file. The difference to `FormatterSettings`
|
/// Resolved options for formatting one individual file. The difference to `FormatterSettings`
|
||||||
/// is that `FormatterSettings` stores the settings for multiple files (the entire project, a subdirectory, ..)
|
/// is that `FormatterSettings` stores the settings for multiple files (the entire project, a subdirectory, ..)
|
||||||
|
|
@ -367,15 +368,39 @@ impl fmt::Display for DocstringCode {
|
||||||
#[cfg_attr(feature = "serde", serde(untagged))]
|
#[cfg_attr(feature = "serde", serde(untagged))]
|
||||||
#[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.
|
||||||
Fixed(LineWidth),
|
Fixed(LineWidth),
|
||||||
|
|
||||||
|
/// Respect the line length limit setting for the surrounding Python code.
|
||||||
#[default]
|
#[default]
|
||||||
#[cfg_attr(
|
#[cfg_attr(
|
||||||
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"))]
|
||||||
Dynamic,
|
Dynamic,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A dummy type that is used to generate a schema for `DocstringCodeLineWidth::Dynamic`.
|
||||||
|
#[cfg(feature = "schemars")]
|
||||||
|
struct DynamicSchema;
|
||||||
|
|
||||||
|
#[cfg(feature = "schemars")]
|
||||||
|
impl schemars::JsonSchema for DynamicSchema {
|
||||||
|
fn schema_name() -> String {
|
||||||
|
"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()),
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
.into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl fmt::Debug for DocstringCodeLineWidth {
|
impl fmt::Debug for DocstringCodeLineWidth {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match *self {
|
match *self {
|
||||||
|
|
|
||||||
|
|
@ -750,13 +750,27 @@
|
||||||
"DocstringCodeLineWidth": {
|
"DocstringCodeLineWidth": {
|
||||||
"anyOf": [
|
"anyOf": [
|
||||||
{
|
{
|
||||||
"$ref": "#/definitions/LineWidth"
|
"description": "Wrap docstring code examples at a fixed line width.",
|
||||||
|
"allOf": [
|
||||||
|
{
|
||||||
|
"$ref": "#/definitions/LineWidth"
|
||||||
|
}
|
||||||
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type": "null"
|
"description": "Respect the line length limit setting for the surrounding Python code.",
|
||||||
|
"allOf": [
|
||||||
|
{
|
||||||
|
"$ref": "#/definitions/Dynamic"
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
"Dynamic": {
|
||||||
|
"type": "string",
|
||||||
|
"const": "dynamic"
|
||||||
|
},
|
||||||
"Flake8AnnotationsOptions": {
|
"Flake8AnnotationsOptions": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue