[ty] Synthesize read-only properties for all declared members on NamedTuple classes (#19899)

This commit is contained in:
Alex Waygood 2025-08-14 22:25:45 +01:00 committed by GitHub
parent 82350a398e
commit f6093452ed
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 74 additions and 5 deletions

View file

@ -24,8 +24,8 @@ use crate::types::tuple::{TupleSpec, TupleType};
use crate::types::{
ApplyTypeMappingVisitor, BareTypeAliasType, Binding, BoundSuperError, BoundSuperType,
CallableType, DataclassParams, DeprecatedInstance, HasRelationToVisitor, KnownInstanceType,
NormalizedVisitor, StringLiteralType, TypeAliasType, TypeMapping, TypeRelation,
TypeVarBoundOrConstraints, TypeVarInstance, TypeVarKind, declaration_type,
NormalizedVisitor, PropertyInstanceType, StringLiteralType, TypeAliasType, TypeMapping,
TypeRelation, TypeVarBoundOrConstraints, TypeVarInstance, TypeVarKind, declaration_type,
infer_definition_types, todo_type,
};
use crate::{
@ -1862,6 +1862,18 @@ impl<'db> ClassLiteral<'db> {
.with_qualifiers(TypeQualifiers::CLASS_VAR);
}
if CodeGeneratorKind::NamedTuple.matches(db, self) {
if let Some(field) = self.own_fields(db, specialization).get(name) {
let property_getter_signature = Signature::new(
Parameters::new([Parameter::positional_only(Some(Name::new_static("self")))]),
Some(field.declared_ty),
);
let property_getter = CallableType::single(db, property_getter_signature);
let property = PropertyInstanceType::new(db, Some(property_getter), None);
return Place::bound(Type::PropertyInstance(property)).into();
}
}
let body_scope = self.body_scope(db);
let symbol = class_symbol(db, body_scope, name).map_type(|ty| {
// The `__new__` and `__init__` members of a non-specialized generic class are handled