[ty] Allow unions including Any/Unknown as bases (#18094)
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run

## Summary

Alternative fix for https://github.com/astral-sh/ty/issues/312

## Test Plan

New Markdown test
This commit is contained in:
David Peter 2025-05-16 06:57:26 +02:00 committed by GitHub
parent 7dc4fefb47
commit 6e39250015
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 82 additions and 38 deletions

View file

@ -123,7 +123,32 @@ impl<'db> ClassBase<'db> {
Some(valid_element)
}
}
Type::Union(_) => None, // TODO -- forces consideration of multiple possible MROs?
Type::Union(union) => {
// We do not support full unions of MROs (yet). Until we do,
// support the cases where one of the types in the union is
// a dynamic type such as `Any` or `Unknown`, and all other
// types *would be* valid class bases. In this case, we can
// "fold" the other potential bases into the dynamic type,
// and return `Any`/`Unknown` as the class base to prevent
// invalid-base diagnostics and further downstream errors.
let Some(Type::Dynamic(dynamic)) = union
.elements(db)
.iter()
.find(|elem| matches!(elem, Type::Dynamic(_)))
else {
return None;
};
if union
.elements(db)
.iter()
.all(|elem| ClassBase::try_from_type(db, *elem).is_some())
{
Some(ClassBase::Dynamic(*dynamic))
} else {
None
}
}
Type::NominalInstance(_) => None, // TODO -- handle `__mro_entries__`?
Type::PropertyInstance(_) => None,
Type::Never