[ty] Provide completions on TypeVars (#20943)

## Summary

closes https://github.com/astral-sh/ty/issues/1370

## Test Plan

New snapshot tests
This commit is contained in:
David Peter 2025-10-17 20:05:20 +02:00 committed by GitHub
parent c7e2bfd759
commit 6e7ff07065
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 73 additions and 2 deletions

View file

@ -14,7 +14,7 @@ use crate::types::call::{CallArguments, MatchedArgument};
use crate::types::signatures::Signature;
use crate::types::{
ClassBase, ClassLiteral, DynamicType, KnownClass, KnownInstanceType, Type,
class::CodeGeneratorKind,
TypeVarBoundOrConstraints, class::CodeGeneratorKind,
};
use crate::{Db, HasType, NameKind, SemanticModel};
use ruff_db::files::{File, FileRange};
@ -177,6 +177,29 @@ impl<'db> AllMembers<'db> {
Type::TypeAlias(alias) => self.extend_with_type(db, alias.value_type(db)),
Type::TypeVar(bound_typevar) => {
match bound_typevar.typevar(db).bound_or_constraints(db) {
None => {
self.extend_with_type(db, Type::object());
}
Some(TypeVarBoundOrConstraints::UpperBound(bound)) => {
self.extend_with_type(db, bound);
}
Some(TypeVarBoundOrConstraints::Constraints(constraints)) => {
self.members.extend(
constraints
.elements(db)
.iter()
.map(|ty| AllMembers::of(db, *ty).members)
.reduce(|acc, members| {
acc.intersection(&members).cloned().collect()
})
.unwrap_or_default(),
);
}
}
}
Type::IntLiteral(_)
| Type::BooleanLiteral(_)
| Type::StringLiteral(_)
@ -194,7 +217,6 @@ impl<'db> AllMembers<'db> {
| Type::ProtocolInstance(_)
| Type::SpecialForm(_)
| Type::KnownInstance(_)
| Type::TypeVar(_)
| Type::BoundSuper(_)
| Type::TypeIs(_) => match ty.to_meta_type(db) {
Type::ClassLiteral(class_literal) => {