Skip to content

Conversation

TaKO8Ki
Copy link
Contributor

@TaKO8Ki TaKO8Ki commented Oct 17, 2025

Summary

FIxes #19173

Update schemars to v1

I’ve verified that the fundamentals of ruff.schema.json and ty.schema.json remain unchanged following the Schemars upgrade.

The migration is based on https://graham.cool/schemars/migrating.

Shared Changes

  • Upgraded both schemas to JSON Schema draft 2020-12 andmigrated shared types into $defs, with all $ref paths now using #/$defs/....
  • Added reusable helpers such as $defs/string (and array helpers like Array_of_string) to avoid duplicating scalar/collection shapes.
  • Reformatted descriptions with consistent newline breaks. [Schemars 1.0] — since v1.0.0-alpha.3, Schemars stops collapsing doc-comment whitespace; multi-line Rust docs flow through to description verbatim. ref: Handling of newline in RustDoc GREsau/schemars#120
  • Kept existing deprecated flags but moved them earlier within each property object to match the new formatting.

ty.schema.json

  • Introduced $defs entries for path-related types (RelativePathBuf, SystemPathBuf) and updated path properties to reference them.
  • Redirected overrides to a new OverridesOptions array definition with expanded multi-line documentation and examples, while maintaining the original rule descriptions under the updated formatting.

Test Plan

Check if existing tests pass.

@TaKO8Ki TaKO8Ki force-pushed the schemars-v1-upgrade branch from 128d5b2 to 2ce146a Compare October 17, 2025 15:45
Copy link
Contributor

github-actions bot commented Oct 17, 2025

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

Copy link
Contributor

github-actions bot commented Oct 17, 2025

mypy_primer results

No ecosystem changes detected ✅
No memory usage changes detected ✅

@TaKO8Ki TaKO8Ki force-pushed the schemars-v1-upgrade branch from 2ce146a to 66af9fe Compare October 17, 2025 15:53
Copy link
Contributor

github-actions bot commented Oct 17, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@TaKO8Ki TaKO8Ki force-pushed the schemars-v1-upgrade branch from 66af9fe to 2244ab2 Compare October 17, 2025 17:39
ruff.schema.json Outdated
Comment on lines 1398 to 1408
@@ -1415,7 +1405,7 @@
"null"
],
"items": {
"type": "string"
"$ref": "#/$defs/string"
Copy link
Contributor Author

@TaKO8Ki TaKO8Ki Oct 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes looks weird, but they refer to Name struct, which means String in schemars. That is why they use defs/string instead of string.

/// Additional names to ignore when considering `flake8-self` violations,
/// in addition to those included in [`ignore-names`](#lint_flake8-self_ignore-names).
#[option(
default = r#"[]"#,
value_type = "list[str]",
example = r#"extend-ignore-names = ["_base_manager", "_default_manager", "_meta"]"#
)]
pub extend_ignore_names: Option<Vec<Name>>,

/// A list of names to ignore when considering `flake8-self` violations.
#[option(
default = r#"["_make", "_asdict", "_replace", "_fields", "_field_defaults", "_name_", "_value_"]"#,
value_type = "list[str]",
example = r#"
ignore-names = ["_new"]
"#
)]
pub ignore_names: Option<Vec<Name>>,

#[cfg(feature = "schemars")]
impl schemars::JsonSchema for Name {
fn is_referenceable() -> bool {
String::is_referenceable()
}
fn schema_name() -> String {
String::schema_name()
}
fn schema_id() -> std::borrow::Cow<'static, str> {
String::schema_id()
}
fn json_schema(generator: &mut schemars::r#gen::SchemaGenerator) -> schemars::schema::Schema {
String::json_schema(generator)
}
fn _schemars_private_non_optional_json_schema(
generator: &mut schemars::r#gen::SchemaGenerator,
) -> schemars::schema::Schema {
String::_schemars_private_non_optional_json_schema(generator)
}

