Add Python 3.14 to configuration options (#17647)

A small PR that just updates the various settings/configurations to
allow Python 3.14. At the moment selecting that target version will
have no impact compared to Python 3.13 - except that a warning
is emitted if the user does so with `preview` disabled.
This commit is contained in:
Dylan 2025-04-28 16:29:00 -05:00 committed by GitHub
parent 504fa20057
commit ae7691b026
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 32 additions and 5 deletions

View file

@ -30,14 +30,14 @@ use crate::fix::{fix_file, FixResult};
use crate::message::Message;
use crate::noqa::add_noqa;
use crate::package::PackageRoot;
use crate::preview::is_unsupported_syntax_enabled;
use crate::preview::{is_py314_support_enabled, is_unsupported_syntax_enabled};
use crate::registry::{AsRule, Rule, RuleSet};
#[cfg(any(feature = "test-rules", test))]
use crate::rules::ruff::rules::test_rules::{self, TestRule, TEST_RULES};
use crate::settings::types::UnsafeFixes;
use crate::settings::{flags, LinterSettings};
use crate::source_kind::SourceKind;
use crate::{directives, fs, Locator};
use crate::{directives, fs, warn_user_once, Locator};
pub struct LinterResult {
/// A collection of diagnostic messages generated by the linter.
@ -450,6 +450,11 @@ pub fn lint_only(
source: ParseSource,
) -> LinterResult {
let target_version = settings.resolve_target_version(path);
if matches!(target_version, PythonVersion::PY314) && !is_py314_support_enabled(settings) {
warn_user_once!("Support for Python 3.14 is under development and may be unstable. Enable `preview` to remove this warning.");
}
let parsed = source.into_parsed(source_kind, source_type, target_version);
// Map row and column locations to byte slices (lazily).
@ -559,6 +564,10 @@ pub fn lint_fix<'a>(
let target_version = settings.resolve_target_version(path);
if matches!(target_version, PythonVersion::PY314) && !is_py314_support_enabled(settings) {
warn_user_once!("Support for Python 3.14 is under development and may be unstable. Enable `preview` to remove this warning.");
}
// Continuously fix until the source code stabilizes.
loop {
// Parse once.

View file

@ -18,6 +18,10 @@ pub(crate) const fn is_unsupported_syntax_enabled(settings: &LinterSettings) ->
settings.preview.is_enabled()
}
pub(crate) const fn is_py314_support_enabled(settings: &LinterSettings) -> bool {
settings.preview.is_enabled()
}
// Rule-specific behavior
// https://github.com/astral-sh/ruff/pull/17136

View file

@ -34,6 +34,7 @@ pub enum PythonVersion {
Py311,
Py312,
Py313,
Py314,
}
impl Default for PythonVersion {
@ -55,6 +56,7 @@ impl TryFrom<ast::PythonVersion> for PythonVersion {
ast::PythonVersion::PY311 => Ok(Self::Py311),
ast::PythonVersion::PY312 => Ok(Self::Py312),
ast::PythonVersion::PY313 => Ok(Self::Py313),
ast::PythonVersion::PY314 => Ok(Self::Py314),
_ => Err(format!("unrecognized python version {value}")),
}
}
@ -84,6 +86,7 @@ impl PythonVersion {
Self::Py311 => (3, 11),
Self::Py312 => (3, 12),
Self::Py313 => (3, 13),
Self::Py314 => (3, 14),
}
}
}

View file

@ -30,6 +30,10 @@ impl PythonVersion {
major: 3,
minor: 13,
};
pub const PY314: PythonVersion = PythonVersion {
major: 3,
minor: 14,
};
pub fn iter() -> impl Iterator<Item = PythonVersion> {
[
@ -40,6 +44,7 @@ impl PythonVersion {
PythonVersion::PY311,
PythonVersion::PY312,
PythonVersion::PY313,
PythonVersion::PY314,
]
.into_iter()
}
@ -49,6 +54,7 @@ impl PythonVersion {
Self::PY37
}
// TODO: change this to 314 when it is released
pub const fn latest() -> Self {
Self::PY313
}