[ty] Set None as the definition of Self type variables (#22648)

This commit is contained in:
Alex Waygood
2026-01-18 19:04:40 +00:00
committed by GitHub
parent bab571c12c
commit 7c9803310e
2 changed files with 9 additions and 4 deletions

View File

@@ -4623,9 +4623,7 @@ impl<'db> BindingError<'db> {
}
}
if !typevar.is_self(context.db())
&& let Some(typevar_definition) = typevar.definition(context.db())
{
if let Some(typevar_definition) = typevar.definition(context.db()) {
let module = parsed_module(context.db(), typevar_definition.file(context.db()))
.load(context.db());
let typevar_range = typevar_definition.full_range(context.db(), &module);

View File

@@ -96,7 +96,14 @@ pub(crate) fn typing_self<'db>(
let identity = TypeVarIdentity::new(
db,
ast::name::Name::new_static("Self"),
class.definition(db),
// `Self` has a different upper bound dependent on the containing class,
// so pointing to the definition of the symbol `typing.Self` itself is
// not useful here. We could point to the class definition, but the full
// range of the class definition is much larger than the full range of a
// TypeVar would usually be, which leads to bugs like
// https://github.com/astral-sh/ty/issues/2514. So we just pass `None`
// for the definition field here.
None,
TypeVarKind::TypingSelf,
);
let bounds = TypeVarBoundOrConstraints::UpperBound(Type::instance(