Use FNV hasher in more places (#732)

This commit is contained in:
Charlie Marsh 2022-11-13 23:44:16 -05:00 committed by GitHub
parent ce3c45a361
commit 71f727c380
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 71 additions and 69 deletions

1
Cargo.lock generated
View file

@ -935,6 +935,7 @@ dependencies = [
"anyhow", "anyhow",
"clap 4.0.22", "clap 4.0.22",
"configparser", "configparser",
"fnv",
"once_cell", "once_cell",
"regex", "regex",
"ruff", "ruff",

View file

@ -10,6 +10,7 @@ name = "flake8_to_ruff"
anyhow = { version = "1.0.66" } anyhow = { version = "1.0.66" }
clap = { version = "4.0.1", features = ["derive"] } clap = { version = "4.0.1", features = ["derive"] }
configparser = { version = "3.0.2" } configparser = { version = "3.0.2" }
fnv = { version = "1.0.7" }
once_cell = { version = "1.16.0" } once_cell = { version = "1.16.0" }
regex = { version = "1.6.0" } regex = { version = "1.6.0" }
ruff = { path = "..", default-features = false } ruff = { path = "..", default-features = false }

View file

@ -1,7 +1,7 @@
use std::collections::BTreeMap;
use std::str::FromStr; use std::str::FromStr;
use anyhow::Result; use anyhow::Result;
use fnv::FnvHashMap;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use regex::Regex; use regex::Regex;
use ruff::checks_gen::CheckCodePrefix; use ruff::checks_gen::CheckCodePrefix;
@ -179,8 +179,8 @@ pub fn parse_files_to_codes_mapping(value: &str) -> Result<Vec<PatternPrefixPair
/// Collect a list of `PatternPrefixPair` structs as a `BTreeMap`. /// Collect a list of `PatternPrefixPair` structs as a `BTreeMap`.
pub fn collect_per_file_ignores( pub fn collect_per_file_ignores(
pairs: Vec<PatternPrefixPair>, pairs: Vec<PatternPrefixPair>,
) -> BTreeMap<String, Vec<CheckCodePrefix>> { ) -> FnvHashMap<String, Vec<CheckCodePrefix>> {
let mut per_file_ignores: BTreeMap<String, Vec<CheckCodePrefix>> = BTreeMap::new(); let mut per_file_ignores: FnvHashMap<String, Vec<CheckCodePrefix>> = FnvHashMap::default();
for pair in pairs { for pair in pairs {
per_file_ignores per_file_ignores
.entry(pair.pattern) .entry(pair.pattern)

View file

@ -61,7 +61,7 @@ fn apply_fixes<'a>(
) -> Cow<'a, str> { ) -> Cow<'a, str> {
let mut output = RopeBuilder::new(); let mut output = RopeBuilder::new();
let mut last_pos: Location = Location::new(1, 0); let mut last_pos: Location = Location::new(1, 0);
let mut applied: BTreeSet<&Patch> = BTreeSet::new(); let mut applied: BTreeSet<&Patch> = BTreeSet::default();
for fix in fixes.sorted_by_key(|fix| fix.patch.location) { for fix in fixes.sorted_by_key(|fix| fix.patch.location) {
// If we already applied an identical fix as part of another correction, skip // If we already applied an identical fix as part of another correction, skip

View file

@ -1,8 +1,8 @@
use std::collections::BTreeMap;
use std::fmt; use std::fmt;
use std::path::PathBuf; use std::path::PathBuf;
use clap::{command, Parser}; use clap::{command, Parser};
use fnv::FnvHashMap;
use log::warn; use log::warn;
use regex::Regex; use regex::Regex;
@ -188,7 +188,7 @@ pub fn collect_per_file_ignores(
pairs: Vec<PatternPrefixPair>, pairs: Vec<PatternPrefixPair>,
project_root: &Option<PathBuf>, project_root: &Option<PathBuf>,
) -> Vec<PerFileIgnore> { ) -> Vec<PerFileIgnore> {
let mut per_file_ignores: BTreeMap<String, Vec<CheckCodePrefix>> = BTreeMap::new(); let mut per_file_ignores: FnvHashMap<String, Vec<CheckCodePrefix>> = FnvHashMap::default();
for pair in pairs { for pair in pairs {
per_file_ignores per_file_ignores
.entry(pair.pattern) .entry(pair.pattern)

View file

@ -1,11 +1,10 @@
//! Abstractions for Google-style docstrings. //! Abstractions for Google-style docstrings.
use std::collections::BTreeSet; use fnv::FnvHashSet;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
pub(crate) static GOOGLE_SECTION_NAMES: Lazy<BTreeSet<&'static str>> = Lazy::new(|| { pub(crate) static GOOGLE_SECTION_NAMES: Lazy<FnvHashSet<&'static str>> = Lazy::new(|| {
BTreeSet::from([ FnvHashSet::from_iter([
"Args", "Args",
"Arguments", "Arguments",
"Attention", "Attention",
@ -37,35 +36,36 @@ pub(crate) static GOOGLE_SECTION_NAMES: Lazy<BTreeSet<&'static str>> = Lazy::new
]) ])
}); });
pub(crate) static LOWERCASE_GOOGLE_SECTION_NAMES: Lazy<BTreeSet<&'static str>> = Lazy::new(|| { pub(crate) static LOWERCASE_GOOGLE_SECTION_NAMES: Lazy<FnvHashSet<&'static str>> =
BTreeSet::from([ Lazy::new(|| {
"args", FnvHashSet::from_iter([
"arguments", "args",
"attention", "arguments",
"attributes", "attention",
"caution", "attributes",
"danger", "caution",
"error", "danger",
"example", "error",
"examples", "example",
"hint", "examples",
"important", "hint",
"keyword args", "important",
"keyword arguments", "keyword args",
"methods", "keyword arguments",
"note", "methods",
"notes", "note",
"return", "notes",
"returns", "return",
"raises", "returns",
"references", "raises",
"see also", "references",
"tip", "see also",
"todo", "tip",
"warning", "todo",
"warnings", "warning",
"warns", "warnings",
"yield", "warns",
"yields", "yield",
]) "yields",
}); ])
});

View file

@ -1,11 +1,10 @@
//! Abstractions for NumPy-style docstrings. //! Abstractions for NumPy-style docstrings.
use std::collections::BTreeSet; use fnv::FnvHashSet;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
pub(crate) static LOWERCASE_NUMPY_SECTION_NAMES: Lazy<BTreeSet<&'static str>> = Lazy::new(|| { pub(crate) static LOWERCASE_NUMPY_SECTION_NAMES: Lazy<FnvHashSet<&'static str>> = Lazy::new(|| {
BTreeSet::from([ FnvHashSet::from_iter([
"short summary", "short summary",
"extended summary", "extended summary",
"parameters", "parameters",
@ -22,8 +21,8 @@ pub(crate) static LOWERCASE_NUMPY_SECTION_NAMES: Lazy<BTreeSet<&'static str>> =
]) ])
}); });
pub(crate) static NUMPY_SECTION_NAMES: Lazy<BTreeSet<&'static str>> = Lazy::new(|| { pub(crate) static NUMPY_SECTION_NAMES: Lazy<FnvHashSet<&'static str>> = Lazy::new(|| {
BTreeSet::from([ FnvHashSet::from_iter([
"Short Summary", "Short Summary",
"Extended Summary", "Extended Summary",
"Parameters", "Parameters",

View file

@ -1,5 +1,4 @@
use std::collections::BTreeSet; use fnv::FnvHashSet;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use crate::docstrings::google::{GOOGLE_SECTION_NAMES, LOWERCASE_GOOGLE_SECTION_NAMES}; use crate::docstrings::google::{GOOGLE_SECTION_NAMES, LOWERCASE_GOOGLE_SECTION_NAMES};
@ -11,14 +10,14 @@ pub(crate) enum SectionStyle {
} }
impl SectionStyle { impl SectionStyle {
pub(crate) fn section_names(&self) -> &Lazy<BTreeSet<&'static str>> { pub(crate) fn section_names(&self) -> &Lazy<FnvHashSet<&'static str>> {
match self { match self {
SectionStyle::NumPy => &NUMPY_SECTION_NAMES, SectionStyle::NumPy => &NUMPY_SECTION_NAMES,
SectionStyle::Google => &GOOGLE_SECTION_NAMES, SectionStyle::Google => &GOOGLE_SECTION_NAMES,
} }
} }
pub(crate) fn lowercase_section_names(&self) -> &Lazy<BTreeSet<&'static str>> { pub(crate) fn lowercase_section_names(&self) -> &Lazy<FnvHashSet<&'static str>> {
match self { match self {
SectionStyle::NumPy => &LOWERCASE_NUMPY_SECTION_NAMES, SectionStyle::NumPy => &LOWERCASE_NUMPY_SECTION_NAMES,
SectionStyle::Google => &LOWERCASE_GOOGLE_SECTION_NAMES, SectionStyle::Google => &LOWERCASE_GOOGLE_SECTION_NAMES,

View file

@ -1,11 +1,11 @@
use std::borrow::Cow; use std::borrow::Cow;
use std::collections::BTreeSet;
use std::fs::File; use std::fs::File;
use std::io::{BufReader, Read}; use std::io::{BufReader, Read};
use std::ops::Deref; use std::ops::Deref;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use fnv::FnvHashSet;
use log::debug; use log::debug;
use path_absolutize::{path_dedot, Absolutize}; use path_absolutize::{path_dedot, Absolutize};
use walkdir::{DirEntry, WalkDir}; use walkdir::{DirEntry, WalkDir};
@ -121,7 +121,7 @@ pub fn iter_python_files<'a>(
pub(crate) fn ignores_from_path<'a>( pub(crate) fn ignores_from_path<'a>(
path: &Path, path: &Path,
pattern_code_pairs: &'a [PerFileIgnore], pattern_code_pairs: &'a [PerFileIgnore],
) -> Result<BTreeSet<&'a CheckCode>> { ) -> Result<FnvHashSet<&'a CheckCode>> {
let (file_path, file_basename) = extract_path_names(path)?; let (file_path, file_basename) = extract_path_names(path)?;
Ok(pattern_code_pairs Ok(pattern_code_pairs
.iter() .iter()

View file

@ -1,5 +1,6 @@
use std::collections::BTreeSet; use std::collections::BTreeSet;
use fnv::FnvHashSet;
use itertools::Itertools; use itertools::Itertools;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use regex::Regex; use regex::Regex;
@ -1287,7 +1288,11 @@ fn common_section(
blanks_and_section_underline(checker, definition, context); blanks_and_section_underline(checker, definition, context);
} }
fn missing_args(checker: &mut Checker, definition: &Definition, docstrings_args: &BTreeSet<&str>) { fn missing_args(
checker: &mut Checker,
definition: &Definition,
docstrings_args: &FnvHashSet<&str>,
) {
if let DefinitionKind::Function(parent) if let DefinitionKind::Function(parent)
| DefinitionKind::NestedFunction(parent) | DefinitionKind::NestedFunction(parent)
| DefinitionKind::Method(parent) = definition.kind | DefinitionKind::Method(parent) = definition.kind
@ -1377,7 +1382,7 @@ fn args_section(checker: &mut Checker, definition: &Definition, context: &Sectio
checker, checker,
definition, definition,
// Collect the list of arguments documented in the docstring. // Collect the list of arguments documented in the docstring.
&BTreeSet::from_iter(args_sections.iter().filter_map(|section| { &FnvHashSet::from_iter(args_sections.iter().filter_map(|section| {
match GOOGLE_ARGS_REGEX.captures(section.as_str()) { match GOOGLE_ARGS_REGEX.captures(section.as_str()) {
Some(caps) => caps.get(1).map(|arg_name| arg_name.as_str()), Some(caps) => caps.get(1).map(|arg_name| arg_name.as_str()),
None => None, None => None,
@ -1388,7 +1393,7 @@ fn args_section(checker: &mut Checker, definition: &Definition, context: &Sectio
fn parameters_section(checker: &mut Checker, definition: &Definition, context: &SectionContext) { fn parameters_section(checker: &mut Checker, definition: &Definition, context: &SectionContext) {
// Collect the list of arguments documented in the docstring. // Collect the list of arguments documented in the docstring.
let mut docstring_args: BTreeSet<&str> = Default::default(); let mut docstring_args: FnvHashSet<&str> = FnvHashSet::default();
let section_level_indent = helpers::leading_space(context.line); let section_level_indent = helpers::leading_space(context.line);
for i in 1..context.following_lines.len() { for i in 1..context.following_lines.len() {
let current_line = context.following_lines[i - 1]; let current_line = context.following_lines[i - 1];

View file

@ -1,5 +1,4 @@
use std::collections::BTreeSet; use fnv::FnvHashSet;
use regex::Regex; use regex::Regex;
use rustpython_parser::ast::{ use rustpython_parser::ast::{
Arg, Arguments, Constant, Excepthandler, ExcepthandlerKind, Expr, ExprKind, Stmt, StmtKind, Arg, Arguments, Constant, Excepthandler, ExcepthandlerKind, Expr, ExprKind, Stmt, StmtKind,
@ -113,7 +112,7 @@ pub fn duplicate_arguments(arguments: &Arguments) -> Vec<Check> {
} }
// Search for duplicates. // Search for duplicates.
let mut idents: BTreeSet<&str> = BTreeSet::new(); let mut idents: FnvHashSet<&str> = FnvHashSet::default();
for arg in all_arguments { for arg in all_arguments {
let ident = &arg.node.arg; let ident = &arg.node.arg;
if idents.contains(ident.as_str()) { if idents.contains(ident.as_str()) {

View file

@ -1,10 +1,9 @@
use std::collections::BTreeSet; use fnv::FnvHashSet;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
// See: https://pycqa.github.io/isort/docs/configuration/options.html#known-standard-library // See: https://pycqa.github.io/isort/docs/configuration/options.html#known-standard-library
pub static KNOWN_STANDARD_LIBRARY: Lazy<BTreeSet<&'static str>> = Lazy::new(|| { pub static KNOWN_STANDARD_LIBRARY: Lazy<FnvHashSet<&'static str>> = Lazy::new(|| {
BTreeSet::from([ FnvHashSet::from_iter([
"_ast", "_ast",
"_dummy_thread", "_dummy_thread",
"_thread", "_thread",

View file

@ -1,7 +1,6 @@
//! Options that the user can provide via pyproject.toml. //! Options that the user can provide via pyproject.toml.
use std::collections::BTreeMap; use fnv::FnvHashMap;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::checks_gen::CheckCodePrefix; use crate::checks_gen::CheckCodePrefix;
@ -29,5 +28,5 @@ pub struct Options {
pub isort: Option<isort::settings::Options>, pub isort: Option<isort::settings::Options>,
pub pep8_naming: Option<pep8_naming::settings::Options>, pub pep8_naming: Option<pep8_naming::settings::Options>,
// Tables are required to go last. // Tables are required to go last.
pub per_file_ignores: Option<BTreeMap<String, Vec<CheckCodePrefix>>>, pub per_file_ignores: Option<FnvHashMap<String, Vec<CheckCodePrefix>>>,
} }

View file

@ -96,12 +96,12 @@ pub fn load_options(pyproject: &Option<PathBuf>) -> Result<Options> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::collections::BTreeMap;
use std::env::current_dir; use std::env::current_dir;
use std::path::PathBuf; use std::path::PathBuf;
use std::str::FromStr; use std::str::FromStr;
use anyhow::Result; use anyhow::Result;
use fnv::FnvHashMap;
use crate::checks_gen::CheckCodePrefix; use crate::checks_gen::CheckCodePrefix;
use crate::flake8_quotes::settings::Quote; use crate::flake8_quotes::settings::Quote;
@ -346,7 +346,7 @@ other-attribute = 1
extend_select: None, extend_select: None,
ignore: None, ignore: None,
extend_ignore: None, extend_ignore: None,
per_file_ignores: Some(BTreeMap::from([( per_file_ignores: Some(FnvHashMap::from_iter([(
"__init__.py".to_string(), "__init__.py".to_string(),
vec![CheckCodePrefix::F401] vec![CheckCodePrefix::F401]
),])), ),])),