Skip to content

Commit

Permalink
Remove AST checker's dependency on resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Mar 6, 2023
1 parent 694d418 commit aae3487
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 41 deletions.
4 changes: 2 additions & 2 deletions crates/ruff/src/ast/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use rustc_hash::FxHashMap;
use rustpython_parser::ast::{Expr, Stmt};
use smallvec::smallvec;

use ruff_python_stdlib::path::is_python_stub_file;
use ruff_python_stdlib::typing::TYPING_EXTENSIONS;

use crate::ast::helpers::{collect_call_path, from_relative_import, Exceptions};
use crate::ast::types::{Binding, BindingKind, CallPath, ExecutionContext, RefEquality, Scope};
use crate::ast::visibility::{module_visibility, Modifier, VisibleScope};
use crate::resolver::is_interface_definition_path;

#[allow(clippy::struct_excessive_bools)]
pub struct Context<'a> {
Expand Down Expand Up @@ -82,7 +82,7 @@ impl<'a> Context<'a> {
in_type_checking_block: false,
seen_import_boundary: false,
futures_allowed: true,
annotations_future_enabled: is_interface_definition_path(path),
annotations_future_enabled: is_python_stub_file(path),
handled_exceptions: Vec::default(),
}
}
Expand Down
24 changes: 12 additions & 12 deletions crates/ruff/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use rustpython_parser::ast::{
};

use ruff_python_stdlib::builtins::{BUILTINS, MAGIC_GLOBALS};
use ruff_python_stdlib::path::is_python_stub_file;

use crate::ast::context::Context;
use crate::ast::helpers::{binding_range, extract_handled_exceptions, to_module_path, Exceptions};
Expand All @@ -31,7 +32,6 @@ use crate::docstrings::definition::{
transition_scope, Definition, DefinitionKind, Docstring, Documentable,
};
use crate::registry::{Diagnostic, Rule};
use crate::resolver::is_interface_definition_path;
use crate::rules::{
flake8_2020, flake8_annotations, flake8_bandit, flake8_blind_except, flake8_boolean_trap,
flake8_bugbear, flake8_builtins, flake8_comprehensions, flake8_datetimez, flake8_debugger,
Expand All @@ -58,7 +58,7 @@ pub struct Checker<'a> {
pub path: &'a Path,
module_path: Option<Vec<String>>,
package: Option<&'a Path>,
is_interface_definition: bool,
is_stub: bool,
autofix: flags::Autofix,
noqa: flags::Noqa,
pub settings: &'a Settings,
Expand Down Expand Up @@ -97,7 +97,7 @@ impl<'a> Checker<'a> {
path,
package,
module_path: module_path.clone(),
is_interface_definition: is_interface_definition_path(path),
is_stub: is_python_stub_file(path),
locator,
stylist: style,
indexer,
Expand Down Expand Up @@ -378,7 +378,7 @@ where
}
}

if self.is_interface_definition {
if self.is_stub {
if self.settings.rules.enabled(&Rule::PassStatementStubBody) {
flake8_pyi::rules::pass_statement_stub_body(self, body);
}
Expand Down Expand Up @@ -756,7 +756,7 @@ where
flake8_bugbear::rules::useless_expression(self, body);
}

if !self.is_interface_definition {
if !self.is_stub {
if self
.settings
.rules
Expand All @@ -771,7 +771,7 @@ where
);
}
}
if self.is_interface_definition {
if self.is_stub {
if self.settings.rules.enabled(&Rule::PassStatementStubBody) {
flake8_pyi::rules::pass_statement_stub_body(self, body);
}
Expand Down Expand Up @@ -1805,7 +1805,7 @@ where
}
}

if self.is_interface_definition {
if self.is_stub {
if self.settings.rules.enabled(&Rule::PrefixTypeParams) {
flake8_pyi::rules::prefix_type_params(self, value, targets);
}
Expand Down Expand Up @@ -3301,7 +3301,7 @@ where
flake8_simplify::rules::yoda_conditions(self, expr, left, ops, comparators);
}

if self.is_interface_definition {
if self.is_stub {
if self
.settings
.rules
Expand Down Expand Up @@ -3886,7 +3886,7 @@ where
flake8_bugbear::rules::function_call_argument_default(self, arguments);
}

if self.is_interface_definition {
if self.is_stub {
if self
.settings
.rules
Expand All @@ -3895,7 +3895,7 @@ where
flake8_pyi::rules::typed_argument_simple_defaults(self, arguments);
}
}
if self.is_interface_definition {
if self.is_stub {
if self.settings.rules.enabled(&Rule::ArgumentSimpleDefaults) {
flake8_pyi::rules::argument_simple_defaults(self, arguments);
}
Expand Down Expand Up @@ -4710,7 +4710,7 @@ impl<'a> Checker<'a> {
}

fn check_dead_scopes(&mut self) {
let enforce_typing_imports = !self.is_interface_definition
let enforce_typing_imports = !self.is_stub
&& (self
.settings
.rules
Expand Down Expand Up @@ -5241,7 +5241,7 @@ impl<'a> Checker<'a> {
}
overloaded_name = flake8_annotations::helpers::overloaded_name(self, &definition);
}
if self.is_interface_definition {
if self.is_stub {
if self.settings.rules.enabled(&Rule::DocstringInStub) {
flake8_pyi::rules::docstring_in_stubs(self, definition.docstring);
}
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff/src/checkers/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn check_tokens(
tokens: &[LexResult],
settings: &Settings,
autofix: flags::Autofix,
is_interface_definition: bool,
is_stub: bool,
) -> Vec<Diagnostic> {
let mut diagnostics: Vec<Diagnostic> = vec![];

Expand Down Expand Up @@ -164,7 +164,7 @@ pub fn check_tokens(
}

// PYI033
if enforce_type_comment_in_stub && is_interface_definition {
if enforce_type_comment_in_stub && is_stub {
diagnostics.extend(flake8_pyi::rules::type_comment_in_stub(tokens));
}

Expand Down
12 changes: 3 additions & 9 deletions crates/ruff/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::path::Path;
use anyhow::{anyhow, Result};
use colored::Colorize;
use log::error;
use ruff_python_stdlib::path::is_python_stub_file;
use rustc_hash::FxHashMap;
use rustpython_parser::lexer::LexResult;
use rustpython_parser::ParseError;
Expand All @@ -21,7 +22,6 @@ use crate::doc_lines::{doc_lines_from_ast, doc_lines_from_tokens};
use crate::message::{Message, Source};
use crate::noqa::{add_noqa, rule_is_ignored};
use crate::registry::{Diagnostic, Rule};
use crate::resolver::is_interface_definition_path;
use crate::rules::pycodestyle;
use crate::settings::{flags, Settings};
use crate::source_code::{Indexer, Locator, Stylist};
Expand Down Expand Up @@ -84,14 +84,8 @@ pub fn check_path(
.iter_enabled()
.any(|rule_code| rule_code.lint_source().is_tokens())
{
let is_interface_definition = is_interface_definition_path(path);
diagnostics.extend(check_tokens(
locator,
&tokens,
settings,
autofix,
is_interface_definition,
));
let is_stub = is_python_stub_file(path);
diagnostics.extend(check_tokens(locator, &tokens, settings, autofix, is_stub));
}

// Run the filesystem-based rules.
Expand Down
14 changes: 2 additions & 12 deletions crates/ruff/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use ignore::{DirEntry, WalkBuilder, WalkState};
use itertools::Itertools;
use log::debug;
use path_absolutize::path_dedot;
use ruff_python_stdlib::path::is_python_file;
use rustc_hash::FxHashSet;

use crate::fs;
Expand Down Expand Up @@ -191,20 +192,9 @@ fn match_exclusion(file_path: &str, file_basename: &str, exclusion: &globset::Gl
exclusion.is_match(file_path) || exclusion.is_match(file_basename)
}

/// Return `true` if the [`Path`] appears to be that of a Python file.
fn is_python_path(path: &Path) -> bool {
path.extension()
.map_or(false, |ext| ext == "py" || ext == "pyi")
}

/// Return `true` if the [`Path`] appears to be that of a Python interface definition file (`.pyi`).
pub fn is_interface_definition_path(path: &Path) -> bool {
path.extension().map_or(false, |ext| ext == "pyi")
}

/// Return `true` if the [`DirEntry`] appears to be that of a Python file.
pub fn is_python_entry(entry: &DirEntry) -> bool {
is_python_path(entry.path())
is_python_file(entry.path())
&& !entry
.file_type()
.map_or(false, |file_type| file_type.is_dir())
Expand Down
8 changes: 4 additions & 4 deletions crates/ruff/src/rules/isort/track.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::path::Path;

use ruff_python_stdlib::path::is_python_stub_file;
use rustpython_parser::ast::{
Alias, Arg, Arguments, Boolop, Cmpop, Comprehension, Constant, Excepthandler,
ExcepthandlerKind, Expr, ExprContext, Keyword, MatchCase, Operator, Pattern, Stmt, StmtKind,
Expand All @@ -9,7 +10,6 @@ use rustpython_parser::ast::{
use super::helpers;
use crate::ast::visitor::Visitor;
use crate::directives::IsortDirectives;
use crate::resolver::is_interface_definition_path;
use crate::source_code::Locator;

#[derive(Debug)]
Expand All @@ -29,7 +29,7 @@ pub struct Block<'a> {
pub struct ImportTracker<'a> {
locator: &'a Locator<'a>,
directives: &'a IsortDirectives,
pyi: bool,
is_stub: bool,
blocks: Vec<Block<'a>>,
split_index: usize,
nested: bool,
Expand All @@ -40,7 +40,7 @@ impl<'a> ImportTracker<'a> {
Self {
locator,
directives,
pyi: is_interface_definition_path(path),
is_stub: is_python_stub_file(path),
blocks: vec![Block::default()],
split_index: 0,
nested: false,
Expand All @@ -65,7 +65,7 @@ impl<'a> ImportTracker<'a> {
return None;
}

Some(if self.pyi {
Some(if self.is_stub {
// Black treats interface files differently, limiting to one newline
// (`Trailing::Sibling`).
Trailer::Sibling
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_python_stdlib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod bytes;
pub mod future;
pub mod identifiers;
pub mod keyword;
pub mod path;
pub mod str;
pub mod sys;
pub mod typing;
12 changes: 12 additions & 0 deletions crates/ruff_python_stdlib/src/path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use std::path::Path;

/// Return `true` if the [`Path`] appears to be that of a Python file.
pub fn is_python_file(path: &Path) -> bool {
path.extension()
.map_or(false, |ext| ext == "py" || ext == "pyi")
}

/// Return `true` if the [`Path`] appears to be that of a Python interface definition file (`.pyi`).
pub fn is_python_stub_file(path: &Path) -> bool {
path.extension().map_or(false, |ext| ext == "pyi")
}

0 comments on commit aae3487

Please sign in to comment.