[red-knot] Add tests asserting that KnownClass::to_instance() doesn't unexpectedly fallback to Type::Unknown with full typeshed stubs (#16608)

## Summary

One of the motivations in https://github.com/astral-sh/ruff/pull/16428
for panicking when the `test` or `debug_assertions` features are enabled
and a lookup of a `KnownClass` fails is that we've had some latent bugs
in our code where certain variants have been silently falling back to
`Unknown` in every typeshed lookup without us realising. But that in
itself isn't a great motivation for panicking in
`KnownClass::to_instance()`, since we can fairly easily add some tests
that assert that we don't unexpectedly fallback to `Unknown` for any
`KnownClass` variant. This PR adds those tests.

## Test Plan

`cargo test -p red_knot_python_semantic`
This commit is contained in:
Alex Waygood 2025-03-11 16:12:44 +00:00 committed by GitHub
parent e8e24310fb
commit 989075dc16
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -995,11 +995,10 @@ impl<'db> KnownClass {
| Self::MethodWrapperType
| Self::WrapperDescriptorType => KnownModule::Types,
Self::NoneType => KnownModule::Typeshed,
Self::SpecialForm
| Self::TypeVar
| Self::TypeAliasType
| Self::StdlibAlias
| Self::SupportsIndex => KnownModule::Typing,
Self::SpecialForm | Self::TypeVar | Self::StdlibAlias | Self::SupportsIndex => {
KnownModule::Typing
}
Self::TypeAliasType => KnownModule::TypingExtensions,
Self::NoDefaultType => {
let python_version = Program::get(db).python_version(db);
@ -1603,6 +1602,7 @@ mod tests {
use super::*;
use crate::db::tests::setup_db;
use crate::module_resolver::resolve_module;
use salsa::Setter;
use strum::IntoEnumIterator;
#[test]
@ -1619,4 +1619,44 @@ mod tests {
);
}
}
#[test]
fn known_class_doesnt_fallback_to_unknown_unexpectedly_on_latest_version() {
let mut db = setup_db();
Program::get(&db)
.set_python_version(&mut db)
.to(PythonVersion::latest());
for class in KnownClass::iter() {
assert_ne!(
class.to_instance(&db),
Type::unknown(),
"Unexpectedly fell back to `Unknown` for `{class:?}`"
);
}
}
#[test]
fn known_class_doesnt_fallback_to_unknown_unexpectedly_on_low_python_version() {
let mut db = setup_db();
for class in KnownClass::iter() {
let version_added = match class {
KnownClass::BaseExceptionGroup => PythonVersion::PY311,
KnownClass::GenericAlias => PythonVersion::PY39,
_ => PythonVersion::PY37,
};
Program::get(&db)
.set_python_version(&mut db)
.to(version_added);
assert_ne!(
class.to_instance(&db),
Type::unknown(),
"Unexpectedly fell back to `Unknown` for `{class:?}` on Python {version_added}"
);
}
}
}