Use ast::PythonVersion internally in the formatter and linter (#16170)

## Summary

This PR updates the formatter and linter to use the `PythonVersion`
struct from the `ruff_python_ast` crate internally. While this doesn't
remove the need for the `linter::PythonVersion` enum, it does remove the
`formatter::PythonVersion` enum and limits the use in the linter to
deserializing from CLI arguments and config files and moves most of the
remaining methods to the `ast::PythonVersion` struct.

## Test Plan

Existing tests, with some inputs and outputs updated to reflect the new
(de)serialization format. I think these are test-specific and shouldn't
affect any external (de)serialization.

---------

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
Brent Westbrook 2025-02-18 12:03:13 -05:00 committed by GitHub
parent 0868e73d2c
commit a9efdea113
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
153 changed files with 456 additions and 539 deletions

View file

@ -26,8 +26,9 @@ fn black_compatibility() {
let options: PyFormatOptions = if let Ok(options_file) = fs::File::open(&options_path) {
let reader = BufReader::new(options_file);
serde_json::from_reader(reader)
.unwrap_or_else(|_| panic!("Option file {options_path:?} to be a valid Json file"))
serde_json::from_reader(reader).unwrap_or_else(|_| {
panic!("Expected option file {options_path:?} to be a valid Json file")
})
} else {
PyFormatOptions::from_extension(input_path)
};
@ -180,10 +181,12 @@ fn format() {
let mut snapshot = format!("## Input\n{}", CodeFrame::new("python", &content));
let options_path = input_path.with_extension("options.json");
if let Ok(options_file) = fs::File::open(options_path) {
if let Ok(options_file) = fs::File::open(&options_path) {
let reader = BufReader::new(options_file);
let options: Vec<PyFormatOptions> =
serde_json::from_reader(reader).expect("Options to be a valid Json file");
serde_json::from_reader(reader).unwrap_or_else(|_| {
panic!("Expected option file {options_path:?} to be a valid Json file")
});
writeln!(snapshot, "## Outputs").unwrap();
@ -473,7 +476,7 @@ magic-trailing-comma = {magic_trailing_comma:?}
docstring-code = {docstring_code:?}
docstring-code-line-width = {docstring_code_line_width:?}
preview = {preview:?}
target_version = {target_version:?}
target_version = {target_version}
source_type = {source_type:?}"#,
indent_style = self.0.indent_style(),
indent_width = self.0.indent_width().value(),

View file

@ -1,7 +1,6 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/blank_line_before_class_docstring.py
snapshot_kind: text
---
## Input
```python
@ -57,7 +56,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Enabled
target_version = Py39
target_version = 3.9
source_type = Python
```

View file

@ -1,7 +1,6 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/docstring.py
snapshot_kind: text
---
## Input
```python
@ -176,7 +175,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -352,7 +351,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -528,7 +527,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -704,7 +703,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -880,7 +879,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```

View file

@ -1,7 +1,6 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/docstring_code_examples.py
snapshot_kind: text
---
## Input
```python
@ -1369,7 +1368,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -2741,7 +2740,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -4113,7 +4112,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -5485,7 +5484,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -6857,7 +6856,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -8222,7 +8221,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -9587,7 +9586,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -10961,7 +10960,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -12326,7 +12325,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = 60
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -13700,7 +13699,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```

View file

@ -1,7 +1,6 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/docstring_code_examples_crlf.py
snapshot_kind: text
---
## Input
```python
@ -28,7 +27,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```

View file

@ -1,7 +1,6 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/docstring_code_examples_dynamic_line_width.py
snapshot_kind: text
---
## Input
```python
@ -309,7 +308,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -880,7 +879,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -1426,7 +1425,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -1997,7 +1996,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```

View file

@ -1,7 +1,6 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/docstring_tab_indentation.py
snapshot_kind: text
---
## Input
```python
@ -91,7 +90,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -185,7 +184,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```

View file

@ -1,7 +1,6 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/bytes.py
snapshot_kind: text
---
## Input
```python
@ -141,7 +140,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -297,7 +296,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```

View file

@ -751,7 +751,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Enabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -1551,7 +1551,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```

View file

@ -1,7 +1,6 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/fstring_preview.py
snapshot_kind: text
---
## Input
```python
@ -39,7 +38,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Enabled
target_version = Py39
target_version = 3.9
source_type = Python
```

View file

@ -40,7 +40,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -81,7 +81,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py312
target_version = 3.12
source_type = Python
```

View file

@ -1,7 +1,6 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/string.py
snapshot_kind: text
---
## Input
```python
@ -233,7 +232,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -483,7 +482,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```

View file

@ -1,7 +1,6 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/fmt_on_off/fmt_off_docstring.py
snapshot_kind: text
---
## Input
```python
@ -38,7 +37,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -76,7 +75,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```

View file

@ -1,7 +1,6 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/fmt_on_off/indent.py
snapshot_kind: text
---
## Input
```python
@ -19,7 +18,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -38,7 +37,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -57,7 +56,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```

View file

@ -1,7 +1,6 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/fmt_on_off/mixed_space_and_tab.py
snapshot_kind: text
---
## Input
```python
@ -34,7 +33,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -69,7 +68,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -104,7 +103,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```

View file

@ -1,7 +1,6 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/notebook_docstring.py
snapshot_kind: text
---
## Input
```python
@ -25,7 +24,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Ipynb
```
@ -49,7 +48,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```

View file

@ -1,7 +1,6 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/preview.py
snapshot_kind: text
---
## Input
```python
@ -85,7 +84,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -168,7 +167,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Enabled
target_version = Py39
target_version = 3.9
source_type = Python
```

View file

@ -1,7 +1,6 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/quote_style.py
snapshot_kind: text
---
## Input
```python
@ -69,7 +68,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -143,7 +142,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -217,7 +216,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```

View file

@ -1,7 +1,6 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/range_formatting/docstring_code_examples.py
snapshot_kind: text
---
## Input
```python
@ -122,7 +121,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -274,7 +273,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = 88
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```

View file

@ -1,7 +1,6 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/range_formatting/indent.py
snapshot_kind: text
---
## Input
```python
@ -82,7 +81,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -161,7 +160,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -240,7 +239,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```

View file

@ -34,7 +34,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Stub
```

View file

@ -1,7 +1,6 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/skip_magic_trailing_comma.py
snapshot_kind: text
---
## Input
```python
@ -52,7 +51,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -110,7 +109,7 @@ magic-trailing-comma = Ignore
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```

View file

@ -387,7 +387,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
target_version = 3.8
source_type = Python
```
@ -889,7 +889,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Enabled
target_version = Py39
target_version = 3.9
source_type = Python
```

View file

@ -1,7 +1,6 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/with_39.py
snapshot_kind: text
---
## Input
```python
@ -111,7 +110,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Enabled
target_version = Py39
target_version = 3.9
source_type = Python
```

View file

@ -1,7 +1,6 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/stub_files/blank_line_after_nested_stub_class.pyi
snapshot_kind: text
---
## Input
```python
@ -202,7 +201,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Enabled
target_version = Py39
target_version = 3.9
source_type = Stub
```

View file

@ -1,7 +1,6 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/stub_files/blank_line_after_nested_stub_class_eof.pyi
snapshot_kind: text
---
## Input
```python
@ -36,7 +35,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Enabled
target_version = Py39
target_version = 3.9
source_type = Stub
```

View file

@ -1,7 +1,6 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/tab_width.py
snapshot_kind: text
---
## Input
```python
@ -27,7 +26,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -54,7 +53,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```
@ -84,7 +83,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py39
target_version = 3.9
source_type = Python
```