Skip to content

Commit 989075d

Browse files
authored
[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 #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`
1 parent e8e2431 commit 989075d

File tree

1 file changed

+45
-5
lines changed
  • crates/red_knot_python_semantic/src/types

1 file changed

+45
-5
lines changed

crates/red_knot_python_semantic/src/types/class.rs

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -995,11 +995,10 @@ impl<'db> KnownClass {
995995
| Self::MethodWrapperType
996996
| Self::WrapperDescriptorType => KnownModule::Types,
997997
Self::NoneType => KnownModule::Typeshed,
998-
Self::SpecialForm
999-
| Self::TypeVar
1000-
| Self::TypeAliasType
1001-
| Self::StdlibAlias
1002-
| Self::SupportsIndex => KnownModule::Typing,
998+
Self::SpecialForm | Self::TypeVar | Self::StdlibAlias | Self::SupportsIndex => {
999+
KnownModule::Typing
1000+
}
1001+
Self::TypeAliasType => KnownModule::TypingExtensions,
10031002
Self::NoDefaultType => {
10041003
let python_version = Program::get(db).python_version(db);
10051004

@@ -1603,6 +1602,7 @@ mod tests {
16031602
use super::*;
16041603
use crate::db::tests::setup_db;
16051604
use crate::module_resolver::resolve_module;
1605+
use salsa::Setter;
16061606
use strum::IntoEnumIterator;
16071607

16081608
#[test]
@@ -1619,4 +1619,44 @@ mod tests {
16191619
);
16201620
}
16211621
}
1622+
1623+
#[test]
1624+
fn known_class_doesnt_fallback_to_unknown_unexpectedly_on_latest_version() {
1625+
let mut db = setup_db();
1626+
1627+
Program::get(&db)
1628+
.set_python_version(&mut db)
1629+
.to(PythonVersion::latest());
1630+
1631+
for class in KnownClass::iter() {
1632+
assert_ne!(
1633+
class.to_instance(&db),
1634+
Type::unknown(),
1635+
"Unexpectedly fell back to `Unknown` for `{class:?}`"
1636+
);
1637+
}
1638+
}
1639+
1640+
#[test]
1641+
fn known_class_doesnt_fallback_to_unknown_unexpectedly_on_low_python_version() {
1642+
let mut db = setup_db();
1643+
1644+
for class in KnownClass::iter() {
1645+
let version_added = match class {
1646+
KnownClass::BaseExceptionGroup => PythonVersion::PY311,
1647+
KnownClass::GenericAlias => PythonVersion::PY39,
1648+
_ => PythonVersion::PY37,
1649+
};
1650+
1651+
Program::get(&db)
1652+
.set_python_version(&mut db)
1653+
.to(version_added);
1654+
1655+
assert_ne!(
1656+
class.to_instance(&db),
1657+
Type::unknown(),
1658+
"Unexpectedly fell back to `Unknown` for `{class:?}` on Python {version_added}"
1659+
);
1660+
}
1661+
}
16221662
}

0 commit comments

Comments
 (0)