Skip to content
99 changes: 87 additions & 12 deletions pyrefly/lib/alt/class/class_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use pyrefly_graph::index::Idx;
use pyrefly_python::ast::Ast;
use pyrefly_python::dunder;
use pyrefly_python::short_identifier::ShortIdentifier;
use pyrefly_python::sys_info::PythonVersion;
use pyrefly_types::annotation::Annotation;
use pyrefly_types::callable::Params;
use pyrefly_types::known_class::KnownClass;
Expand Down Expand Up @@ -93,6 +94,52 @@ use crate::types::literal::Lit;
use crate::types::types::CalleeKind;
use crate::types::types::Type;

const RUNTIME_EXCLUDED_ATTRIBUTES: &[&str] = &[
"_is_protocol",
"_is_runtime_protocol",
"__abstractmethods__",
"__annotations__",
"__dict__",
"__doc__",
"__init__",
"__module__",
"__new__",
"__orig_bases__",
"__orig_class__",
"__parameters__",
"__slots__",
"__subclasshook__",
"__weakref__",
];

fn is_excluded_runtime_attribute(name: &Name, version: PythonVersion) -> bool {
let name = name.as_str();
if RUNTIME_EXCLUDED_ATTRIBUTES.contains(&name) {
return true;
}
// Ignore internal `_abc_*` attributes injected into class dictionaries by `ABCMeta`.
if name.starts_with("_abc_") {
return true;
}
match name {
// Users assign `= classmethod(types.GenericAlias)` for PEP 585-style generic
// subscription, or `= None` to disable it.
"__class_getitem__" => version.at_least(3, 9),

// CPython 3.13 stops treating a declared `__match_args__` as a runtime protocol
// member (gh-110682).
"__match_args__" => version.at_least(3, 13),

// PEP 749 exposes `__annotate__` as a public attribute of types and functions,
// making it part of the typing surface.
"__annotate__" => version.at_least(3, 14),

// Falling through here means either there is no realistic use case or the name
// should not be excluded. If you insist it should be, please open an issue.
_ => false,
}
}

#[derive(Debug, Clone)]
struct ParsedBaseClass<'a> {
class_object: Class,
Expand Down Expand Up @@ -209,7 +256,6 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> {
});

// Compute data that depends on the `BaseClass` representation of base classes.
let initial_protocol_metadata = self.initial_protocol_metadata(cls, bases);
let has_generic_base_class = bases.iter().any(|x| x.is_generic());
let has_typed_dict_base_class = bases.iter().any(|x| x.is_typed_dict());

Expand All @@ -220,12 +266,6 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> {
.map(|x| self.parse_base_class(x, is_new_type))
.collect::<Vec<_>>();
let contains_base_class_any = parsed_results.iter().any(|x| x.is_any());
let protocol_metadata = self.final_protocol_metadata(
initial_protocol_metadata,
&decorators,
&parsed_results,
errors,
);

let protocol_base_name = Name::new_static("Protocol");
let base_metaclasses = bases
Expand Down Expand Up @@ -307,6 +347,16 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> {
}
}
let metaclass = calculated_metaclass.as_ref().map(|m| m.get());

let explicit_slots = self.explicit_slots(cls, metaclass, errors);
let initial_protocol_metadata = self.initial_protocol_metadata(cls, bases, &explicit_slots);
let protocol_metadata = self.final_protocol_metadata(
initial_protocol_metadata,
&decorators,
&parsed_results,
errors,
);

// Compute base classes with metadata.
let bases_with_metadata = self.bases_with_metadata(parsed_results, is_new_type, errors);
self.check_init_subclass_keywords(
Expand Down Expand Up @@ -465,7 +515,6 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> {
.iter()
.find_map(|(decorator, _)| decorator.deprecation.clone());

let explicit_slots = self.explicit_slots(cls, metaclass, errors);
let has_nonempty_explicit_slots = explicit_slots
.slots_info()
.is_some_and(|slots| !slots.names.is_empty());
Expand Down Expand Up @@ -808,6 +857,7 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> {
&self,
cls: &Class,
bases: &[BaseClass],
explicit_slots: &ExplicitSlots,
) -> Option<ProtocolMetadata> {
if bases.iter().any(|x| {
matches!(
Expand All @@ -818,11 +868,33 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> {
})
)
}) {
// CPython excludes the literal `__slots__` but exposes non-excluded slot
// names as runtime attributes. An unresolvable declaration cannot prove
// data slots, so no error is reported for it.
let version = self.bindings().sys_info().version();
let mut members = SmallSet::new();
let mut runtime_members: SmallSet<Name> = explicit_slots
.slots_info()
.map(|slots| {
slots
.names
.iter()
.filter(|name| !is_excluded_runtime_attribute(name, version))
.cloned()
.collect()
})
.unwrap_or_default();
if let Some(fields) = self.get_class_fields(cls) {
for name in fields.class_body_fields() {
members.insert(name.clone());
if !is_excluded_runtime_attribute(name, version) {
runtime_members.insert(name.clone());
}
}
}
Some(ProtocolMetadata {
members: self
.get_class_fields(cls)
.map(|f| f.class_body_fields().cloned().collect())
.unwrap_or_default(),
members,
runtime_members,
is_runtime_checkable: false,
})
} else {
Expand All @@ -848,6 +920,9 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> {
{
if let Some(base_proto) = metadata.protocol_metadata() {
proto.members.extend(base_proto.members.iter().cloned());
proto
.runtime_members
.extend(base_proto.runtime_members.iter().cloned());
if base_proto.is_runtime_checkable {
proto.is_runtime_checkable = true;
}
Expand Down
128 changes: 85 additions & 43 deletions pyrefly/lib/alt/special_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* file contains the implementations of a few special calls that need to be hard-coded.
*/

use std::iter;

use pyrefly_python::dunder;
use pyrefly_types::display::TypeDisplayContext;
use pyrefly_types::known_class::KnownClass;
Expand Down Expand Up @@ -683,46 +685,57 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> {
}
// Check for unsafe overlap:
// https://typing.python.org/en/latest/spec/protocol.html#runtime-checkable-decorator-and-narrowing-types-by-isinstance
// We need to check if there is any field with unassignable types, since the `isinstance` check only
// checks for the presence of the fields, not their types.
// Presence decides whether an overlap is reported, but every static
// member is type-checked, including runtime-excluded ones the
// implementation does not provide.
//
// Type arguments for the protocol are not provided, so we'll use
// fresh vars and solve them during the `is_subset_eq` check below.
// fresh vars and solve them during the member checks below.
let class_info_protocol = class_info_metadata.protocol_metadata().unwrap();
if let Some(object_type) = &object_or_class
&& !object_type.is_never()
&& let (vs, Type::ClassType(protocol_class_type)) =
self.instantiate_fresh_class(class_info_cls)
{
let mut all_members_present = true;
let all_runtime_members_present = class_info_protocol
.runtime_members
.iter()
.all(|field_name| self.has_attr(object_type, field_name));
let mut unsafe_overlap_errors = vec![];
for field_name in &class_info_protocol.members {
if !self.has_attr(object_type, field_name) {
all_members_present = false;
break;
}
if let Err(subset_err) = self.is_protocol_subset_at_attr(
object_type,
&protocol_class_type,
field_name,
&mut |x, y| self.is_subset_eq_with_reason(x, y),
) {
let error_msg = subset_err
.to_error_msg(self)
.map(|msg| format!(": {msg}"))
.unwrap_or_default();
unsafe_overlap_errors.push(format!(
"Attribute `{}` has incompatible types{}",
field_name, error_msg,
));
if all_runtime_members_present {
for field_name in &class_info_protocol.members {
// Protocol machinery, not instance members; `subset.rs`'s
// `is_subset_protocol_inner` skips the same four names.
if field_name == &dunder::INIT
|| field_name == &dunder::NEW
|| field_name == &dunder::SLOTS
|| field_name == &dunder::CLASS_GETITEM
{
continue;
}
if let Err(subset_err) = self.is_protocol_subset_at_attr(
object_type,
&protocol_class_type,
field_name,
&mut |x, y| self.is_subset_eq_with_reason(x, y),
) {
let error_msg = subset_err
.to_error_msg(self)
.map(|msg| format!(": {msg}"))
.unwrap_or_default();
unsafe_overlap_errors.push(format!(
"Attribute `{}` has incompatible types{}",
field_name, error_msg,
));
}
}
}
if let Err(specialization_errors) = self.finish_quantified(vs, false) {
for e in specialization_errors {
unsafe_overlap_errors.push(e.to_error_msg(self))
}
}
if all_members_present && !unsafe_overlap_errors.is_empty() {
if all_runtime_members_present && !unsafe_overlap_errors.is_empty() {
errors.error_builder(
range,
ErrorKind::UnsafeOverlap,
Expand Down Expand Up @@ -791,29 +804,58 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> {
}

/// Check if a protocol is a data protocol (has non-method members)
///
/// Mirrors CPython's runtime check `callable(getattr(cls, name))`:
/// - `property` objects and `__slots__` descriptors are never callable (data members).
/// - Other members fall back to checking if their declared read type is callable.
fn is_data_protocol(&self, cls: &Class, range: TextRange) -> bool {
// A data protocol has at least one non-method member
// Use protocol metadata to get the member names
let metadata = self.get_metadata_for_class(cls);
if let Some(protocol_metadata) = metadata.protocol_metadata() {
for field_name in &protocol_metadata.members {
// Use the class type to access the field
let class_type = self.as_class_type_unchecked(cls);
let ty = self.type_of_attr_get(
&self.heap.mk_class_type(class_type),
field_name,
range,
&self.error_swallower(),
ErrorKind::MissingAttribute,
None,
"is_data_protocol",
);

// If it's not a callable type, it's a data member
if !ty.is_toplevel_callable() {
let Some(protocol_metadata) = self.get_metadata_for_class(cls).protocol_metadata() else {
return false;
};
let class_type = self.heap.mk_class_type(self.as_class_type_unchecked(cls));
// Skips `Protocol`, `Generic`, and `object`, matching CPython.
let ancestors = self.get_mro_for_class(cls).ancestors_no_object();
for field_name in &protocol_metadata.runtime_members {
for ancestor in
iter::once(cls).chain(ancestors.iter().map(|ancestor| ancestor.class_object()))
{
if self.get_class_fields(ancestor).is_some_and(|fields| {
fields.class_body_fields().any(|name| name == field_name)
&& fields.is_field_initialized_on_class(field_name)
}) {
// `property`s cannot be `callable` -> data member
if self
.get_field_from_current_class_only(ancestor, field_name)
.is_some_and(|field| field.is_property())
{
return true;
}
// Otherwise the declared read type decides.
break;
}
if self
.get_metadata_for_class(ancestor)
.slots_info()
.is_some_and(|slots| slots.names.contains(field_name))
{
// Slot descriptors cannot be `callable` -> data member
return true;
}
}
let ty = self.type_of_attr_get(
&class_type,
field_name,
range,
&self.error_swallower(),
ErrorKind::MissingAttribute,
None,
"is_data_protocol",
);

// If it's not a callable type, it's a data member
if !ty.is_toplevel_callable() {
return true;
}
}
false
}
Expand Down
4 changes: 3 additions & 1 deletion pyrefly/lib/alt/types/class_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,8 +770,10 @@ impl VisitTrait<Type> for DjangoReverseRelationIndex {

#[derive(Clone, Debug, TypeEq, PartialEq, Eq)]
pub struct ProtocolMetadata {
/// All members of the protocol, excluding ones defined on `object` and not overridden in a subclass.
/// All static members of the protocol, excluding ones defined on `object` and not overridden in a subclass.
pub members: SmallSet<Name>,
/// Attribute names CPython inspects when it checks this protocol at runtime.
pub runtime_members: SmallSet<Name>,
/// Whether this protocol is decorated with @runtime_checkable
pub is_runtime_checkable: bool,
}
Expand Down
Loading
Loading