Skip to content

Commit 8cb7950

Browse files
authored
Add target_version to formatter options (#9220)
1 parent ef4bd8d commit 8cb7950

21 files changed

+103
-10
lines changed

crates/ruff_linter/src/settings/types.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ use crate::rule_selector::RuleSelector;
4040
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
4141
pub enum PythonVersion {
4242
Py37,
43+
// Make sure to also change the default for `ruff_python_formatter::PythonVersion`
44+
// when changing the default here.
4345
#[default]
4446
Py38,
4547
Py39,

crates/ruff_python_formatter/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::comments::{
1717
pub use crate::context::PyFormatContext;
1818
pub use crate::options::{
1919
DocstringCode, DocstringCodeLineWidth, MagicTrailingComma, PreviewMode, PyFormatOptions,
20-
QuoteStyle,
20+
PythonVersion, QuoteStyle,
2121
};
2222
pub use crate::shared_traits::{AsFormat, FormattedIter, FormattedIterExt, IntoFormat};
2323
use crate::verbatim::suppressed_node;

crates/ruff_python_formatter/src/options.rs

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ pub struct PyFormatOptions {
1717
/// Whether we're in a `.py` file or `.pyi` file, which have different rules.
1818
source_type: PySourceType,
1919

20+
/// The (minimum) Python version used to run the formatted code. This is used
21+
/// to determine the supported Python syntax.
22+
target_version: PythonVersion,
23+
2024
/// Specifies the indent style:
2125
/// * Either a tab
2226
/// * or a specific amount of spaces
@@ -74,6 +78,7 @@ impl Default for PyFormatOptions {
7478
fn default() -> Self {
7579
Self {
7680
source_type: PySourceType::default(),
81+
target_version: PythonVersion::default(),
7782
indent_style: default_indent_style(),
7883
line_width: default_line_width(),
7984
indent_width: default_indent_width(),
@@ -101,38 +106,48 @@ impl PyFormatOptions {
101106
}
102107
}
103108

104-
pub fn magic_trailing_comma(&self) -> MagicTrailingComma {
109+
pub const fn target_version(&self) -> PythonVersion {
110+
self.target_version
111+
}
112+
113+
pub const fn magic_trailing_comma(&self) -> MagicTrailingComma {
105114
self.magic_trailing_comma
106115
}
107116

108-
pub fn quote_style(&self) -> QuoteStyle {
117+
pub const fn quote_style(&self) -> QuoteStyle {
109118
self.quote_style
110119
}
111120

112-
pub fn source_type(&self) -> PySourceType {
121+
pub const fn source_type(&self) -> PySourceType {
113122
self.source_type
114123
}
115124

116-
pub fn source_map_generation(&self) -> SourceMapGeneration {
125+
pub const fn source_map_generation(&self) -> SourceMapGeneration {
117126
self.source_map_generation
118127
}
119128

120-
pub fn line_ending(&self) -> LineEnding {
129+
pub const fn line_ending(&self) -> LineEnding {
121130
self.line_ending
122131
}
123132

124-
pub fn docstring_code(&self) -> DocstringCode {
133+
pub const fn docstring_code(&self) -> DocstringCode {
125134
self.docstring_code
126135
}
127136

128-
pub fn docstring_code_line_width(&self) -> DocstringCodeLineWidth {
137+
pub const fn docstring_code_line_width(&self) -> DocstringCodeLineWidth {
129138
self.docstring_code_line_width
130139
}
131140

132141
pub const fn preview(&self) -> PreviewMode {
133142
self.preview
134143
}
135144

145+
#[must_use]
146+
pub fn with_target_version(mut self, target_version: PythonVersion) -> Self {
147+
self.target_version = target_version;
148+
self
149+
}
150+
136151
#[must_use]
137152
pub fn with_indent_width(mut self, indent_width: IndentWidth) -> Self {
138153
self.indent_width = indent_width;
@@ -349,3 +364,22 @@ where
349364
)),
350365
}
351366
}
367+
368+
#[derive(CacheKey, Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq, Default)]
369+
#[cfg_attr(
370+
feature = "serde",
371+
derive(serde::Serialize, serde::Deserialize),
372+
serde(rename_all = "lowercase")
373+
)]
374+
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
375+
pub enum PythonVersion {
376+
Py37,
377+
// Make sure to also change the default for `ruff_linter::settings::types::PythonVersion`
378+
// when changing the default here.
379+
#[default]
380+
Py38,
381+
Py39,
382+
Py310,
383+
Py311,
384+
Py312,
385+
}

crates/ruff_python_formatter/tests/fixtures.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,8 @@ line-ending = {line_ending:?}
355355
magic-trailing-comma = {magic_trailing_comma:?}
356356
docstring-code = {docstring_code:?}
357357
docstring-code-line-width = {docstring_code_line_width:?}
358-
preview = {preview:?}"#,
358+
preview = {preview:?}
359+
target_version = {target_version:?}"#,
359360
indent_style = self.0.indent_style(),
360361
indent_width = self.0.indent_width().value(),
361362
line_width = self.0.line_width().value(),
@@ -364,7 +365,8 @@ preview = {preview:?}"#,
364365
magic_trailing_comma = self.0.magic_trailing_comma(),
365366
docstring_code = self.0.docstring_code(),
366367
docstring_code_line_width = self.0.docstring_code_line_width(),
367-
preview = self.0.preview()
368+
preview = self.0.preview(),
369+
target_version = self.0.target_version()
368370
)
369371
}
370372
}

crates/ruff_python_formatter/tests/snapshots/format@blank_line_before_class_docstring.py.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ magic-trailing-comma = Respect
5656
docstring-code = Disabled
5757
docstring-code-line-width = "dynamic"
5858
preview = Enabled
59+
target_version = Py38
5960
```
6061

6162
```python

crates/ruff_python_formatter/tests/snapshots/format@docstring.py.snap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ magic-trailing-comma = Respect
175175
docstring-code = Disabled
176176
docstring-code-line-width = "dynamic"
177177
preview = Disabled
178+
target_version = Py38
178179
```
179180

180181
```python
@@ -349,6 +350,7 @@ magic-trailing-comma = Respect
349350
docstring-code = Disabled
350351
docstring-code-line-width = "dynamic"
351352
preview = Disabled
353+
target_version = Py38
352354
```
353355

354356
```python
@@ -523,6 +525,7 @@ magic-trailing-comma = Respect
523525
docstring-code = Disabled
524526
docstring-code-line-width = "dynamic"
525527
preview = Disabled
528+
target_version = Py38
526529
```
527530

528531
```python
@@ -697,6 +700,7 @@ magic-trailing-comma = Respect
697700
docstring-code = Disabled
698701
docstring-code-line-width = "dynamic"
699702
preview = Disabled
703+
target_version = Py38
700704
```
701705

702706
```python
@@ -871,6 +875,7 @@ magic-trailing-comma = Respect
871875
docstring-code = Disabled
872876
docstring-code-line-width = "dynamic"
873877
preview = Disabled
878+
target_version = Py38
874879
```
875880

876881
```python

crates/ruff_python_formatter/tests/snapshots/format@docstring_code_examples.py.snap

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,7 @@ magic-trailing-comma = Respect
13681368
docstring-code = Disabled
13691369
docstring-code-line-width = "dynamic"
13701370
preview = Disabled
1371+
target_version = Py38
13711372
```
13721373
13731374
```python
@@ -2738,6 +2739,7 @@ magic-trailing-comma = Respect
27382739
docstring-code = Disabled
27392740
docstring-code-line-width = "dynamic"
27402741
preview = Disabled
2742+
target_version = Py38
27412743
```
27422744
27432745
```python
@@ -4108,6 +4110,7 @@ magic-trailing-comma = Respect
41084110
docstring-code = Disabled
41094111
docstring-code-line-width = "dynamic"
41104112
preview = Disabled
4113+
target_version = Py38
41114114
```
41124115
41134116
```python
@@ -5478,6 +5481,7 @@ magic-trailing-comma = Respect
54785481
docstring-code = Disabled
54795482
docstring-code-line-width = "dynamic"
54805483
preview = Disabled
5484+
target_version = Py38
54815485
```
54825486
54835487
```python
@@ -6848,6 +6852,7 @@ magic-trailing-comma = Respect
68486852
docstring-code = Enabled
68496853
docstring-code-line-width = "dynamic"
68506854
preview = Disabled
6855+
target_version = Py38
68516856
```
68526857
68536858
```python
@@ -8215,6 +8220,7 @@ magic-trailing-comma = Respect
82158220
docstring-code = Enabled
82168221
docstring-code-line-width = "dynamic"
82178222
preview = Disabled
8223+
target_version = Py38
82188224
```
82198225
82208226
```python
@@ -9582,6 +9588,7 @@ magic-trailing-comma = Respect
95829588
docstring-code = Enabled
95839589
docstring-code-line-width = "dynamic"
95849590
preview = Disabled
9591+
target_version = Py38
95859592
```
95869593
95879594
```python
@@ -10958,6 +10965,7 @@ magic-trailing-comma = Respect
1095810965
docstring-code = Enabled
1095910966
docstring-code-line-width = "dynamic"
1096010967
preview = Disabled
10968+
target_version = Py38
1096110969
```
1096210970
1096310971
```python
@@ -12325,6 +12333,7 @@ magic-trailing-comma = Respect
1232512333
docstring-code = Enabled
1232612334
docstring-code-line-width = 60
1232712335
preview = Disabled
12336+
target_version = Py38
1232812337
```
1232912338
1233012339
```python
@@ -13701,6 +13710,7 @@ magic-trailing-comma = Respect
1370113710
docstring-code = Enabled
1370213711
docstring-code-line-width = "dynamic"
1370313712
preview = Disabled
13713+
target_version = Py38
1370413714
```
1370513715
1370613716
```python

crates/ruff_python_formatter/tests/snapshots/format@docstring_code_examples_crlf.py.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ magic-trailing-comma = Respect
2727
docstring-code = Enabled
2828
docstring-code-line-width = "dynamic"
2929
preview = Disabled
30+
target_version = Py38
3031
```
3132

3233
```python

crates/ruff_python_formatter/tests/snapshots/format@docstring_code_examples_dynamic_line_width.py.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ magic-trailing-comma = Respect
239239
docstring-code = Enabled
240240
docstring-code-line-width = "dynamic"
241241
preview = Disabled
242+
target_version = Py38
242243
```
243244

244245
```python
@@ -545,6 +546,7 @@ magic-trailing-comma = Respect
545546
docstring-code = Enabled
546547
docstring-code-line-width = "dynamic"
547548
preview = Disabled
549+
target_version = Py38
548550
```
549551

550552
```python
@@ -841,6 +843,7 @@ magic-trailing-comma = Respect
841843
docstring-code = Enabled
842844
docstring-code-line-width = "dynamic"
843845
preview = Disabled
846+
target_version = Py38
844847
```
845848

846849
```python
@@ -1147,6 +1150,7 @@ magic-trailing-comma = Respect
11471150
docstring-code = Enabled
11481151
docstring-code-line-width = "dynamic"
11491152
preview = Disabled
1153+
target_version = Py38
11501154
```
11511155

11521156
```python

crates/ruff_python_formatter/tests/snapshots/format@expression__bytes.py.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ magic-trailing-comma = Respect
138138
docstring-code = Disabled
139139
docstring-code-line-width = "dynamic"
140140
preview = Disabled
141+
target_version = Py38
141142
```
142143

143144
```python
@@ -290,6 +291,7 @@ magic-trailing-comma = Respect
290291
docstring-code = Disabled
291292
docstring-code-line-width = "dynamic"
292293
preview = Disabled
294+
target_version = Py38
293295
```
294296

295297
```python

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy