Skip to content

Commit 86583c1

Browse files
committed
unify PyVersion enums
1 parent 3934d4b commit 86583c1

9 files changed

Lines changed: 13 additions & 96 deletions

File tree

crates/ruff_python_formatter/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::comments::{
1515
pub use crate::context::PyFormatContext;
1616
pub use crate::options::{
1717
DocstringCode, DocstringCodeLineWidth, MagicTrailingComma, PreviewMode, PyFormatOptions,
18-
PythonVersion, QuoteStyle,
18+
QuoteStyle,
1919
};
2020
use crate::range::is_logical_line;
2121
pub use crate::shared_traits::{AsFormat, FormattedIter, FormattedIterExt, IntoFormat};

crates/ruff_python_formatter/src/options.rs

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use ruff_formatter::printer::{LineEnding, PrinterOptions, SourceMapGeneration};
66
use ruff_formatter::{FormatOptions, IndentStyle, IndentWidth, LineWidth};
77
use ruff_macros::CacheKey;
88
use ruff_python_ast::PySourceType;
9+
use ruff_python_parser::python_version::PyVersion;
910

1011
/// Resolved options for formatting one individual file. The difference to `FormatterSettings`
1112
/// is that `FormatterSettings` stores the settings for multiple files (the entire project, a subdirectory, ..)
@@ -21,7 +22,7 @@ pub struct PyFormatOptions {
2122

2223
/// The (minimum) Python version used to run the formatted code. This is used
2324
/// to determine the supported Python syntax.
24-
target_version: PythonVersion,
25+
target_version: PyVersion,
2526

2627
/// Specifies the indent style:
2728
/// * Either a tab
@@ -80,7 +81,7 @@ impl Default for PyFormatOptions {
8081
fn default() -> Self {
8182
Self {
8283
source_type: PySourceType::default(),
83-
target_version: PythonVersion::default(),
84+
target_version: PyVersion::default(),
8485
indent_style: default_indent_style(),
8586
line_width: default_line_width(),
8687
indent_width: default_indent_width(),
@@ -108,7 +109,7 @@ impl PyFormatOptions {
108109
}
109110
}
110111

111-
pub const fn target_version(&self) -> PythonVersion {
112+
pub const fn target_version(&self) -> PyVersion {
112113
self.target_version
113114
}
114115

@@ -145,7 +146,7 @@ impl PyFormatOptions {
145146
}
146147

147148
#[must_use]
148-
pub fn with_target_version(mut self, target_version: PythonVersion) -> Self {
149+
pub fn with_target_version(mut self, target_version: PyVersion) -> Self {
149150
self.target_version = target_version;
150151
self
151152
}
@@ -468,52 +469,3 @@ where
468469
)),
469470
}
470471
}
471-
472-
#[derive(CacheKey, Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq, Default)]
473-
#[cfg_attr(
474-
feature = "serde",
475-
derive(serde::Serialize, serde::Deserialize),
476-
serde(rename_all = "lowercase")
477-
)]
478-
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
479-
pub enum PythonVersion {
480-
Py37,
481-
Py38,
482-
// Make sure to also change the default for `ruff_linter::settings::types::PythonVersion`
483-
// when changing the default here.
484-
#[default]
485-
Py39,
486-
Py310,
487-
Py311,
488-
Py312,
489-
Py313,
490-
}
491-
492-
impl PythonVersion {
493-
/// Return `true` if the current version supports [PEP 701].
494-
///
495-
/// [PEP 701]: https://peps.python.org/pep-0701/
496-
pub fn supports_pep_701(self) -> bool {
497-
self >= Self::Py312
498-
}
499-
500-
pub fn as_tuple(self) -> (u8, u8) {
501-
match self {
502-
Self::Py37 => (3, 7),
503-
Self::Py38 => (3, 8),
504-
Self::Py39 => (3, 9),
505-
Self::Py310 => (3, 10),
506-
Self::Py311 => (3, 11),
507-
Self::Py312 => (3, 12),
508-
Self::Py313 => (3, 13),
509-
}
510-
}
511-
512-
pub fn latest() -> Self {
513-
Self::Py313
514-
}
515-
516-
pub fn minimal_supported() -> Self {
517-
Self::Py37
518-
}
519-
}

crates/ruff_python_formatter/src/statement/stmt_with.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use ruff_formatter::{format_args, write, FormatContext, FormatError};
22
use ruff_python_ast::{StmtWith, WithItem};
3+
use ruff_python_parser::python_version::PyVersion;
34
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
45
use ruff_text_size::{Ranged, TextRange};
56

@@ -14,7 +15,6 @@ use crate::other::with_item::WithItemLayout;
1415
use crate::prelude::*;
1516
use crate::statement::clause::{clause_body, clause_header, ClauseHeader};
1617
use crate::statement::suite::SuiteKind;
17-
use crate::PythonVersion;
1818

1919
#[derive(Default)]
2020
pub struct FormatStmtWith;
@@ -302,7 +302,7 @@ impl<'a> WithItemsLayout<'a> {
302302
}
303303
}
304304

305-
let can_parenthesize = context.options().target_version() >= PythonVersion::Py39
305+
let can_parenthesize = context.options().target_version() >= PyVersion::Py39
306306
|| are_with_items_parenthesized(with, context)?;
307307

308308
// If the target version doesn't support parenthesized context managers and they aren't

crates/ruff_python_formatter/src/string/implicit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ impl<'a> FormatImplicitConcatenatedStringFlat<'a> {
164164
}
165165

166166
if let StringLikePart::FString(fstring) = part {
167-
if context.options().target_version().supports_pep_701() {
167+
if context.options().target_version().supports_pep701() {
168168
if is_fstring_with_quoted_format_spec_and_debug(fstring, context) {
169169
if preserve_quotes_requirement
170170
.is_some_and(|quote| quote != part.flags().quote_style())

crates/ruff_python_formatter/src/string/normalize.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl<'a, 'src> StringNormalizer<'a, 'src> {
4545
let preferred_quote_style = self
4646
.preferred_quote_style
4747
.unwrap_or(self.context.options().quote_style());
48-
let supports_pep_701 = self.context.options().target_version().supports_pep_701();
48+
let supports_pep_701 = self.context.options().target_version().supports_pep701();
4949

5050
// For f-strings prefer alternating the quotes unless The outer string is triple quoted and the inner isn't.
5151
if let FStringState::InsideExpressionElement(parent_context) = self.context.f_string_state()

crates/ruff_python_parser/src/python_version.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ mod pyversion {
2121
pub enum PyVersion {
2222
Py37,
2323
Py38,
24-
// Make sure to also change the default for `ruff_python_formatter::PyVersion`
25-
// when changing the default here.
2624
#[default]
2725
Py39,
2826
Py310,

crates/ruff_workspace/src/configuration.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,7 @@ impl Configuration {
178178
exclude: FilePatternSet::try_from_iter(format.exclude.unwrap_or_default())?,
179179
extension: self.extension.clone().unwrap_or_default(),
180180
preview: format_preview,
181-
target_version: match target_version {
182-
PyVersion::Py37 => ruff_python_formatter::PythonVersion::Py37,
183-
PyVersion::Py38 => ruff_python_formatter::PythonVersion::Py38,
184-
PyVersion::Py39 => ruff_python_formatter::PythonVersion::Py39,
185-
PyVersion::Py310 => ruff_python_formatter::PythonVersion::Py310,
186-
PyVersion::Py311 => ruff_python_formatter::PythonVersion::Py311,
187-
PyVersion::Py312 => ruff_python_formatter::PythonVersion::Py312,
188-
PyVersion::Py313 => ruff_python_formatter::PythonVersion::Py313,
189-
},
181+
target_version,
190182
line_width: self
191183
.line_length
192184
.map_or(format_defaults.line_width, |length| {

crates/ruff_workspace/src/options.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3848,8 +3848,6 @@ mod tests {
38483848
use crate::options::Flake8SelfOptions;
38493849
use ruff_linter::rules::flake8_self;
38503850
use ruff_python_ast::name::Name;
3851-
use ruff_python_formatter::PythonVersion as FormatterPythonVersion;
3852-
use ruff_python_parser::python_version::PyVersion as LinterPythonVersion;
38533851

38543852
#[test]
38553853
fn flake8_self_options() {
@@ -3897,28 +3895,4 @@ mod tests {
38973895
vec![Name::new_static("_foo"), Name::new_static("_bar")]
38983896
);
38993897
}
3900-
3901-
#[test]
3902-
fn formatter_and_linter_target_version_have_same_default() {
3903-
assert_eq!(
3904-
FormatterPythonVersion::default().as_tuple(),
3905-
LinterPythonVersion::default().as_tuple()
3906-
);
3907-
}
3908-
3909-
#[test]
3910-
fn formatter_and_linter_target_version_have_same_latest() {
3911-
assert_eq!(
3912-
FormatterPythonVersion::latest().as_tuple(),
3913-
LinterPythonVersion::latest().as_tuple()
3914-
);
3915-
}
3916-
3917-
#[test]
3918-
fn formatter_and_linter_target_version_have_same_minimal_supported() {
3919-
assert_eq!(
3920-
FormatterPythonVersion::minimal_supported().as_tuple(),
3921-
LinterPythonVersion::minimal_supported().as_tuple()
3922-
);
3923-
}
39243898
}

crates/ruff_workspace/src/settings.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use ruff_python_formatter::{
1313
DocstringCode, DocstringCodeLineWidth, MagicTrailingComma, PreviewMode, PyFormatOptions,
1414
QuoteStyle,
1515
};
16+
use ruff_python_parser::python_version::PyVersion;
1617
use ruff_source_file::find_newline;
1718
use std::fmt;
1819
use std::path::{Path, PathBuf};
@@ -164,7 +165,7 @@ pub struct FormatterSettings {
164165
pub exclude: FilePatternSet,
165166
pub extension: ExtensionMapping,
166167
pub preview: PreviewMode,
167-
pub target_version: ruff_python_formatter::PythonVersion,
168+
pub target_version: PyVersion,
168169

169170
pub line_width: LineWidth,
170171

0 commit comments

Comments
 (0)