@@ -17,6 +17,10 @@ pub struct PyFormatOptions {
17
17
/// Whether we're in a `.py` file or `.pyi` file, which have different rules.
18
18
source_type : PySourceType ,
19
19
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
+
20
24
/// Specifies the indent style:
21
25
/// * Either a tab
22
26
/// * or a specific amount of spaces
@@ -74,6 +78,7 @@ impl Default for PyFormatOptions {
74
78
fn default ( ) -> Self {
75
79
Self {
76
80
source_type : PySourceType :: default ( ) ,
81
+ target_version : PythonVersion :: default ( ) ,
77
82
indent_style : default_indent_style ( ) ,
78
83
line_width : default_line_width ( ) ,
79
84
indent_width : default_indent_width ( ) ,
@@ -101,38 +106,48 @@ impl PyFormatOptions {
101
106
}
102
107
}
103
108
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 {
105
114
self . magic_trailing_comma
106
115
}
107
116
108
- pub fn quote_style ( & self ) -> QuoteStyle {
117
+ pub const fn quote_style ( & self ) -> QuoteStyle {
109
118
self . quote_style
110
119
}
111
120
112
- pub fn source_type ( & self ) -> PySourceType {
121
+ pub const fn source_type ( & self ) -> PySourceType {
113
122
self . source_type
114
123
}
115
124
116
- pub fn source_map_generation ( & self ) -> SourceMapGeneration {
125
+ pub const fn source_map_generation ( & self ) -> SourceMapGeneration {
117
126
self . source_map_generation
118
127
}
119
128
120
- pub fn line_ending ( & self ) -> LineEnding {
129
+ pub const fn line_ending ( & self ) -> LineEnding {
121
130
self . line_ending
122
131
}
123
132
124
- pub fn docstring_code ( & self ) -> DocstringCode {
133
+ pub const fn docstring_code ( & self ) -> DocstringCode {
125
134
self . docstring_code
126
135
}
127
136
128
- pub fn docstring_code_line_width ( & self ) -> DocstringCodeLineWidth {
137
+ pub const fn docstring_code_line_width ( & self ) -> DocstringCodeLineWidth {
129
138
self . docstring_code_line_width
130
139
}
131
140
132
141
pub const fn preview ( & self ) -> PreviewMode {
133
142
self . preview
134
143
}
135
144
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
+
136
151
#[ must_use]
137
152
pub fn with_indent_width ( mut self , indent_width : IndentWidth ) -> Self {
138
153
self . indent_width = indent_width;
@@ -349,3 +364,22 @@ where
349
364
) ) ,
350
365
}
351
366
}
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
+ }
0 commit comments