mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 13:51:16 +00:00
Filter private symbols from stubs if they are internal types (#19121)
This implements filtering of private symbols from stub files based on type information as discussed in https://github.com/astral-sh/ruff/pull/19102. It extends the previous implementation to apply to all stub files, instead of just the `builtins` module, and uses type information to retain private names that are may be relevant at runtime.
This commit is contained in:
parent
1b813cd5f1
commit
1c6717b149
4 changed files with 186 additions and 47 deletions
|
@ -68,12 +68,10 @@ impl<'db> SemanticModel<'db> {
|
|||
return vec![];
|
||||
};
|
||||
let ty = Type::module_literal(self.db, self.file, &module);
|
||||
let builtin = module.is_known(KnownModule::Builtins);
|
||||
crate::types::all_members(self.db, ty)
|
||||
.into_iter()
|
||||
.map(|name| Completion {
|
||||
name,
|
||||
builtin: module.is_known(KnownModule::Builtins),
|
||||
})
|
||||
.map(|name| Completion { name, builtin })
|
||||
.collect()
|
||||
}
|
||||
|
||||
|
@ -130,6 +128,39 @@ impl<'db> SemanticModel<'db> {
|
|||
}
|
||||
}
|
||||
|
||||
/// A classification of symbol names.
|
||||
///
|
||||
/// The ordering here is used for sorting completions.
|
||||
///
|
||||
/// This sorts "normal" names first, then dunder names and finally
|
||||
/// single-underscore names. This matches the order of the variants defined for
|
||||
/// this enum, which is in turn picked up by the derived trait implementation
|
||||
/// for `Ord`.
|
||||
#[derive(Clone, Copy, Eq, PartialEq, PartialOrd, Ord)]
|
||||
pub enum NameKind {
|
||||
Normal,
|
||||
Dunder,
|
||||
Sunder,
|
||||
}
|
||||
|
||||
impl NameKind {
|
||||
pub fn classify(name: &Name) -> NameKind {
|
||||
// Dunder needs a prefix and suffix double underscore.
|
||||
// When there's only a prefix double underscore, this
|
||||
// results in explicit name mangling. We let that be
|
||||
// classified as-if they were single underscore names.
|
||||
//
|
||||
// Ref: <https://docs.python.org/3/reference/lexical_analysis.html#reserved-classes-of-identifiers>
|
||||
if name.starts_with("__") && name.ends_with("__") {
|
||||
NameKind::Dunder
|
||||
} else if name.starts_with('_') {
|
||||
NameKind::Sunder
|
||||
} else {
|
||||
NameKind::Normal
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A suggestion for code completion.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Completion {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue