mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 05:44:56 +00:00
[ty] Synthesize read-only properties for all declared members on NamedTuple
classes (#19899)
This commit is contained in:
parent
82350a398e
commit
f6093452ed
2 changed files with 74 additions and 5 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue