Visitar URL original
[ty] Replace `strsim` with CPython-based Levenshtein implementation by AlexWaygood · Pull Request #23291 · astral-sh/ruff · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move levenshtein module to diagnostic submodule, revert `types/diag…
…nostic.rs`

- Move `levenshtein.rs` from `types::diagnostic` to `diagnostic`
  (converting `diagnostic.rs` to `diagnostic/mod.rs`)
- Revert all changes to `types/diagnostic.rs`: restore `did_you_mean`
  import and original callsite instead of calling levenshtein directly
- Remove the `levenshtein` re-export from `types.rs`
- Use `HideUnderscoredSuggestions::Yes` in `did_you_mean`
- Update `_typos.toml` exclusion path to match new file location

https://claude.ai/code/session_014vwDhReNbswR4MXhYNcvBW
  • Loading branch information
claude committed Feb 15, 2026
commit 0def71dcf5ee8753a5ae478048d41888d7bc0a32
2 changes: 1 addition & 1 deletion _typos.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extend-exclude = [
# of them actually words. So just ignore typos here.
"crates/ty_ide/src/completion.rs",
# Same for "Did you mean...?" levenshtein tests.
"crates/ty_python_semantic/src/types/diagnostic/levenshtein.rs",
"crates/ty_python_semantic/src/diagnostic/levenshtein.rs",
]

[default.extend-words]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use std::collections::BTreeSet;
/// If the typo itself starts with an underscore, this policy is ignored.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum HideUnderscoredSuggestions {
#[cfg_attr(not(test), expect(dead_code))]
Yes,
#[cfg_attr(not(test), expect(dead_code))]
No,
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
use crate::{
Db, Program, PythonVersionWithSource, lint::lint_documentation_url, types::TypeCheckDiagnostics,
};
use levenshtein::{HideUnderscoredSuggestions, find_best_suggestion};
use ruff_db::{
diagnostic::{Annotation, Diagnostic, DiagnosticId, SubDiagnostic, SubDiagnosticSeverity},
files::File,
};
use std::cell::RefCell;
use std::fmt::Write;

pub(crate) mod levenshtein;

/// Suggest a name from `existing_names` that is similar to `wrong_name`.
pub(crate) fn did_you_mean<S: AsRef<str>, T: AsRef<str>>(
existing_names: impl Iterator<Item = S>,
wrong_name: T,
) -> Option<String> {
use crate::types::levenshtein::{HideUnderscoredSuggestions, find_best_suggestion};

let names: Vec<String> = existing_names.map(|n| n.as_ref().to_string()).collect();
let name_refs: Vec<&str> = names.iter().map(String::as_str).collect();
find_best_suggestion(
name_refs,
wrong_name.as_ref(),
HideUnderscoredSuggestions::No,
HideUnderscoredSuggestions::Yes,
)
.map(str::to_string)
}
Expand Down
1 change: 0 additions & 1 deletion crates/ty_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ use crate::unpack::EvaluationMode;
use crate::{Db, FxOrderSet, Program};
pub use class::KnownClass;
pub(crate) use class::{ClassLiteral, ClassType, GenericAlias, StaticClassLiteral};
pub(crate) use diagnostic::levenshtein;
use instance::Protocol;
pub use instance::{NominalInstanceType, ProtocolInstanceType};
pub use special_form::SpecialFormType;
Expand Down
11 changes: 3 additions & 8 deletions crates/ty_python_semantic/src/types/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use super::{
CallArguments, CallDunderError, ClassBase, ClassLiteral, KnownClass, StaticClassLiteral,
add_inferred_python_version_hint_to_diagnostic,
};
use crate::diagnostic::did_you_mean;
use crate::diagnostic::format_enumeration;
use crate::lint::{Level, LintRegistryBuilder, LintStatus};
use crate::place::{DefinedPlace, Place};
Expand Down Expand Up @@ -48,8 +49,6 @@ use rustc_hash::FxHashSet;
use std::fmt::{self, Formatter};
use ty_module_resolver::{KnownModule, Module, ModuleName, file_to_module};

pub(crate) mod levenshtein;

const RUNTIME_CHECKABLE_DOCS_URL: &str =
"https://docs.python.org/3/library/typing.html#typing.runtime_checkable";

Expand Down Expand Up @@ -4522,12 +4521,8 @@ pub(crate) fn report_invalid_key_on_typed_dict<'db>(
.message(format_args!("TypedDict `{typed_dict_name}`"))
});

let existing_keys = items.keys().map(|name| -> &str { name });
if let Some(suggestion) = levenshtein::find_best_suggestion(
existing_keys,
key,
levenshtein::HideUnderscoredSuggestions::No,
) {
let existing_keys = items.keys();
if let Some(suggestion) = did_you_mean(existing_keys, key) {
if let AnyNodeRef::ExprStringLiteral(literal) = key_node {
let quoted_suggestion = format!(
"{quote}{suggestion}{quote}",
Expand Down
Loading