mirror of https://github.com/astral-sh/ruff
Rename the `flake8-future-annotations` rules (#4716)
This commit is contained in:
parent
0106bce02f
commit
1846d90bbd
|
|
@ -2184,19 +2184,19 @@ where
|
|||
Expr::Subscript(ast::ExprSubscript { value, slice, .. }) => {
|
||||
// Ex) Optional[...], Union[...]
|
||||
if self.any_enabled(&[
|
||||
Rule::MissingFutureAnnotationsImportOldStyle,
|
||||
Rule::FutureRewritableTypeAnnotation,
|
||||
Rule::NonPEP604Annotation,
|
||||
]) {
|
||||
if let Some(operator) =
|
||||
analyze::typing::to_pep604_operator(value, slice, &self.semantic_model)
|
||||
{
|
||||
if self.enabled(Rule::MissingFutureAnnotationsImportOldStyle) {
|
||||
if self.enabled(Rule::FutureRewritableTypeAnnotation) {
|
||||
if self.settings.target_version < PythonVersion::Py310
|
||||
&& self.settings.target_version >= PythonVersion::Py37
|
||||
&& !self.semantic_model.future_annotations()
|
||||
&& self.semantic_model.in_annotation()
|
||||
{
|
||||
flake8_future_annotations::rules::missing_future_annotations_old_style(
|
||||
flake8_future_annotations::rules::future_rewritable_type_annotation(
|
||||
self, value,
|
||||
);
|
||||
}
|
||||
|
|
@ -2216,13 +2216,13 @@ where
|
|||
}
|
||||
|
||||
// Ex) list[...]
|
||||
if self.enabled(Rule::MissingFutureAnnotationsImportNewStyle) {
|
||||
if self.enabled(Rule::FutureRequiredTypeAnnotation) {
|
||||
if self.settings.target_version < PythonVersion::Py39
|
||||
&& !self.semantic_model.future_annotations()
|
||||
&& self.semantic_model.in_annotation()
|
||||
&& analyze::typing::is_pep585_generic(value, &self.semantic_model)
|
||||
{
|
||||
flake8_future_annotations::rules::missing_future_annotations_new_style(
|
||||
flake8_future_annotations::rules::future_required_type_annotation(
|
||||
self,
|
||||
expr,
|
||||
flake8_future_annotations::rules::Reason::PEP585,
|
||||
|
|
@ -2286,19 +2286,19 @@ where
|
|||
|
||||
// Ex) List[...]
|
||||
if self.any_enabled(&[
|
||||
Rule::MissingFutureAnnotationsImportOldStyle,
|
||||
Rule::FutureRewritableTypeAnnotation,
|
||||
Rule::NonPEP585Annotation,
|
||||
]) {
|
||||
if let Some(replacement) =
|
||||
analyze::typing::to_pep585_generic(expr, &self.semantic_model)
|
||||
{
|
||||
if self.enabled(Rule::MissingFutureAnnotationsImportOldStyle) {
|
||||
if self.enabled(Rule::FutureRewritableTypeAnnotation) {
|
||||
if self.settings.target_version < PythonVersion::Py39
|
||||
&& self.settings.target_version >= PythonVersion::Py37
|
||||
&& !self.semantic_model.future_annotations()
|
||||
&& self.semantic_model.in_annotation()
|
||||
{
|
||||
flake8_future_annotations::rules::missing_future_annotations_old_style(
|
||||
flake8_future_annotations::rules::future_rewritable_type_annotation(
|
||||
self, expr,
|
||||
);
|
||||
}
|
||||
|
|
@ -2364,19 +2364,19 @@ where
|
|||
Expr::Attribute(ast::ExprAttribute { attr, value, .. }) => {
|
||||
// Ex) typing.List[...]
|
||||
if self.any_enabled(&[
|
||||
Rule::MissingFutureAnnotationsImportOldStyle,
|
||||
Rule::FutureRewritableTypeAnnotation,
|
||||
Rule::NonPEP585Annotation,
|
||||
]) {
|
||||
if let Some(replacement) =
|
||||
analyze::typing::to_pep585_generic(expr, &self.semantic_model)
|
||||
{
|
||||
if self.enabled(Rule::MissingFutureAnnotationsImportOldStyle) {
|
||||
if self.enabled(Rule::FutureRewritableTypeAnnotation) {
|
||||
if self.settings.target_version < PythonVersion::Py39
|
||||
&& self.settings.target_version >= PythonVersion::Py37
|
||||
&& !self.semantic_model.future_annotations()
|
||||
&& self.semantic_model.in_annotation()
|
||||
{
|
||||
flake8_future_annotations::rules::missing_future_annotations_old_style(
|
||||
flake8_future_annotations::rules::future_rewritable_type_annotation(
|
||||
self, expr,
|
||||
);
|
||||
}
|
||||
|
|
@ -3214,12 +3214,12 @@ where
|
|||
..
|
||||
}) => {
|
||||
// Ex) `str | None`
|
||||
if self.enabled(Rule::MissingFutureAnnotationsImportNewStyle) {
|
||||
if self.enabled(Rule::FutureRequiredTypeAnnotation) {
|
||||
if self.settings.target_version < PythonVersion::Py310
|
||||
&& !self.semantic_model.future_annotations()
|
||||
&& self.semantic_model.in_annotation()
|
||||
{
|
||||
flake8_future_annotations::rules::missing_future_annotations_new_style(
|
||||
flake8_future_annotations::rules::future_required_type_annotation(
|
||||
self,
|
||||
expr,
|
||||
flake8_future_annotations::rules::Reason::PEP604,
|
||||
|
|
|
|||
|
|
@ -318,8 +318,8 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> {
|
|||
(Flake8Annotations, "401") => (RuleGroup::Unspecified, Rule::AnyType),
|
||||
|
||||
// flake8-future-annotations
|
||||
(Flake8FutureAnnotations, "100") => (RuleGroup::Unspecified, Rule::MissingFutureAnnotationsImportOldStyle),
|
||||
(Flake8FutureAnnotations, "102") => (RuleGroup::Unspecified, Rule::MissingFutureAnnotationsImportNewStyle),
|
||||
(Flake8FutureAnnotations, "100") => (RuleGroup::Unspecified, Rule::FutureRewritableTypeAnnotation),
|
||||
(Flake8FutureAnnotations, "102") => (RuleGroup::Unspecified, Rule::FutureRequiredTypeAnnotation),
|
||||
|
||||
// flake8-2020
|
||||
(Flake82020, "101") => (RuleGroup::Unspecified, Rule::SysVersionSlice3),
|
||||
|
|
|
|||
|
|
@ -267,8 +267,8 @@ ruff_macros::register_rules!(
|
|||
rules::flake8_annotations::rules::MissingReturnTypeClassMethod,
|
||||
rules::flake8_annotations::rules::AnyType,
|
||||
// flake8-future-annotations
|
||||
rules::flake8_future_annotations::rules::MissingFutureAnnotationsImportOldStyle,
|
||||
rules::flake8_future_annotations::rules::MissingFutureAnnotationsImportNewStyle,
|
||||
rules::flake8_future_annotations::rules::FutureRewritableTypeAnnotation,
|
||||
rules::flake8_future_annotations::rules::FutureRequiredTypeAnnotation,
|
||||
// flake8-2020
|
||||
rules::flake8_2020::rules::SysVersionSlice3,
|
||||
rules::flake8_2020::rules::SysVersion2,
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ mod tests {
|
|||
Path::new("flake8_future_annotations").join(path).as_path(),
|
||||
&settings::Settings {
|
||||
target_version: PythonVersion::Py37,
|
||||
..settings::Settings::for_rule(Rule::MissingFutureAnnotationsImportOldStyle)
|
||||
..settings::Settings::for_rule(Rule::FutureRewritableTypeAnnotation)
|
||||
},
|
||||
)?;
|
||||
assert_messages!(snapshot, diagnostics);
|
||||
|
|
@ -49,7 +49,7 @@ mod tests {
|
|||
Path::new("flake8_future_annotations").join(path).as_path(),
|
||||
&settings::Settings {
|
||||
target_version: PythonVersion::Py37,
|
||||
..settings::Settings::for_rule(Rule::MissingFutureAnnotationsImportNewStyle)
|
||||
..settings::Settings::for_rule(Rule::FutureRequiredTypeAnnotation)
|
||||
},
|
||||
)?;
|
||||
assert_messages!(snapshot, diagnostics);
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ use crate::checkers::ast::Checker;
|
|||
/// ...
|
||||
/// ```
|
||||
#[violation]
|
||||
pub struct MissingFutureAnnotationsImportNewStyle {
|
||||
pub struct FutureRequiredTypeAnnotation {
|
||||
reason: Reason,
|
||||
}
|
||||
|
||||
|
|
@ -56,22 +56,18 @@ impl fmt::Display for Reason {
|
|||
}
|
||||
}
|
||||
|
||||
impl Violation for MissingFutureAnnotationsImportNewStyle {
|
||||
impl Violation for FutureRequiredTypeAnnotation {
|
||||
#[derive_message_formats]
|
||||
fn message(&self) -> String {
|
||||
let MissingFutureAnnotationsImportNewStyle { reason } = self;
|
||||
let FutureRequiredTypeAnnotation { reason } = self;
|
||||
format!("Missing `from __future__ import annotations`, but uses {reason}")
|
||||
}
|
||||
}
|
||||
|
||||
/// FA102
|
||||
pub(crate) fn missing_future_annotations_new_style(
|
||||
checker: &mut Checker,
|
||||
expr: &Expr,
|
||||
reason: Reason,
|
||||
) {
|
||||
pub(crate) fn future_required_type_annotation(checker: &mut Checker, expr: &Expr, reason: Reason) {
|
||||
checker.diagnostics.push(Diagnostic::new(
|
||||
MissingFutureAnnotationsImportNewStyle { reason },
|
||||
FutureRequiredTypeAnnotation { reason },
|
||||
expr.range(),
|
||||
));
|
||||
}
|
||||
|
|
@ -50,20 +50,20 @@ use crate::checkers::ast::Checker;
|
|||
/// ...
|
||||
/// ```
|
||||
#[violation]
|
||||
pub struct MissingFutureAnnotationsImportOldStyle {
|
||||
pub struct FutureRewritableTypeAnnotation {
|
||||
name: String,
|
||||
}
|
||||
|
||||
impl Violation for MissingFutureAnnotationsImportOldStyle {
|
||||
impl Violation for FutureRewritableTypeAnnotation {
|
||||
#[derive_message_formats]
|
||||
fn message(&self) -> String {
|
||||
let MissingFutureAnnotationsImportOldStyle { name } = self;
|
||||
let FutureRewritableTypeAnnotation { name } = self;
|
||||
format!("Missing `from __future__ import annotations`, but uses `{name}`")
|
||||
}
|
||||
}
|
||||
|
||||
/// FA100
|
||||
pub(crate) fn missing_future_annotations_old_style(checker: &mut Checker, expr: &Expr) {
|
||||
pub(crate) fn future_rewritable_type_annotation(checker: &mut Checker, expr: &Expr) {
|
||||
let name = checker
|
||||
.semantic_model()
|
||||
.resolve_call_path(expr)
|
||||
|
|
@ -71,7 +71,7 @@ pub(crate) fn missing_future_annotations_old_style(checker: &mut Checker, expr:
|
|||
|
||||
if let Some(name) = name {
|
||||
checker.diagnostics.push(Diagnostic::new(
|
||||
MissingFutureAnnotationsImportOldStyle { name },
|
||||
FutureRewritableTypeAnnotation { name },
|
||||
expr.range(),
|
||||
));
|
||||
}
|
||||
|
|
@ -1,9 +1,9 @@
|
|||
pub(crate) use missing_future_annotations_new_style::{
|
||||
missing_future_annotations_new_style, MissingFutureAnnotationsImportNewStyle, Reason,
|
||||
pub(crate) use future_required_type_annotation::{
|
||||
future_required_type_annotation, FutureRequiredTypeAnnotation, Reason,
|
||||
};
|
||||
pub(crate) use missing_future_annotations_old_style::{
|
||||
missing_future_annotations_old_style, MissingFutureAnnotationsImportOldStyle,
|
||||
pub(crate) use future_rewritable_type_annotation::{
|
||||
future_rewritable_type_annotation, FutureRewritableTypeAnnotation,
|
||||
};
|
||||
|
||||
mod missing_future_annotations_new_style;
|
||||
mod missing_future_annotations_old_style;
|
||||
mod future_required_type_annotation;
|
||||
mod future_rewritable_type_annotation;
|
||||
|
|
|
|||
Loading…
Reference in New Issue