Add target_version to formatter options (#9220)

This commit is contained in:
Micha Reiser
2023-12-21 13:05:58 +09:00
committed by GitHub
parent ef4bd8d5ff
commit 8cb7950102
21 changed files with 103 additions and 10 deletions

View File

@@ -17,7 +17,7 @@ use crate::comments::{
pub use crate::context::PyFormatContext;
pub use crate::options::{
DocstringCode, DocstringCodeLineWidth, MagicTrailingComma, PreviewMode, PyFormatOptions,
QuoteStyle,
PythonVersion, QuoteStyle,
};
pub use crate::shared_traits::{AsFormat, FormattedIter, FormattedIterExt, IntoFormat};
use crate::verbatim::suppressed_node;

View File

@@ -17,6 +17,10 @@ pub struct PyFormatOptions {
/// Whether we're in a `.py` file or `.pyi` file, which have different rules.
source_type: PySourceType,
/// The (minimum) Python version used to run the formatted code. This is used
/// to determine the supported Python syntax.
target_version: PythonVersion,
/// Specifies the indent style:
/// * Either a tab
/// * or a specific amount of spaces
@@ -74,6 +78,7 @@ impl Default for PyFormatOptions {
fn default() -> Self {
Self {
source_type: PySourceType::default(),
target_version: PythonVersion::default(),
indent_style: default_indent_style(),
line_width: default_line_width(),
indent_width: default_indent_width(),
@@ -101,31 +106,35 @@ impl PyFormatOptions {
}
}
pub fn magic_trailing_comma(&self) -> MagicTrailingComma {
pub const fn target_version(&self) -> PythonVersion {
self.target_version
}
pub const fn magic_trailing_comma(&self) -> MagicTrailingComma {
self.magic_trailing_comma
}
pub fn quote_style(&self) -> QuoteStyle {
pub const fn quote_style(&self) -> QuoteStyle {
self.quote_style
}
pub fn source_type(&self) -> PySourceType {
pub const fn source_type(&self) -> PySourceType {
self.source_type
}
pub fn source_map_generation(&self) -> SourceMapGeneration {
pub const fn source_map_generation(&self) -> SourceMapGeneration {
self.source_map_generation
}
pub fn line_ending(&self) -> LineEnding {
pub const fn line_ending(&self) -> LineEnding {
self.line_ending
}
pub fn docstring_code(&self) -> DocstringCode {
pub const fn docstring_code(&self) -> DocstringCode {
self.docstring_code
}
pub fn docstring_code_line_width(&self) -> DocstringCodeLineWidth {
pub const fn docstring_code_line_width(&self) -> DocstringCodeLineWidth {
self.docstring_code_line_width
}
@@ -133,6 +142,12 @@ impl PyFormatOptions {
self.preview
}
#[must_use]
pub fn with_target_version(mut self, target_version: PythonVersion) -> Self {
self.target_version = target_version;
self
}
#[must_use]
pub fn with_indent_width(mut self, indent_width: IndentWidth) -> Self {
self.indent_width = indent_width;
@@ -349,3 +364,22 @@ where
)),
}
}
#[derive(CacheKey, Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq, Default)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "lowercase")
)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub enum PythonVersion {
Py37,
// Make sure to also change the default for `ruff_linter::settings::types::PythonVersion`
// when changing the default here.
#[default]
Py38,
Py39,
Py310,
Py311,
Py312,
}

View File

@@ -355,7 +355,8 @@ line-ending = {line_ending:?}
magic-trailing-comma = {magic_trailing_comma:?}
docstring-code = {docstring_code:?}
docstring-code-line-width = {docstring_code_line_width:?}
preview = {preview:?}"#,
preview = {preview:?}
target_version = {target_version:?}"#,
indent_style = self.0.indent_style(),
indent_width = self.0.indent_width().value(),
line_width = self.0.line_width().value(),
@@ -364,7 +365,8 @@ preview = {preview:?}"#,
magic_trailing_comma = self.0.magic_trailing_comma(),
docstring_code = self.0.docstring_code(),
docstring_code_line_width = self.0.docstring_code_line_width(),
preview = self.0.preview()
preview = self.0.preview(),
target_version = self.0.target_version()
)
}
}

View File

@@ -56,6 +56,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Enabled
target_version = Py38
```
```python

View File

@@ -175,6 +175,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -349,6 +350,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -523,6 +525,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -697,6 +700,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -871,6 +875,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python

View File

@@ -1368,6 +1368,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -2738,6 +2739,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -4108,6 +4110,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -5478,6 +5481,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -6848,6 +6852,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -8215,6 +8220,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -9582,6 +9588,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -10958,6 +10965,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -12325,6 +12333,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = 60
preview = Disabled
target_version = Py38
```
```python
@@ -13701,6 +13710,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python

View File

@@ -27,6 +27,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python

View File

@@ -239,6 +239,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -545,6 +546,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -841,6 +843,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -1147,6 +1150,7 @@ magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python

View File

@@ -138,6 +138,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -290,6 +291,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python

View File

@@ -153,6 +153,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -329,6 +330,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python

View File

@@ -37,6 +37,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -73,6 +74,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python

View File

@@ -18,6 +18,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -35,6 +36,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -52,6 +54,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python

View File

@@ -33,6 +33,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -66,6 +67,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -99,6 +101,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python

View File

@@ -84,6 +84,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -166,6 +167,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Enabled
target_version = Py38
```
```python

View File

@@ -68,6 +68,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -139,6 +140,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -210,6 +212,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python

View File

@@ -51,6 +51,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -107,6 +108,7 @@ magic-trailing-comma = Ignore
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python

View File

@@ -253,6 +253,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Enabled
target_version = Py38
```
```python

View File

@@ -26,6 +26,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -51,6 +52,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python
@@ -79,6 +81,7 @@ magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = "dynamic"
preview = Disabled
target_version = Py38
```
```python