[ty] Don't suggest things that aren't subclasses of BaseException after raise

This only applies to items that have a type associated with them. That
is, things that are already in scope. For items that don't have a type
associated with them (i.e., suggestions from auto-import), we still
suggest them since we can't know if they're appropriate or not. It's not
quite clear on how best to improve here for the auto-import case. (Short
of, say, asking for the type of each such symbol. But the performance
implications of that aren't known yet.)

Note that because of auto-import, we were still suggesting
`NotImplemented` even though astral-sh/ty#1262 specifically cites it as
the motivating example that we *shouldn't* suggest. This was occuring
because auto-import was including symbols from the `builtins` module,
even though those are actually already in scope. So this PR also gets
rid of those suggestions from auto-import.

Overall, this means that, at least, `raise NotImpl` won't suggest
`NotImplemented`.

Fixes astral-sh/ty#1262
This commit is contained in:
Andrew Gallant
2025-11-21 13:47:53 -05:00
committed by Andrew Gallant
parent a57e291311
commit 68343e7edf
7 changed files with 102 additions and 17 deletions

View File

@@ -12,8 +12,8 @@ pub use db::Db;
pub use diagnostic::add_inferred_python_version_hint_to_diagnostic;
pub use module_name::{ModuleName, ModuleNameResolutionError};
pub use module_resolver::{
Module, SearchPath, SearchPathValidationError, SearchPaths, all_modules, list_modules,
resolve_module, resolve_real_module, system_module_search_paths,
KnownModule, Module, SearchPath, SearchPathValidationError, SearchPaths, all_modules,
list_modules, resolve_module, resolve_real_module, system_module_search_paths,
};
pub use program::{
Program, ProgramSettings, PythonVersionFileSource, PythonVersionSource,

View File

@@ -1,7 +1,7 @@
use std::iter::FusedIterator;
pub use list::{all_modules, list_modules};
pub(crate) use module::KnownModule;
pub use module::KnownModule;
pub use module::Module;
pub use path::{SearchPath, SearchPathValidationError};
pub use resolver::SearchPaths;

View File

@@ -67,7 +67,7 @@ impl<'db> Module<'db> {
}
/// Does this module represent the given known module?
pub(crate) fn is_known(self, db: &'db dyn Database, known_module: KnownModule) -> bool {
pub fn is_known(self, db: &'db dyn Database, known_module: KnownModule) -> bool {
self.known(db) == Some(known_module)
}

View File

@@ -74,7 +74,8 @@ use crate::types::variance::VarianceInferable;
use crate::types::visitor::{any_over_type, exceeds_max_specialization_depth};
use crate::unpack::EvaluationMode;
use crate::{Db, FxOrderSet, Module, Program};
pub(crate) use class::{ClassLiteral, ClassType, GenericAlias, KnownClass};
pub use class::KnownClass;
pub(crate) use class::{ClassLiteral, ClassType, GenericAlias};
use instance::Protocol;
pub use instance::{NominalInstanceType, ProtocolInstanceType};
pub use special_form::SpecialFormType;
@@ -892,7 +893,7 @@ impl<'db> Type<'db> {
!(check_dunder("__eq__", true) && check_dunder("__ne__", false))
}
pub(crate) fn is_notimplemented(&self, db: &'db dyn Db) -> bool {
pub fn is_notimplemented(&self, db: &'db dyn Db) -> bool {
self.is_instance_of(db, KnownClass::NotImplementedType)
}
@@ -1670,8 +1671,8 @@ impl<'db> Type<'db> {
/// Return true if this type is assignable to type `target`.
///
/// See [`TypeRelation::Assignability`] for more details.
pub(crate) fn is_assignable_to(self, db: &'db dyn Db, target: Type<'db>) -> bool {
/// See `TypeRelation::Assignability` for more details.
pub fn is_assignable_to(self, db: &'db dyn Db, target: Type<'db>) -> bool {
self.when_assignable_to(db, target, InferableTypeVars::None)
.is_always_satisfied(db)
}
@@ -12108,7 +12109,7 @@ impl get_size2::GetSize for UnionType<'_> {}
impl<'db> UnionType<'db> {
/// Create a union from a list of elements
/// (which may be eagerly simplified into a different variant of [`Type`] altogether).
pub(crate) fn from_elements<I, T>(db: &'db dyn Db, elements: I) -> Type<'db>
pub fn from_elements<I, T>(db: &'db dyn Db, elements: I) -> Type<'db>
where
I: IntoIterator<Item = T>,
T: Into<Type<'db>>,

View File

@@ -4743,7 +4743,7 @@ impl KnownClass {
///
/// If the class cannot be found in typeshed, a debug-level log message will be emitted stating this.
#[track_caller]
pub(crate) fn to_instance(self, db: &dyn Db) -> Type<'_> {
pub fn to_instance(self, db: &dyn Db) -> Type<'_> {
debug_assert_ne!(
self,
KnownClass::Tuple,
@@ -4896,7 +4896,7 @@ impl KnownClass {
/// representing that class and all possible subclasses of the class.
///
/// If the class cannot be found in typeshed, a debug-level log message will be emitted stating this.
pub(crate) fn to_subclass_of(self, db: &dyn Db) -> Type<'_> {
pub fn to_subclass_of(self, db: &dyn Db) -> Type<'_> {
self.to_class_literal(db)
.to_class_type(db)
.map(|class| SubclassOfType::from(db, class))