ruff.schema.json Outdated
Comment on lines 1168 to 1178
@@ -1185,7 +1175,7 @@
"null"
],
"items": {
"type": "string"
"$ref": "#/$defs/string"
Copy link
Contributor Author

@TaKO8Ki TaKO8Ki Oct 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as below.

#20942 (comment)

Comment on lines +1132 to +1137
"string": {
"type": "string"
Copy link
Contributor Author

@TaKO8Ki TaKO8Ki Oct 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is related to #20942 (comment)

ruff.schema.json Outdated
}
]
},
"string": {
"type": "string"
Copy link
Contributor Author

@TaKO8Ki TaKO8Ki Oct 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is related to #20942 (comment).

ty.schema.json Outdated
"description": "The root of the project, used for finding first-party modules.\n\nIf left unspecified, ty will try to detect common project layouts and initialize `src.root` accordingly:\n\n* if a `./src` directory exists, include `.` and `./src` in the first party search path (src layout or flat)\n* if a `./<project-name>/<project-name>` directory exists, include `.` and `./<project-name>` in the first party search path\n* otherwise, default to `.` (flat layout)\n\nBesides, if a `./tests` directory exists and is not a package (i.e. it does not contain an `__init__.py` file),\nit will also be included in the first party search path.",
"anyOf": [
{
"$ref": "#/$defs/RelativePathBuf"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With Schemars 1.0 (defaulting to JSON Schema 2020-12), named newtypes are emitted as reusable $refs in #/$defs by default, even when #[serde(transparent)] makes their schema equal to the inner field. This preserves type identity and deduplicates shared shapes.

#[serde(transparent)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct RelativePathBuf(RangedValue<SystemPathBuf>);

Comment on lines +1104 to +1111
"SystemPathBuf": {
"description": "An owned, mutable path on [`System`](`super::System`) (akin to [`String`]).\n\nThe path is guaranteed to be valid UTF-8.",
"type": "string"
},
Copy link
Contributor Author

@TaKO8Ki TaKO8Ki Oct 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is related to #20942 (comment).

@TaKO8Ki TaKO8Ki marked this pull request as ready for review October 17, 2025 18:27
@AlexWaygood AlexWaygood removed their request for review October 17, 2025 18:36
@TaKO8Ki TaKO8Ki force-pushed the schemars-v1-upgrade branch 2 times, most recently from 4cabd1e to 7c5b4d4 Compare October 18, 2025 16:20
ty.schema.json Outdated
{
"type": "string",
"pattern": "^\\d+\\.\\d+$"
"pattern": "^\\\\d+\\\\.\\\\d+$"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The escaping here looks incorrect to me


fn schema_name() -> String {
String::schema_name()
fn schema_name() -> std::borrow::Cow<'static, str> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could replace this with

#[cfg_attr(
    feature = "serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(transparent)
)]
#[cfg_attr(feature = "cache", derive(ruff_macros::CacheKey))]
#[cfg_attr(feature = "salsa", derive(salsa::Update))]
#[cfg_attr(feature = "get-size", derive(get_size2::GetSize))]
#[cfg_attr(
    feature = "schemars",
    derive(schemars::JsonSchema),
    schemars(with = "String")
)]

(serde(transparent) and schemars(with="String"))

Comment on lines 200 to 206
let string_with_pattern = schemars::json_schema!({
"type": "string",
"pattern": r"^\\d+\\.\\d+$",
});

let mut any_of: Vec<Value> = Vec::new();
any_of.push(string_with_pattern.into());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit

Suggested change
let string_with_pattern = schemars::json_schema!({
"type": "string",
"pattern": r"^\\d+\\.\\d+$",
});
let mut any_of: Vec<Value> = Vec::new();
any_of.push(string_with_pattern.into());
let mut any_of: Vec<Value> = vec![
schemars::json_schema!({
"type": "string",
"pattern": r"^\\d+\\.\\d+$",
})
];

Comment on lines 3941 to 3942
per_file_ignores: Option<BTreeMap<String, Vec<RuleSelector>>>,
extend_per_file_ignores: Option<BTreeMap<String, Vec<RuleSelector>>>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for changing those fields to a BTreeMap?

}
}

#[cfg(feature = "schemars")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I'd prefer to keep this in its own mod (and in the same position, it makes attributing changes with git easier)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I want to do is implementing JsonSchema for Rules instead of schemars(with = "schema::Rules"), so I will keep the mod and implement it inside the module.

Comment on lines -80 to -81
// Promote well-known values for better auto-completion.
// Using `const` over `enumValues` as recommended [here](https://github.com/SchemaStore/schemastore/blob/master/CONTRIBUTING.md#documenting-enums).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we preserve this comment

ruff.schema.json Outdated
@@ -1,15 +1,15 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "https://json-schema.org/draft/2020-12/schema",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SchemaStore's recommendation is to still use draft-07 because many editors lack support for newer drafts, see

We recommend using the draft-07 JSON schema version. Later versions of JSON Schema are not yet recommended for use in SchemaStore until IDE and language support improves for those versions.

https://github.com/SchemaStore/schemastore/blob/master/CONTRIBUTING.md#best-practices

That's why I think we should not change the schema version as part of this PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. It's also possible to continue to use draft-07. I will replace the schema version.

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. This looks great overall. My only concern is the bump of the schema version. SchemaStore recommends draft-07 for best editor compatibility. I suggest we defer the schema version bump as it requires extensive testing across editors.

@MichaReiser MichaReiser added the internal An internal refactor or improvement label Oct 19, 2025
@TaKO8Ki TaKO8Ki force-pushed the schemars-v1-upgrade branch from 065b5e4 to 7711a64 Compare October 20, 2025 06:47
@TaKO8Ki
Copy link
Contributor Author

TaKO8Ki commented Oct 20, 2025

@MichaReiser Thank you for the review. I have addressed your comments!

@MichaReiser
Copy link
Member

This is great. Thank you so much!

@MichaReiser MichaReiser merged commit 48b5012 into astral-sh:main Oct 20, 2025
39 of 42 checks passed
@TaKO8Ki TaKO8Ki deleted the schemars-v1-upgrade branch October 20, 2025 07:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

internal An internal refactor or improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Update schemars to v1

2 participants