[ty] Speedup the known_class_doesnt_fallback_to_unknown_unexpectedly_on_low_python_version test (#19760)

This commit is contained in:
Alex Waygood 2025-08-05 12:55:11 +01:00 committed by GitHub
parent 7df7be5c7d
commit 3af0b31de3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -4676,22 +4676,41 @@ mod tests {
fn known_class_doesnt_fallback_to_unknown_unexpectedly_on_low_python_version() { fn known_class_doesnt_fallback_to_unknown_unexpectedly_on_low_python_version() {
let mut db = setup_db(); let mut db = setup_db();
for class in KnownClass::iter() { // First, collect the `KnownClass` variants
// and sort them according to the version they were added in.
// This makes the test far faster as it minimizes the number of times
// we need to change the Python version in the loop.
let mut classes: Vec<(KnownClass, PythonVersion)> = KnownClass::iter()
.map(|class| {
let version_added = match class { let version_added = match class {
KnownClass::UnionType => PythonVersion::PY310, KnownClass::UnionType => PythonVersion::PY310,
KnownClass::BaseExceptionGroup | KnownClass::ExceptionGroup => PythonVersion::PY311, KnownClass::BaseExceptionGroup | KnownClass::ExceptionGroup => {
PythonVersion::PY311
}
KnownClass::GenericAlias => PythonVersion::PY39, KnownClass::GenericAlias => PythonVersion::PY39,
KnownClass::KwOnly => PythonVersion::PY310, KnownClass::KwOnly => PythonVersion::PY310,
KnownClass::Member | KnownClass::Nonmember => PythonVersion::PY311, KnownClass::Member | KnownClass::Nonmember => PythonVersion::PY311,
_ => PythonVersion::PY37, _ => PythonVersion::PY37,
}; };
(class, version_added)
})
.collect();
Program::get(&db) classes.sort_unstable_by_key(|(_, version)| *version);
let program = Program::get(&db);
let mut current_version = program.python_version(&db);
for (class, version_added) in classes {
if version_added != current_version {
program
.set_python_version_with_source(&mut db) .set_python_version_with_source(&mut db)
.to(PythonVersionWithSource { .to(PythonVersionWithSource {
version: version_added, version: version_added,
source: PythonVersionSource::default(), source: PythonVersionSource::default(),
}); });
current_version = version_added;
}
assert_ne!( assert_ne!(
class.to_instance(&db), class.to_instance(&db),