Skip to content

Commit 8f362bf

Browse files
committed
Integrate vendored Unicode names into Ruff
Use the vendored runtime and generator instead of the registry dependency, providing Unicode 17 names with Python-compatible matching. Update workspace dependencies and tool configuration, add parser regressions, and document how to update Unicode data when supporting a new Python version.
1 parent 88a50d6 commit 8f362bf

11 files changed

Lines changed: 595 additions & 38 deletions

File tree

‎.cargo/deny.toml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ allow-build-scripts = [
5959
"tikv-jemalloc-sys",
6060
"typeid",
6161
"typenum",
62-
"unicode_names2",
6362
"wasm-bindgen",
6463
"wasm-bindgen-shared",
6564
"winapi",

‎CONTRIBUTING.md‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,70 @@ See the [ruff-ecosystem package](https://github.com/astral-sh/ruff/tree/main/pyt
584584
1. Bump the Rust version in Ruff's conda forge recipe. See [this PR](https://github.com/conda-forge/ruff-feedstock/pull/266) for an example.
585585
1. Enjoy the new Rust version!
586586

587+
## Updating the Python version
588+
589+
1. Update `PythonVersion::latest` in `crates/ruff_python_ast/src/python_version.rs` to the latest
590+
version.
591+
592+
1. Update `PythonVersion::default` in the same file, assuming that version has also reached
593+
end-of-life. You can check this schedule
594+
[here](https://devguide.python.org/versions/#supported-versions).
595+
596+
1. Update the `Options::target_version` metadata and docs in `crates/ruff_workspace/src/options.rs`.
597+
598+
1. Remove any preview warnings about the new version, typically in
599+
`crates/ruff_linter/src/linter.rs`
600+
601+
1. Run the tests and update snapshots
602+
603+
Note that if the tests were intentionally exercising behavior on a particular version, you may need
604+
to pin the version in the tests or add new tests to maintain coverage. Otherwise, prefer updating
605+
the snapshots in place.
606+
607+
1. Also consider updating the values for ty in the following locations:
608+
609+
- `PythonVersion::latest_ty` in `crates/ruff_python_ast/src/python_version.rs`
610+
- The default in the `EnvironmentOptions::python_version` metadata in
611+
`crates/ty_project/src/metadata/options.rs`
612+
613+
1. Regenerate the affected schemas with `cargo dev generate-all`
614+
615+
See [#28792](https://github.com/astral-sh/ruff/pull/28792) and
616+
[#20725](https://github.com/astral-sh/ruff/pull/20725) for previous examples.
617+
618+
### Updating the supported Unicode version
619+
620+
When adding support for a new Python version, update Ruff's Unicode data to match
621+
the Unicode standard used by that Python version. Check the target interpreter's
622+
`unicodedata.unidata_version` to find the required version.
623+
624+
Check that the `unicode-ident` and `unicode-normalization` dependencies also
625+
support that Unicode version; the parser uses them to recognize and normalize
626+
identifiers.
627+
628+
Unicode names and aliases for `\N{...}` escapes are vendored in
629+
[`ruff_unicode_names2`](https://github.com/astral-sh/ruff/blob/main/crates/ruff_unicode_names2/README.md). To update them:
630+
631+
1. Download `UnicodeData.txt` and `NameAliases.txt` from
632+
`https://www.unicode.org/Public/<version>/ucd/` and replace both files in
633+
`crates/ruff_unicode_names2/data/`.
634+
635+
1. Update the Unicode version in `crates/ruff_unicode_names2/src/lib.rs` and its
636+
README, and the data source URLs in `crates/ruff_unicode_names2/build.rs`.
637+
638+
1. Add a parser regression test for a name introduced in the new Unicode version.
639+
Run the parser tests and the vendored library's exhaustive tests:
640+
641+
```shell
642+
cargo test -p ruff_python_parser
643+
cargo +nightly test -p ruff_unicode_names2 --lib
644+
```
645+
646+
The build script regenerates the lookup tables from these files automatically;
647+
there are no generated tables to check in. Preserve the vendored crate's strict,
648+
case-insensitive matching when taking upstream changes: Python requires spaces,
649+
hyphens, and underscores to match the registered name or alias.
650+
587651
## Benchmarking and Profiling
588652
589653
We have several ways of benchmarking and profiling Ruff:

‎Cargo.lock‎

Lines changed: 2 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,6 @@ typed-arena = { version = "2.0.2", default-features = false, features = ["std"]
247247
unicode-ident = { version = "1.0.12", default-features = false }
248248
unicode-normalization = { version = "0.1.23", default-features = false, features = ["std"] }
249249
unicode-width = { version = "0.2.0", default-features = false }
250-
unicode_names2 = { version = "1.2.2", default-features = false }
251250
url = { version = "2.5.0", default-features = false, features = ["std"] }
252251
uuid = { version = "1.6.1", default-features = false, features = ["std"] }
253252
walkdir = { version = "2.3.2", default-features = false }

‎crates/ruff_linter/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ ruff_python_trivia = { workspace = true }
3131
ruff_ranged_value = { workspace = true }
3232
ruff_source_file = { workspace = true, features = ["serde"] }
3333
ruff_text_size = { workspace = true }
34+
ruff_unicode_names2 = { workspace = true }
3435

3536
aho-corasick = { workspace = true }
3637
anyhow = { workspace = true }
@@ -66,7 +67,6 @@ toml = { workspace = true }
6667
typed-arena = { workspace = true }
6768
unicode-normalization = { workspace = true }
6869
unicode-width = { workspace = true }
69-
unicode_names2 = { workspace = true }
7070
url = { workspace = true }
7171

7272
[dev-dependencies]

‎crates/ruff_python_parser/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ license = { workspace = true }
1616
ruff_python_ast = { workspace = true, features = ["get-size"] }
1717
ruff_python_trivia = { workspace = true }
1818
ruff_text_size = { workspace = true, features = ["get-size"] }
19+
ruff_unicode_names2 = { workspace = true }
1920

2021
bitflags = { workspace = true }
2122
bstr = { workspace = true }
@@ -29,7 +30,6 @@ stacker = { workspace = true }
2930
thin-vec = { workspace = true }
3031
unicode-ident = { workspace = true }
3132
unicode-normalization = { workspace = true }
32-
unicode_names2 = { workspace = true }
3333

3434
[dev-dependencies]
3535
ruff_annotate_snippets = { workspace = true }
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Named escapes require the registered spaces and hyphens, ignoring ASCII case.
2+
'\N{LOWLINE}'
3+
'\N{LOW_LINE}'
4+
'\N{FULL S-T-O-P}'
5+
'\N{ FULL STOP}'
6+
'\N{new_line}'
7+
'\N{HANGULSYLLABLEGA}'
8+
'\N{CJKUNIFIEDIDEOGRAPH4E00}'
9+
'\N{-}'

‎crates/ruff_python_parser/resources/valid/expressions/string.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@
1515
b'hello world'
1616
b'bytes' b'concatenated'
1717

18+
'\N{latin small letter a}'
19+
'\N{new line}' '\N{byte order mark}'
20+
'\N{TROMBONE}'
21+
'\N{hangul syllable ga}'
22+
'\N{cjk unified ideograph-4e00}'
23+
'\N{HANGUL JUNGSEONG O-E}' '\N{HANGUL JUNGSEONG OE}'

0 commit comments

Comments
 (0)