Skip to content

Commit

Permalink
Move list into typing
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed May 4, 2023
1 parent 73b4653 commit 69abeb0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
26 changes: 6 additions & 20 deletions crates/ruff/src/rules/flake8_future_annotations/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use rustpython_parser::ast::{Alias, Expr, Stmt};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_stdlib::typing::PEP_585_SUBSCRIPT_ELIGIBLE;

use crate::checkers::ast::Checker;

Expand Down Expand Up @@ -59,21 +60,6 @@ impl Violation for MissingFutureAnnotationsWithImports {
}
}

// PEP_593_SUBSCRIPTS
pub const FUTURE_ANNOTATIONS_REWRITE_ELIGIBLE: &[&[&str]] = &[
&["typing", "DefaultDict"],
&["typing", "Deque"],
&["typing", "Dict"],
&["typing", "FrozenSet"],
&["typing", "List"],
&["typing", "Optional"],
&["typing", "Set"],
&["typing", "Tuple"],
&["typing", "Type"],
&["typing", "Union"],
&["typing_extensions", "Type"],
];

/// FA100
pub fn missing_future_annotations_from_typing_import(
checker: &mut Checker,
Expand All @@ -85,17 +71,17 @@ pub fn missing_future_annotations_from_typing_import(
return;
}

let result: Vec<String> = names
let names: Vec<String> = names
.iter()
.map(|name| name.node.name.as_str())
.filter(|alias| FUTURE_ANNOTATIONS_REWRITE_ELIGIBLE.contains(&[module, alias].as_slice()))
.filter(|alias| PEP_585_SUBSCRIPT_ELIGIBLE.contains(&[module, alias].as_slice()))
.map(std::string::ToString::to_string)
.sorted()
.collect();

if !result.is_empty() {
if !names.is_empty() {
checker.diagnostics.push(Diagnostic::new(
MissingFutureAnnotationsWithImports { names: result },
MissingFutureAnnotationsWithImports { names },
stmt.range(),
));
}
Expand All @@ -108,7 +94,7 @@ pub fn missing_future_annotations_from_typing_usage(checker: &mut Checker, expr:
}

if let Some(binding) = checker.ctx.resolve_call_path(expr) {
if FUTURE_ANNOTATIONS_REWRITE_ELIGIBLE.contains(&binding.as_slice()) {
if PEP_585_SUBSCRIPT_ELIGIBLE.contains(&binding.as_slice()) {
checker.diagnostics.push(Diagnostic::new(
MissingFutureAnnotationsWithImports {
names: vec![binding.iter().join(".")],
Expand Down
15 changes: 15 additions & 0 deletions crates/ruff_python_stdlib/src/typing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,21 @@ pub const PEP_585_BUILTINS_ELIGIBLE: &[&[&str]] = &[
&["typing_extensions", "Type"],
];

// See: https://peps.python.org/pep-0585/
pub const PEP_585_SUBSCRIPT_ELIGIBLE: &[&[&str]] = &[
&["typing", "DefaultDict"],
&["typing", "Deque"],
&["typing", "Dict"],
&["typing", "FrozenSet"],
&["typing", "List"],
&["typing", "Optional"],
&["typing", "Set"],
&["typing", "Tuple"],
&["typing", "Type"],
&["typing", "Union"],
&["typing_extensions", "Type"],
];

// See: https://github.com/JelleZijlstra/autotyping/blob/0adba5ba0eee33c1de4ad9d0c79acfd737321dd9/autotyping/autotyping.py#L69-L91
pub static SIMPLE_MAGIC_RETURN_TYPES: Lazy<FxHashMap<&'static str, &'static str>> =
Lazy::new(|| {
Expand Down

0 comments on commit 69abeb0

Please sign in to comment.