From 223a4c8bd7b65e1e8ecd1bd40cf7d2480faa93de Mon Sep 17 00:00:00 2001 From: amas127 Date: Fri, 13 Nov 2026 04:32:59 -0500 Subject: [PATCH 1/7] Use the CPython runtime member set for protocol checks A runtime-checkable protocol declaring `__slots__ = ()` was classified as a data protocol, so `issubclass(bytes, BufferProtocol)` and `issubclass(int, SupportsInt)` reported `invalid-argument` even though CPython accepts both. The same happened for typeshed protocols such as `typing.SupportsInt` and `collections.abc.Buffer`, reported in #5614. `ProtocolMetadata.members` answered two questions at once: what structural subtyping requires, and what the runtime protocol check inspects. `__slots__` is an ordinary class-body field, so it entered `members`; the protocol subsetting code already skipped it, which is why the consumers disagreed. Add `runtime_members`, built from the class-body fields minus the names CPython's `typing._get_protocol_attrs` excludes, plus the slot names that survive the same filter. `has_data_slots` records whether the declaration proves a data attribute, because a slot name can resolve to an inherited callable and the member type alone cannot decide it. `is_data_protocol` and the unsafe-overlap presence check now read `runtime_members`. The overlap check compares types only for names in `members`: a slot-only name is part of the runtime contract but has no protocol-side type to compare. `_abc_*` is skipped by prefix, and `__class_getitem__` (3.9+), `__match_args__` (3.13+), and the `__annotate__` family (3.14+) are gated to match the CPython versions that exclude them. Inheritance merges both sets and the flag from protocol bases. An unresolvable `__slots__` value is treated as non-data. The runtime may reject the `issubclass()` call, but the checker cannot prove it; this is a deliberate false negative. Tests cover empty, non-empty, excluded, and inherited slots; string and annotation-only declarations; typeshed protocols; the version boundaries; and the unsafe-overlap presence rule. --- pyrefly/lib/alt/class/class_metadata.rs | 96 ++++- pyrefly/lib/alt/special_calls.rs | 27 +- pyrefly/lib/alt/types/class_metadata.rs | 11 + pyrefly/lib/test/protocol.rs | 526 ++++++++++++++++++++++++ 4 files changed, 647 insertions(+), 13 deletions(-) diff --git a/pyrefly/lib/alt/class/class_metadata.rs b/pyrefly/lib/alt/class/class_metadata.rs index 3a4d0b461e..1cd03de0eb 100644 --- a/pyrefly/lib/alt/class/class_metadata.rs +++ b/pyrefly/lib/alt/class/class_metadata.rs @@ -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; @@ -94,6 +95,58 @@ use crate::types::literal::Lit; use crate::types::types::CalleeKind; use crate::types::types::Type; +/// Names that CPython's `typing._get_protocol_attrs` excludes from the runtime +/// attribute set of a protocol on every version that provides `typing.Protocol`. +/// Names whose exclusion depends on the version or on a prefix are handled in +/// `is_excluded_runtime_attribute`. +/// +/// Static membership is a separate question: see `ProtocolMetadata::members`. +const RUNTIME_EXCLUDED_ATTRIBUTES: &[&str] = &[ + "_MutableMapping__marker", + "_is_protocol", + "_is_runtime_protocol", + "__abstractmethods__", + "__annotations__", + "__dict__", + "__doc__", + "__init__", + "__module__", + "__new__", + "__orig_bases__", + "__orig_class__", + "__parameters__", + "__slots__", + "__subclasshook__", + "__weakref__", +]; + +/// Whether CPython's `typing._get_protocol_attrs` excludes `name` from the runtime +/// attribute set of a protocol on `version`. +/// +/// The match arms cover the names a class body can declare directly. Names that +/// are injected by the runtime, the compiler, or a decorator, such as +/// `__static_attributes__`, `__non_callable_proto_members__`, and `__final__`, +/// are omitted: gating them would add micro-version windows for names that +/// ordinary code does not write. An explicit write therefore keeps the name in +/// the runtime set and produces a residual false positive, which is accepted. +fn is_excluded_runtime_attribute(name: &Name, version: PythonVersion) -> bool { + let name = name.as_str(); + if RUNTIME_EXCLUDED_ATTRIBUTES.contains(&name) { + return true; + } + // CPython skips `_abc_*` when collecting runtime protocol attributes; ABCMeta + // injects those names into class dictionaries. + if name.starts_with("_abc_") { + return true; + } + match name { + "__class_getitem__" => version.at_least(3, 9), + "__match_args__" => version.at_least(3, 13), + "__annotate__" | "__annotate_func__" | "__annotations_cache__" => version.at_least(3, 14), + _ => false, + } +} + #[derive(Debug, Clone)] struct ParsedBaseClass<'a> { class_object: Class, @@ -203,8 +256,10 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> { ) }); + let explicit_slots = self.explicit_slots(cls); + // Compute data that depends on the `BaseClass` representation of base classes. - let initial_protocol_metadata = self.initial_protocol_metadata(cls, bases); + let initial_protocol_metadata = self.initial_protocol_metadata(cls, bases, &explicit_slots); 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()); @@ -472,7 +527,6 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> { .iter() .find_map(|(decorator, _)| decorator.deprecation.clone()); - let explicit_slots = self.explicit_slots(cls); let has_nonempty_explicit_slots = explicit_slots .slots_info() .is_some_and(|slots| !slots.names.is_empty()); @@ -755,6 +809,7 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> { &self, cls: &Class, bases: &[BaseClass], + explicit_slots: &ExplicitSlots, ) -> Option { if bases.iter().any(|x| { matches!( @@ -765,11 +820,36 @@ 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 slot_runtime_members: SmallSet = explicit_slots + .slots_info() + .map(|slots| { + slots + .names + .iter() + .filter(|name| !is_excluded_runtime_attribute(name, version)) + .cloned() + .collect() + }) + .unwrap_or_default(); + let has_data_slots = !slot_runtime_members.is_empty(); + let mut members = SmallSet::new(); + let mut runtime_members = slot_runtime_members; + 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, + has_data_slots, is_runtime_checkable: false, }) } else { @@ -795,6 +875,10 @@ 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()); + proto.has_data_slots |= base_proto.has_data_slots; if base_proto.is_runtime_checkable { proto.is_runtime_checkable = true; } diff --git a/pyrefly/lib/alt/special_calls.rs b/pyrefly/lib/alt/special_calls.rs index f15623d233..23e517a5ff 100644 --- a/pyrefly/lib/alt/special_calls.rs +++ b/pyrefly/lib/alt/special_calls.rs @@ -695,11 +695,20 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> { { let mut all_members_present = true; let mut unsafe_overlap_errors = vec![]; - for field_name in &class_info_protocol.members { + for field_name in &class_info_protocol.runtime_members { if !self.has_attr(object_type, field_name) { all_members_present = false; break; } + // Unsafe overlap requires the class to be statically + // incompatible with the protocol. A name outside `members` + // is not part of the protocol's static contract, so it + // cannot contribute to an incompatibility. Slot-only names + // land here because the runtime member set carries + // presence, not a protocol-side type. + if !class_info_protocol.members.contains(field_name) { + continue; + } if let Err(subset_err) = self.is_protocol_subset_at_attr( object_type, &protocol_class_type, @@ -791,15 +800,19 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> { /// Check if a protocol is a data protocol (has non-method members) 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); + // A slot name can resolve to an inherited callable attribute, so the + // member types alone cannot prove data-ness; the flag records the slot + // declaration. + if protocol_metadata.has_data_slots { + return true; + } + let class_type = self.as_class_type_unchecked(cls); + let class_type = self.heap.mk_class_type(class_type); + for field_name in &protocol_metadata.runtime_members { let ty = self.type_of_attr_get( - &self.heap.mk_class_type(class_type), + &class_type, field_name, range, &self.error_swallower(), diff --git a/pyrefly/lib/alt/types/class_metadata.rs b/pyrefly/lib/alt/types/class_metadata.rs index b1e20974b2..39a50c151c 100644 --- a/pyrefly/lib/alt/types/class_metadata.rs +++ b/pyrefly/lib/alt/types/class_metadata.rs @@ -782,6 +782,17 @@ impl VisitTrait for DjangoReverseRelationIndex { pub struct ProtocolMetadata { /// All members of the protocol, excluding ones defined on `object` and not overridden in a subclass. pub members: SmallSet, + /// The runtime attribute names CPython's protocol checks inspect, used by the + /// runtime `isinstance`/`issubclass` checks. This includes non-excluded slot + /// names and inherited protocol members, and is version-aware; it never + /// contains the literal `__slots__`. + pub runtime_members: SmallSet, + /// Whether this protocol or an inherited protocol base declares at least one + /// `__slots__` name that CPython's runtime attribute filter keeps. An + /// unresolvable declaration does not count as data because no runtime + /// `TypeError` can be established for it. A child method that shadows an + /// inherited slot is still reported as data (a known gap). + pub has_data_slots: bool, /// Whether this protocol is decorated with @runtime_checkable pub is_runtime_checkable: bool, } diff --git a/pyrefly/lib/test/protocol.rs b/pyrefly/lib/test/protocol.rs index 852fe047ac..acbc9ff4c0 100644 --- a/pyrefly/lib/test/protocol.rs +++ b/pyrefly/lib/test/protocol.rs @@ -5,6 +5,8 @@ * LICENSE file in the root directory of this source tree. */ +use pyrefly_python::sys_info::PythonVersion; + use crate::test::util::TestEnv; use crate::testcase; @@ -1529,6 +1531,530 @@ issubclass(Implementation, DataOnlyProtocol) # E: Protocol `DataOnlyProtocol` "#, ); +testcase!( + test_runtime_checkable_protocol_slots, + r#" +from collections.abc import Buffer +from typing import Protocol, SupportsInt, runtime_checkable + +@runtime_checkable +class BufferProtocol(Protocol): + __slots__ = () + + def __buffer__(self, flags: int, /) -> memoryview: ... + +class BytesLike: + def __buffer__(self, flags: int, /) -> memoryview: ... + +# An empty `__slots__` declaration is not a protocol member. +issubclass(BytesLike, BufferProtocol) +issubclass(bytes, BufferProtocol) + +# typeshed declares these runtime-checkable protocols with `__slots__ = ()`. +issubclass(int, SupportsInt) +issubclass(bytes, Buffer) + +# The unsafe-overlap check inspects runtime members, so the wrong-typed +# `__buffer__` is reported here. +class WrongBuffer: + def __buffer__(self, flags: int, /) -> bytes: ... +isinstance(WrongBuffer(), BufferProtocol) # E: Runtime checkable protocol `BufferProtocol` has an unsafe overlap with type `WrongBuffer` + +@runtime_checkable +class DataWithEmptySlots(Protocol): + __slots__ = () + name: str + +issubclass(BytesLike, DataWithEmptySlots) # E: Protocol `DataWithEmptySlots` has non-method members and cannot be used with issubclass() + +# Non-empty slots declare data attributes, so the protocol is a data protocol. +@runtime_checkable +class NonEmptySlots(Protocol): + __slots__ = ("name",) + + def __buffer__(self, flags: int, /) -> memoryview: ... + +issubclass(BytesLike, NonEmptySlots) # E: Protocol `NonEmptySlots` has non-method members and cannot be used with issubclass() + +# A dict `__slots__` contributes its keys as slot names. +@runtime_checkable +class DictSlots(Protocol): + __slots__ = {"name": "doc"} + + def __buffer__(self, flags: int, /) -> memoryview: ... + +issubclass(BytesLike, DictSlots) # E: Protocol `DictSlots` has non-method members and cannot be used with issubclass() +"#, +); + +// Unresolved `__slots__` is not treated as data. The runtime value may be +// non-empty, but static analysis cannot prove it, so the diagnostic is suppressed. +testcase!( + bug = "Unresolved `__slots__` is treated as non-data, so these runtime TypeErrors are not reported", + test_runtime_checkable_protocol_unknown_slots, + r#" +from typing import Protocol, runtime_checkable + +def get_slots() -> tuple[str, ...]: + return ("name",) + +SLOTS = ("name",) + +@runtime_checkable +class UnknownSlots(Protocol): + __slots__ = get_slots() + + def m(self) -> None: ... + +@runtime_checkable +class ConstantSlots(Protocol): + __slots__ = SLOTS + + def m(self) -> None: ... + +class Impl: + def m(self) -> None: ... + +issubclass(Impl, UnknownSlots) +issubclass(Impl, ConstantSlots) +"#, +); + +// A string `__slots__` declares one slot. +testcase!( + test_runtime_checkable_protocol_string_slots, + r#" +from typing import Protocol, runtime_checkable + +@runtime_checkable +class OneStringSlot(Protocol): + __slots__ = "name" + + def m(self) -> None: ... + +class Impl: + def m(self) -> None: ... + +issubclass(Impl, OneStringSlot) # E: Protocol `OneStringSlot` has non-method members and cannot be used with issubclass() +"#, +); + +testcase!( + test_runtime_checkable_protocol_excluded_attributes, + r#" +from typing import Any, Protocol, runtime_checkable + +# CPython's `typing.EXCLUDED_ATTRIBUTES` names are not protocol members even when +# the protocol declares them explicitly. +@runtime_checkable +class ProtocolWithDunders(Protocol): + __doc__: str | None + __module__: str + __annotations__: dict[str, Any] + __match_args__: tuple[str, ...] + + def m(self) -> None: ... + +class Impl: + def m(self) -> None: ... + +issubclass(Impl, ProtocolWithDunders) +"#, +); + +// CPython filters slot descriptor names through EXCLUDED_ATTRIBUTES before +// deciding whether a protocol has non-method members. +testcase!( + test_runtime_checkable_protocol_slots_excluded_names, + r#" +from typing import Protocol, runtime_checkable + +@runtime_checkable +class DictSlot(Protocol): + __slots__ = ("__dict__",) + def m(self) -> None: ... + +class Impl: + def m(self) -> None: ... + +issubclass(Impl, DictSlot) +"#, +); + +// CPython skips `_abc_*` when collecting runtime protocol members, for both +// annotations and slot names. The names remain static protocol members. +testcase!( + test_runtime_checkable_protocol_abc_marker, + r#" +from typing import Protocol, runtime_checkable + +@runtime_checkable +class Annotated(Protocol): + _abc_marker: int + def m(self) -> None: ... + +@runtime_checkable +class SlotNamed(Protocol): + __slots__ = ("_abc_marker",) + def m(self) -> None: ... + +class Impl: + def m(self) -> None: ... + +issubclass(Impl, Annotated) +issubclass(Impl, SlotNamed) +"#, +); + +// An annotation-only `__slots__` declares no slot descriptors and no data. +testcase!( + test_runtime_checkable_protocol_annotated_slots, + r#" +from typing import Protocol, runtime_checkable + +@runtime_checkable +class AnnotatedOnly(Protocol): + __slots__: tuple[str, ...] + def m(self) -> None: ... + +class Impl: + def m(self) -> None: ... + +issubclass(Impl, AnnotatedOnly) +"#, +); + +// A PEP 695 generic protocol with empty slots is not a data protocol. +testcase!( + test_runtime_checkable_protocol_generic_empty_slots, + TestEnv::new_with_version(PythonVersion::new(3, 12, 0)), + r#" +from typing import Protocol, runtime_checkable + +@runtime_checkable +class P[T](Protocol): + __slots__ = () + def m(self, value: T) -> T: ... + +class Impl: + def m(self, value: int) -> int: ... + +issubclass(Impl, P) +"#, +); + +// Data slots inherited from a base protocol still make the child a data protocol; +// excluded slot names do not. +testcase!( + test_runtime_checkable_protocol_slots_inheritance, + r#" +from typing import Protocol, runtime_checkable + +@runtime_checkable +class DataBase(Protocol): + __slots__ = ("value",) + def m(self) -> None: ... + +@runtime_checkable +class DataChild(DataBase, Protocol): + __slots__ = () + def m(self) -> None: ... + +@runtime_checkable +class ExcludedBase(Protocol): + __slots__ = ("__weakref__",) + def m(self) -> None: ... + +@runtime_checkable +class EmptyChild(ExcludedBase, Protocol): + __slots__ = () + def m(self) -> None: ... + +class Impl: + def m(self) -> None: ... + +# A slotted implementation exercises the implementation-side `__slots__` +# attribute, which is not a protocol member. +class SlottedImpl: + __slots__ = ("value", "__weakref__") + def m(self) -> None: ... + +issubclass(Impl, DataChild) # E: Protocol `DataChild` has non-method members and cannot be used with issubclass() +issubclass(Impl, EmptyChild) +issubclass(SlottedImpl, DataChild) # E: Protocol `DataChild` has non-method members and cannot be used with issubclass() +issubclass(SlottedImpl, EmptyChild) +"#, +); + +// A slot in the child shadows an inherited method at runtime. The member type +// alone resolves to the inherited callable, so the slot declaration must mark +// the child as a data protocol. +testcase!( + test_runtime_checkable_protocol_child_slot_shadows_inherited_method, + r#" +from typing import Protocol, runtime_checkable + +@runtime_checkable +class Base(Protocol): + def value(self) -> None: ... + +@runtime_checkable +class Child(Base, Protocol): + __slots__ = ("value",) + +class Impl: + def value(self) -> None: ... + +issubclass(Impl, Child) # E: Protocol `Child` has non-method members and cannot be used with issubclass() +"#, +); + +// A method in the child shadows an inherited slot at runtime, so CPython accepts +// the check. The inherited flag still marks the child as data. +testcase!( + bug = "A child method shadowing an inherited slot reports despite runtime acceptance", + test_runtime_checkable_protocol_inherited_slot_shadowed_by_method, + r#" +from typing import Protocol, runtime_checkable + +@runtime_checkable +class SlotBase(Protocol): + __slots__ = ("value",) + +@runtime_checkable +class MethodChild(SlotBase, Protocol): + def value(self) -> None: ... + +class Impl: + def value(self) -> None: ... + +issubclass(Impl, MethodChild) # E: Protocol `MethodChild` has non-method members and cannot be used with issubclass() +"#, +); + +// `__match_args__` is a real protocol member on 3.12 and excluded on 3.13+. +// The slot and annotation forms share the same runtime predicate; the 3.13 side +// is covered by `test_runtime_checkable_protocol_excluded_attributes`. +testcase!( + test_runtime_checkable_excluded_match_args_3_12, + TestEnv::new_with_version(PythonVersion::new(3, 12, 0)), + r#" +from typing import Protocol, runtime_checkable + +@runtime_checkable +class SlotForm(Protocol): + __slots__ = ("__match_args__",) + def m(self) -> None: ... + +@runtime_checkable +class AnnotationForm(Protocol): + __match_args__: tuple[str, ...] + def m(self) -> None: ... + +class Impl: + def m(self) -> None: ... + +issubclass(Impl, SlotForm) # E: Protocol `SlotForm` has non-method members and cannot be used with issubclass() +issubclass(Impl, AnnotationForm) # E: Protocol `AnnotationForm` has non-method members and cannot be used with issubclass() +"#, +); + +// On 3.8 `__class_getitem__` is not excluded from the runtime attribute set. +// An explicit non-callable binding shadows the inherited +// `Generic.__class_getitem__` and makes the protocol a data protocol. +testcase!( + test_runtime_checkable_class_getitem_3_8_assignment, + TestEnv::new_with_version(PythonVersion::new(3, 8, 0)), + r#" +from typing import Protocol, runtime_checkable + +@runtime_checkable +class P(Protocol): + __class_getitem__ = 1 + def m(self) -> None: ... + +class Impl: + def m(self) -> None: ... + +issubclass(Impl, P) # E: Protocol `P` has non-method members and cannot be used with issubclass() +"#, +); + +// On 3.9+, CPython excludes the name, so the same assignment does not report. +testcase!( + test_runtime_checkable_excluded_class_getitem_3_9, + TestEnv::new_with_version(PythonVersion::new(3, 9, 0)), + r#" +from typing import Protocol, runtime_checkable + +@runtime_checkable +class P(Protocol): + __class_getitem__ = 1 + def m(self) -> None: ... + +class Impl: + def m(self) -> None: ... + +issubclass(Impl, P) +"#, +); + +// On 3.8 `__class_getitem__` is not reserved, so an annotation-only declaration +// is a protocol data member under the declaration-based model. CPython resolves +// the inherited `Generic.__class_getitem__` and accepts it, so this is a +// deliberate false positive. +testcase!( + bug = "3.8 annotation-only form reports even though CPython resolves the inherited method", + test_runtime_checkable_protocol_annotated_class_getitem_3_8, + TestEnv::new_with_version(PythonVersion::new(3, 8, 0)), + r#" +from typing import Protocol, runtime_checkable + +@runtime_checkable +class P(Protocol): + __class_getitem__: object + def m(self) -> None: ... + +class Impl: + def m(self) -> None: ... + +issubclass(Impl, P) # E: Protocol `P` has non-method members and cannot be used with issubclass() +"#, +); + +// `__annotate__` is excluded starting in 3.14. +testcase!( + test_runtime_checkable_excluded_annotate_3_13, + TestEnv::new_with_version(PythonVersion::new(3, 13, 0)), + r#" +from typing import Protocol, runtime_checkable + +@runtime_checkable +class P(Protocol): + __annotate__: object + def m(self) -> None: ... + +class Impl: + def m(self) -> None: ... + +issubclass(Impl, P) # E: Protocol `P` has non-method members and cannot be used with issubclass() +"#, +); + +testcase!( + test_runtime_checkable_excluded_annotate_3_14, + TestEnv::new_with_version(PythonVersion::new(3, 14, 0)), + r#" +from typing import Protocol, runtime_checkable + +@runtime_checkable +class P(Protocol): + __annotate__: object + def m(self) -> None: ... + +class Impl: + def m(self) -> None: ... + +issubclass(Impl, P) +"#, +); + +// Slot names participate in the runtime member-presence check, while real data +// members are still type-checked when they are present. +testcase!( + test_runtime_checkable_unsafe_overlap_nonempty_slots, + r#" +from typing import Protocol, runtime_checkable + +@runtime_checkable +class P(Protocol): + __slots__ = ("name",) + name: int + def m(self) -> int: ... + +class Mismatched: + name: str = "x" + def m(self) -> int: ... + +isinstance(Mismatched(), P) # E: Runtime checkable protocol `P` has an unsafe overlap with type `Mismatched` + +class DifferentSlots: + __slots__ = ("x", "name") + def m(self) -> int: ... + +isinstance(DifferentSlots(), P) + +class DifferentSlotsNoName: + __slots__ = ("x",) + def m(self) -> int: ... + +isinstance(DifferentSlotsNoName(), P) + +@runtime_checkable +class SlotOnly(Protocol): + __slots__ = ("name",) + def m(self) -> int: ... + +class MissingSlotBadMethod: + def m(self) -> str: ... + +isinstance(MissingSlotBadMethod(), SlotOnly) +"#, +); + +// A slot-only name participates in the presence check but has no +// protocol-side type, so it must not be compared; other members still are. +testcase!( + test_runtime_checkable_unsafe_overlap_slot_only_members, + r#" +from typing import Protocol, runtime_checkable + +@runtime_checkable +class SlotOnly(Protocol): + __slots__ = ("name",) + def m(self) -> int: ... + +class Annotated: + name: str + def m(self) -> int: ... + +class Assigned: + name: int = 1 + def m(self) -> int: ... + +isinstance(Annotated(), SlotOnly) +isinstance(Assigned(), SlotOnly) + +class BadMethod: + name: int = 1 + def m(self) -> str: ... + +isinstance(BadMethod(), SlotOnly) # E: Runtime checkable protocol `SlotOnly` has an unsafe overlap with type `BadMethod` +"#, +); + +// Private slot names are mangled at runtime, but the runtime member set stores +// the literal name. The presence check therefore looks for `__x` and misses +// `_P__x`, which suppresses the unsafe-overlap diagnostic. +testcase!( + bug = "Private slot names are not mangled, so the presence check misses `_P__x`", + test_runtime_checkable_protocol_private_slot_name, + r#" +from typing import Protocol, runtime_checkable + +@runtime_checkable +class P(Protocol): + __slots__ = ("__x",) + def m(self) -> None: ... + +class Impl: + __slots__ = ("_P__x",) + def m(self) -> str: ... + +isinstance(Impl(), P) +"#, +); + testcase!( test_runtime_checkable_non_protocol, r#" From bbac008cbddb579fee431e739e5d8a9513d59915 Mon Sep 17 00:00:00 2001 From: amas127 Date: Fri, 13 Nov 2026 05:41:52 -0500 Subject: [PATCH 2/7] Remove unnecessary comments --- pyrefly/lib/alt/class/class_metadata.rs | 16 ++-------------- pyrefly/lib/alt/special_calls.rs | 4 +--- 2 files changed, 3 insertions(+), 17 deletions(-) diff --git a/pyrefly/lib/alt/class/class_metadata.rs b/pyrefly/lib/alt/class/class_metadata.rs index 1cd03de0eb..220e68c406 100644 --- a/pyrefly/lib/alt/class/class_metadata.rs +++ b/pyrefly/lib/alt/class/class_metadata.rs @@ -95,11 +95,7 @@ use crate::types::literal::Lit; use crate::types::types::CalleeKind; use crate::types::types::Type; -/// Names that CPython's `typing._get_protocol_attrs` excludes from the runtime -/// attribute set of a protocol on every version that provides `typing.Protocol`. -/// Names whose exclusion depends on the version or on a prefix are handled in -/// `is_excluded_runtime_attribute`. -/// +/// Attributes excluded by `typing._get_protocol_attrs` at runtime. /// Static membership is a separate question: see `ProtocolMetadata::members`. const RUNTIME_EXCLUDED_ATTRIBUTES: &[&str] = &[ "_MutableMapping__marker", @@ -122,20 +118,12 @@ const RUNTIME_EXCLUDED_ATTRIBUTES: &[&str] = &[ /// Whether CPython's `typing._get_protocol_attrs` excludes `name` from the runtime /// attribute set of a protocol on `version`. -/// -/// The match arms cover the names a class body can declare directly. Names that -/// are injected by the runtime, the compiler, or a decorator, such as -/// `__static_attributes__`, `__non_callable_proto_members__`, and `__final__`, -/// are omitted: gating them would add micro-version windows for names that -/// ordinary code does not write. An explicit write therefore keeps the name in -/// the runtime set and produces a residual false positive, which is accepted. fn is_excluded_runtime_attribute(name: &Name, version: PythonVersion) -> bool { let name = name.as_str(); if RUNTIME_EXCLUDED_ATTRIBUTES.contains(&name) { return true; } - // CPython skips `_abc_*` when collecting runtime protocol attributes; ABCMeta - // injects those names into class dictionaries. + // ABCMeta injects `_abc_*`s into class dictionaries, but they will be ignored. if name.starts_with("_abc_") { return true; } diff --git a/pyrefly/lib/alt/special_calls.rs b/pyrefly/lib/alt/special_calls.rs index 23e517a5ff..eb58e9e8f4 100644 --- a/pyrefly/lib/alt/special_calls.rs +++ b/pyrefly/lib/alt/special_calls.rs @@ -703,9 +703,7 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> { // Unsafe overlap requires the class to be statically // incompatible with the protocol. A name outside `members` // is not part of the protocol's static contract, so it - // cannot contribute to an incompatibility. Slot-only names - // land here because the runtime member set carries - // presence, not a protocol-side type. + // cannot contribute to an incompatibility. if !class_info_protocol.members.contains(field_name) { continue; } From 84fce1bd3a7993401a480866abc940a719fe7347 Mon Sep 17 00:00:00 2001 From: amas127 Date: Fri, 13 Nov 2026 05:51:20 -0500 Subject: [PATCH 3/7] Resolve protocol slots through the MRO A subclass method that shadows an inherited `__slots__` name was reported as a data member, so `issubclass()` rejected the protocol even though CPython accepts the call. The `has_data_slots` flag was inherited by every protocol subclass, so it could not tell which class actually binds the name. CPython decides whether a member is a method with `callable(getattr(cls, name))`, so a data member is a name whose runtime value is non-callable. `is_data_protocol` now walks the MRO per runtime member: a class-body binding lets the declared read type decide, and a slot declaration that no nearer class body initializes is a data member. A body that only annotates the name creates no class attribute, so it does not shadow a slot. Members with no runtime binding are classified by their declared type, which keeps the check aligned with the type an implementation must provide; CPython would reject those calls. The flag is no longer needed and is removed. The test for a child method shadowing an inherited slot now expects no error. New tests cover a slot and a method on separate bases in both orders, an annotation-only member that still exposes the inherited slot, and the declared-type rule for a callable annotation-only member and a property whose getter returns a callable. --- pyrefly/lib/alt/class/class_metadata.rs | 8 +- pyrefly/lib/alt/special_calls.rs | 72 +++++++++++------ pyrefly/lib/alt/types/class_metadata.rs | 10 +-- pyrefly/lib/test/protocol.rs | 102 +++++++++++++++++++++++- 4 files changed, 152 insertions(+), 40 deletions(-) diff --git a/pyrefly/lib/alt/class/class_metadata.rs b/pyrefly/lib/alt/class/class_metadata.rs index 220e68c406..0b32afe42f 100644 --- a/pyrefly/lib/alt/class/class_metadata.rs +++ b/pyrefly/lib/alt/class/class_metadata.rs @@ -812,7 +812,8 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> { // 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 slot_runtime_members: SmallSet = explicit_slots + let mut members = SmallSet::new(); + let mut runtime_members: SmallSet = explicit_slots .slots_info() .map(|slots| { slots @@ -823,9 +824,6 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> { .collect() }) .unwrap_or_default(); - let has_data_slots = !slot_runtime_members.is_empty(); - let mut members = SmallSet::new(); - let mut runtime_members = slot_runtime_members; if let Some(fields) = self.get_class_fields(cls) { for name in fields.class_body_fields() { members.insert(name.clone()); @@ -837,7 +835,6 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> { Some(ProtocolMetadata { members, runtime_members, - has_data_slots, is_runtime_checkable: false, }) } else { @@ -866,7 +863,6 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> { proto .runtime_members .extend(base_proto.runtime_members.iter().cloned()); - proto.has_data_slots |= base_proto.has_data_slots; if base_proto.is_runtime_checkable { proto.is_runtime_checkable = true; } diff --git a/pyrefly/lib/alt/special_calls.rs b/pyrefly/lib/alt/special_calls.rs index eb58e9e8f4..80043216b2 100644 --- a/pyrefly/lib/alt/special_calls.rs +++ b/pyrefly/lib/alt/special_calls.rs @@ -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; @@ -796,34 +798,56 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> { } } - /// Check if a protocol is a data protocol (has non-method members) + /// Check if a protocol is a data protocol (has non-method members). + /// + /// CPython classifies a runtime member with `callable(getattr(cls, name))`, so the + /// nearest class in the MRO that binds the name to a value decides whether it is a + /// method. A class body that only annotates the name creates no class attribute, so + /// it does not shadow a slot. Attribute lookup does not model `__slots__` + /// descriptors, which are never callable, so an unshadowed slot name is data. fn is_data_protocol(&self, cls: &Class, range: TextRange) -> bool { - let metadata = self.get_metadata_for_class(cls); - if let Some(protocol_metadata) = metadata.protocol_metadata() { - // A slot name can resolve to an inherited callable attribute, so the - // member types alone cannot prove data-ness; the flag records the slot - // declaration. - if protocol_metadata.has_data_slots { - return true; - } - let class_type = self.as_class_type_unchecked(cls); - let class_type = self.heap.mk_class_type(class_type); - for field_name in &protocol_metadata.runtime_members { - 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() { + 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)); + // `ancestors_no_object` omits `cls` itself, `Protocol`, `Generic`, and + // `object`, which CPython also skips when it collects protocol members. + 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) + }) { + // The nearest binding is in a class body, so its type decides. + break; + } + if self + .get_metadata_for_class(ancestor) + .slots_info() + .is_some_and(|slots| slots.names.contains(field_name)) + { + // A slot descriptor is never callable, and attribute lookup does + // not model it, so an unshadowed slot name is a 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 } diff --git a/pyrefly/lib/alt/types/class_metadata.rs b/pyrefly/lib/alt/types/class_metadata.rs index 39a50c151c..14c6aaccb2 100644 --- a/pyrefly/lib/alt/types/class_metadata.rs +++ b/pyrefly/lib/alt/types/class_metadata.rs @@ -785,14 +785,10 @@ pub struct ProtocolMetadata { /// The runtime attribute names CPython's protocol checks inspect, used by the /// runtime `isinstance`/`issubclass` checks. This includes non-excluded slot /// names and inherited protocol members, and is version-aware; it never - /// contains the literal `__slots__`. + /// contains the literal `__slots__`. Whether a name is a method depends on which + /// class in the MRO binds it, so `is_data_protocol` decides that at the check + /// site. pub runtime_members: SmallSet, - /// Whether this protocol or an inherited protocol base declares at least one - /// `__slots__` name that CPython's runtime attribute filter keeps. An - /// unresolvable declaration does not count as data because no runtime - /// `TypeError` can be established for it. A child method that shadows an - /// inherited slot is still reported as data (a known gap). - pub has_data_slots: bool, /// Whether this protocol is decorated with @runtime_checkable pub is_runtime_checkable: bool, } diff --git a/pyrefly/lib/test/protocol.rs b/pyrefly/lib/test/protocol.rs index acbc9ff4c0..67f3595744 100644 --- a/pyrefly/lib/test/protocol.rs +++ b/pyrefly/lib/test/protocol.rs @@ -1810,9 +1810,8 @@ issubclass(Impl, Child) # E: Protocol `Child` has non-method members and cannot ); // A method in the child shadows an inherited slot at runtime, so CPython accepts -// the check. The inherited flag still marks the child as data. +// the check: `getattr` resolves to the nearer method, which is callable. testcase!( - bug = "A child method shadowing an inherited slot reports despite runtime acceptance", test_runtime_checkable_protocol_inherited_slot_shadowed_by_method, r#" from typing import Protocol, runtime_checkable @@ -1828,7 +1827,104 @@ class MethodChild(SlotBase, Protocol): class Impl: def value(self) -> None: ... -issubclass(Impl, MethodChild) # E: Protocol `MethodChild` has non-method members and cannot be used with issubclass() +issubclass(Impl, MethodChild) +"#, +); + +// An annotation-only declaration does not create a class attribute, so `getattr` +// still finds the inherited slot descriptor at runtime. +testcase!( + test_runtime_checkable_protocol_annotation_does_not_shadow_slot, + r#" +from typing import Callable, Protocol, runtime_checkable + +@runtime_checkable +class SlotBase(Protocol): + __slots__ = ("value",) + +@runtime_checkable +class AnnotatedChild(SlotBase, Protocol): + value: Callable[[], None] + +class Impl: + def value(self) -> None: ... + +issubclass(Impl, AnnotatedChild) # E: Protocol `AnnotatedChild` has non-method members and cannot be used with issubclass() +"#, +); + +// A property is read as its getter's return type, so a callable return type counts +// as a method. CPython instead sees the non-callable `property` object in the class +// body and rejects the call. Members are classified by their declared read type, +// which matches the type an implementation must provide; the same shape without +// slots behaves the same way. +testcase!( + test_runtime_checkable_protocol_property_does_not_shadow_slot, + r#" +from typing import Callable, Protocol, runtime_checkable + +@runtime_checkable +class SlotBase(Protocol): + __slots__ = ("value",) + +@runtime_checkable +class PropertyChild(SlotBase, Protocol): + @property + def value(self) -> Callable[[], None]: ... + +class Impl: + def value(self) -> None: ... + +issubclass(Impl, PropertyChild) +"#, +); + +// An annotation-only member typed callable is classified by its declared read type, +// so it counts as a method. CPython instead finds no class attribute and rejects the +// call; the checker follows the declaration. +testcase!( + test_runtime_checkable_protocol_callable_annotation_member, + r#" +from typing import Callable, Protocol, runtime_checkable + +@runtime_checkable +class P(Protocol): + __slots__ = () + f: Callable[[], None] + def m(self) -> None: ... + +class Impl: + def f(self) -> None: ... + def m(self) -> None: ... + +issubclass(Impl, P) +"#, +); + +// With the slot and the method on separate bases, the MRO decides which one +// shadows the other, matching `getattr` on the class. +testcase!( + test_runtime_checkable_protocol_slot_and_method_bases, + r#" +from typing import Protocol, runtime_checkable + +@runtime_checkable +class SlotBase(Protocol): + __slots__ = ("value",) + +@runtime_checkable +class MethodBase(Protocol): + def value(self) -> None: ... + +class MethodFirst(MethodBase, SlotBase, Protocol): ... + +class SlotFirst(SlotBase, MethodBase, Protocol): ... + +class Impl: + def value(self) -> None: ... + +issubclass(Impl, MethodFirst) +issubclass(Impl, SlotFirst) # E: Protocol `SlotFirst` has non-method members and cannot be used with issubclass() "#, ); From 07862ad49b0b11976fdf33056983a03b509071f5 Mon Sep 17 00:00:00 2001 From: amas127 Date: Fri, 13 Nov 2026 07:13:02 -0500 Subject: [PATCH 4/7] Classify protocol property members as data members CPython decides whether a protocol member is a method with `callable(getattr(cls, name))`. A `property` object is not callable, so a runtime-checkable protocol whose member is a property always rejects `issubclass()`. `is_data_protocol` classified a class-body binding by its declared read type, which for a property is the getter's return type. That accepted the call and, for a property shadowing an inherited slot, removed an error the previous rule reported. A property binding is now a data member. Other bindings, and members with no runtime binding, still follow the declared read type. Tests cover a property shadowing an inherited slot, a protocol whose only member is a property, and decorated methods shadowing an inherited slot. Two shapes where the check trusts the declaration over the runtime value keep their divergence from CPython and now carry `bug` markers. --- pyrefly/lib/alt/special_calls.rs | 23 +++++++-- pyrefly/lib/test/protocol.rs | 85 +++++++++++++++++++++++++++++--- 2 files changed, 95 insertions(+), 13 deletions(-) diff --git a/pyrefly/lib/alt/special_calls.rs b/pyrefly/lib/alt/special_calls.rs index 80043216b2..00a026c0a9 100644 --- a/pyrefly/lib/alt/special_calls.rs +++ b/pyrefly/lib/alt/special_calls.rs @@ -803,8 +803,12 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> { /// CPython classifies a runtime member with `callable(getattr(cls, name))`, so the /// nearest class in the MRO that binds the name to a value decides whether it is a /// method. A class body that only annotates the name creates no class attribute, so - /// it does not shadow a slot. Attribute lookup does not model `__slots__` - /// descriptors, which are never callable, so an unshadowed slot name is data. + /// it does not shadow a slot. A `property` object is not callable, so a property + /// binding is a data member. Any other binding, and a member with no binding at all, + /// is classified by its declared read type, which is the type an implementation must + /// provide. A slot declaration with no nearer binding is a data member because the + /// slot descriptor is never callable and attribute lookup reports the declared type + /// rather than that descriptor. fn is_data_protocol(&self, cls: &Class, range: TextRange) -> bool { let Some(protocol_metadata) = self.get_metadata_for_class(cls).protocol_metadata() else { return false; @@ -821,7 +825,15 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> { fields.class_body_fields().any(|name| name == field_name) && fields.is_field_initialized_on_class(field_name) }) { - // The nearest binding is in a class body, so its type decides. + // The nearest binding is in a class body. A `property` object is not + // callable, so it is a data member regardless of its read type. + 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 @@ -829,8 +841,9 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> { .slots_info() .is_some_and(|slots| slots.names.contains(field_name)) { - // A slot descriptor is never callable, and attribute lookup does - // not model it, so an unshadowed slot name is a data member. + // A slot descriptor is never callable, and attribute lookup reports + // the declared type rather than that descriptor, so an unshadowed + // slot name is a data member. return true; } } diff --git a/pyrefly/lib/test/protocol.rs b/pyrefly/lib/test/protocol.rs index 67f3595744..fcf08359ad 100644 --- a/pyrefly/lib/test/protocol.rs +++ b/pyrefly/lib/test/protocol.rs @@ -1853,13 +1853,10 @@ issubclass(Impl, AnnotatedChild) # E: Protocol `AnnotatedChild` has non-method "#, ); -// A property is read as its getter's return type, so a callable return type counts -// as a method. CPython instead sees the non-callable `property` object in the class -// body and rejects the call. Members are classified by their declared read type, -// which matches the type an implementation must provide; the same shape without -// slots behaves the same way. +// A `property` object is not callable, so the nearest runtime binding is a data +// member even though the property's read type is the getter's return type. testcase!( - test_runtime_checkable_protocol_property_does_not_shadow_slot, + test_runtime_checkable_protocol_property_shadows_inherited_slot, r#" from typing import Callable, Protocol, runtime_checkable @@ -1875,14 +1872,63 @@ class PropertyChild(SlotBase, Protocol): class Impl: def value(self) -> None: ... -issubclass(Impl, PropertyChild) +issubclass(Impl, PropertyChild) # E: Protocol `PropertyChild` has non-method members and cannot be used with issubclass() +"#, +); + +// A property is a data member even when no slot declares the name. +testcase!( + test_runtime_checkable_protocol_property_member, + r#" +from typing import Callable, Protocol, runtime_checkable + +@runtime_checkable +class P(Protocol): + @property + def value(self) -> Callable[[], None]: ... + +class Impl: + def value(self) -> None: ... + +issubclass(Impl, P) # E: Protocol `P` has non-method members and cannot be used with issubclass() +"#, +); + +// Decorated methods are class-body bindings whose runtime value is callable, so +// they shadow an inherited slot like a plain method does. +testcase!( + test_runtime_checkable_protocol_decorated_methods_shadow_slot, + r#" +from typing import Protocol, runtime_checkable + +@runtime_checkable +class SlotBase(Protocol): + __slots__ = ("value",) + +@runtime_checkable +class ClassMethodChild(SlotBase, Protocol): + @classmethod + def value(cls) -> None: ... + +@runtime_checkable +class StaticMethodChild(SlotBase, Protocol): + @staticmethod + def value() -> None: ... + +class Impl: + def value(self) -> None: ... + +issubclass(Impl, ClassMethodChild) +issubclass(Impl, StaticMethodChild) "#, ); // An annotation-only member typed callable is classified by its declared read type, // so it counts as a method. CPython instead finds no class attribute and rejects the -// call; the checker follows the declaration. +// call; the checker follows the declaration, which is the type an implementation +// must provide. testcase!( + bug = "CPython rejects an annotation-only member, which has no class attribute", test_runtime_checkable_protocol_callable_annotation_member, r#" from typing import Callable, Protocol, runtime_checkable @@ -1901,6 +1947,29 @@ issubclass(Impl, P) "#, ); +// A class-body value whose declared read type is callable counts as a method even +// when the value bound at runtime is not callable. +testcase!( + bug = "CPython rejects the non-callable value bound in the class body", + test_runtime_checkable_protocol_non_callable_value_with_callable_type, + r#" +from typing import Callable, Protocol, runtime_checkable, cast + +@runtime_checkable +class SlotBase(Protocol): + __slots__ = ("value",) + +@runtime_checkable +class AssignedChild(SlotBase, Protocol): + value: Callable[[], None] = cast(Callable[[], None], None) + +class Impl: + def value(self) -> None: ... + +issubclass(Impl, AssignedChild) +"#, +); + // With the slot and the method on separate bases, the MRO decides which one // shadows the other, matching `getattr` on the class. testcase!( From 69e41a507d89770f927382fe4bd017ca0dc404b6 Mon Sep 17 00:00:00 2001 From: amas127 Date: Sat, 14 Nov 2026 22:23:34 -0500 Subject: [PATCH 5/7] Check declared types of runtime-excluded protocol members The unsafe-overlap check only iterated runtime members, so it skipped members that CPython excludes from its runtime check. Keep the runtime presence check over runtime_members, then compare the declared types of the protocol members the implementation provides. A missing runtime-excluded member stays optional, while an incompatible annotation or assignment is reported. --- pyrefly/lib/alt/special_calls.rs | 79 +++++++++++++++---------- pyrefly/lib/alt/types/class_metadata.rs | 3 +- pyrefly/lib/test/protocol.rs | 29 +++++++++ 3 files changed, 79 insertions(+), 32 deletions(-) diff --git a/pyrefly/lib/alt/special_calls.rs b/pyrefly/lib/alt/special_calls.rs index 00a026c0a9..ccb8958467 100644 --- a/pyrefly/lib/alt/special_calls.rs +++ b/pyrefly/lib/alt/special_calls.rs @@ -684,45 +684,62 @@ 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. + // The runtime check only verifies presence. A class can pass + // it while declaring an incompatible type for a member that + // CPython excludes from the runtime check. Compare the types of + // the protocol members the implementation actually provides; + // runtime-excluded members are not required to exist. // // 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.runtime_members { - if !self.has_attr(object_type, field_name) { - all_members_present = false; - break; - } - // Unsafe overlap requires the class to be statically - // incompatible with the protocol. A name outside `members` - // is not part of the protocol's static contract, so it - // cannot contribute to an incompatibility. - if !class_info_protocol.members.contains(field_name) { - 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() - .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 { + // A runtime-excluded member is not a runtime + // requirement. Only compare members that the + // implementation declares or exposes; a bare + // annotation is still a static declaration, so + // `has_static_attr` covers it. + if !self.has_attr(object_type, field_name) + && !self.has_static_attr(object_type, field_name) + { + continue; + } + // These names are protocol machinery, not + // instance members. Keep this in sync with the + // structural checks in `subset.rs`. + 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() + .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) { @@ -730,7 +747,7 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> { 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, diff --git a/pyrefly/lib/alt/types/class_metadata.rs b/pyrefly/lib/alt/types/class_metadata.rs index b3a1fa78bf..5ac1d10b52 100644 --- a/pyrefly/lib/alt/types/class_metadata.rs +++ b/pyrefly/lib/alt/types/class_metadata.rs @@ -786,7 +786,8 @@ pub struct ProtocolMetadata { /// names and inherited protocol members, and is version-aware; it never /// contains the literal `__slots__`. Whether a name is a method depends on which /// class in the MRO binds it, so `is_data_protocol` decides that at the check - /// site. + /// site. The unsafe-overlap check uses this set only for presence; it compares + /// the declared types of `members` that the implementation provides. pub runtime_members: SmallSet, /// Whether this protocol is decorated with @runtime_checkable pub is_runtime_checkable: bool, diff --git a/pyrefly/lib/test/protocol.rs b/pyrefly/lib/test/protocol.rs index fcf08359ad..a5a465e7ad 100644 --- a/pyrefly/lib/test/protocol.rs +++ b/pyrefly/lib/test/protocol.rs @@ -1706,6 +1706,35 @@ issubclass(Impl, SlotNamed) "#, ); +// A runtime-excluded member is not required to exist, but when an +// implementation declares one it must still have a compatible type. +testcase!( + test_runtime_checkable_unsafe_overlap_excluded_member, + r#" +from typing import Protocol, runtime_checkable + +@runtime_checkable +class P(Protocol): + _abc_marker: int + def m(self) -> None: ... + +class AnnotatedWrong: + _abc_marker: str + def m(self) -> None: ... + +class AssignedWrong: + _abc_marker = "wrong" + def m(self) -> None: ... + +class Missing: + def m(self) -> None: ... + +isinstance(AnnotatedWrong(), P) # E: Runtime checkable protocol `P` has an unsafe overlap with type `AnnotatedWrong` +isinstance(AssignedWrong(), P) # E: Runtime checkable protocol `P` has an unsafe overlap with type `AssignedWrong` +isinstance(Missing(), P) +"#, +); + // An annotation-only `__slots__` declares no slot descriptors and no data. testcase!( test_runtime_checkable_protocol_annotated_slots, From 18b4ee677b5707db5fe7b8db3fde471d93b26bf5 Mon Sep 17 00:00:00 2001 From: amas127 Date: Sun, 15 Nov 2026 14:31:21 -0500 Subject: [PATCH 6/7] Separate runtime presence from static protocol member checks The unsafe-overlap check used runtime_members for both the presence gate and the type comparison, so a runtime-excluded member was skipped even when the implementation omitted it. Keep the presence gate on runtime_members, then check every static protocol member. A missing runtime-excluded member now reports an unsafe overlap, while a missing ordinary member still fails the presence gate first. Remove exclusions with no observed manual declaration: `_MutableMapping__marker` and the internal PEP 749 companions `__annotate_func__` and `__annotations_cache__`. Add tests for missing members on both sides of the gate and for the `__match_args__` 3.12/3.13 boundary. --- pyrefly/lib/alt/class/class_metadata.rs | 20 +++--- pyrefly/lib/alt/special_calls.rs | 47 ++++---------- pyrefly/lib/alt/types/class_metadata.rs | 10 +-- pyrefly/lib/test/protocol.rs | 82 ++++++++++++++++++++++--- 4 files changed, 99 insertions(+), 60 deletions(-) diff --git a/pyrefly/lib/alt/class/class_metadata.rs b/pyrefly/lib/alt/class/class_metadata.rs index ba07b22e4d..a075330fbb 100644 --- a/pyrefly/lib/alt/class/class_metadata.rs +++ b/pyrefly/lib/alt/class/class_metadata.rs @@ -95,10 +95,7 @@ use crate::types::literal::Lit; use crate::types::types::CalleeKind; use crate::types::types::Type; -/// Attributes excluded by `typing._get_protocol_attrs` at runtime. -/// Static membership is a separate question: see `ProtocolMetadata::members`. const RUNTIME_EXCLUDED_ATTRIBUTES: &[&str] = &[ - "_MutableMapping__marker", "_is_protocol", "_is_runtime_protocol", "__abstractmethods__", @@ -116,21 +113,30 @@ const RUNTIME_EXCLUDED_ATTRIBUTES: &[&str] = &[ "__weakref__", ]; -/// Whether CPython's `typing._get_protocol_attrs` excludes `name` from the runtime -/// attribute set of a protocol on `version`. fn is_excluded_runtime_attribute(name: &Name, version: PythonVersion) -> bool { let name = name.as_str(); if RUNTIME_EXCLUDED_ATTRIBUTES.contains(&name) { return true; } - // ABCMeta injects `_abc_*`s into class dictionaries, but they will be ignored. + // 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), - "__annotate__" | "__annotate_func__" | "__annotations_cache__" => version.at_least(3, 14), + + // 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, } } diff --git a/pyrefly/lib/alt/special_calls.rs b/pyrefly/lib/alt/special_calls.rs index ccb8958467..e4ada784c9 100644 --- a/pyrefly/lib/alt/special_calls.rs +++ b/pyrefly/lib/alt/special_calls.rs @@ -684,11 +684,9 @@ 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 - // The runtime check only verifies presence. A class can pass - // it while declaring an incompatible type for a member that - // CPython excludes from the runtime check. Compare the types of - // the protocol members the implementation actually provides; - // runtime-excluded members are not required to exist. + // 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 member checks below. @@ -705,19 +703,8 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> { let mut unsafe_overlap_errors = vec![]; if all_runtime_members_present { for field_name in &class_info_protocol.members { - // A runtime-excluded member is not a runtime - // requirement. Only compare members that the - // implementation declares or exposes; a bare - // annotation is still a static declaration, so - // `has_static_attr` covers it. - if !self.has_attr(object_type, field_name) - && !self.has_static_attr(object_type, field_name) - { - continue; - } - // These names are protocol machinery, not - // instance members. Keep this in sync with the - // structural checks in `subset.rs`. + // 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 @@ -815,24 +802,17 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> { } } - /// Check if a protocol is a data protocol (has non-method members). + /// Check if a protocol is a data protocol (has non-method members) /// - /// CPython classifies a runtime member with `callable(getattr(cls, name))`, so the - /// nearest class in the MRO that binds the name to a value decides whether it is a - /// method. A class body that only annotates the name creates no class attribute, so - /// it does not shadow a slot. A `property` object is not callable, so a property - /// binding is a data member. Any other binding, and a member with no binding at all, - /// is classified by its declared read type, which is the type an implementation must - /// provide. A slot declaration with no nearer binding is a data member because the - /// slot descriptor is never callable and attribute lookup reports the declared type - /// rather than that descriptor. + /// 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 { 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)); - // `ancestors_no_object` omits `cls` itself, `Protocol`, `Generic`, and - // `object`, which CPython also skips when it collects protocol members. + // 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 @@ -842,8 +822,7 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> { fields.class_body_fields().any(|name| name == field_name) && fields.is_field_initialized_on_class(field_name) }) { - // The nearest binding is in a class body. A `property` object is not - // callable, so it is a data member regardless of its read type. + // `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()) @@ -858,9 +837,7 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> { .slots_info() .is_some_and(|slots| slots.names.contains(field_name)) { - // A slot descriptor is never callable, and attribute lookup reports - // the declared type rather than that descriptor, so an unshadowed - // slot name is a data member. + // Slot descriptors cannot be `callable` -> data member return true; } } diff --git a/pyrefly/lib/alt/types/class_metadata.rs b/pyrefly/lib/alt/types/class_metadata.rs index 5ac1d10b52..ecbc60b680 100644 --- a/pyrefly/lib/alt/types/class_metadata.rs +++ b/pyrefly/lib/alt/types/class_metadata.rs @@ -779,15 +779,9 @@ impl VisitTrait 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, - /// The runtime attribute names CPython's protocol checks inspect, used by the - /// runtime `isinstance`/`issubclass` checks. This includes non-excluded slot - /// names and inherited protocol members, and is version-aware; it never - /// contains the literal `__slots__`. Whether a name is a method depends on which - /// class in the MRO binds it, so `is_data_protocol` decides that at the check - /// site. The unsafe-overlap check uses this set only for presence; it compares - /// the declared types of `members` that the implementation provides. + /// Attribute names CPython inspects when it checks this protocol at runtime. pub runtime_members: SmallSet, /// Whether this protocol is decorated with @runtime_checkable pub is_runtime_checkable: bool, diff --git a/pyrefly/lib/test/protocol.rs b/pyrefly/lib/test/protocol.rs index a5a465e7ad..df6bb0891a 100644 --- a/pyrefly/lib/test/protocol.rs +++ b/pyrefly/lib/test/protocol.rs @@ -1644,8 +1644,9 @@ testcase!( r#" from typing import Any, Protocol, runtime_checkable -# CPython's `typing.EXCLUDED_ATTRIBUTES` names are not protocol members even when -# the protocol declares them explicitly. +# CPython's `typing.EXCLUDED_ATTRIBUTES` names are not runtime protocol members +# even when the protocol declares them explicitly. They remain static members, so +# an implementation that lacks them is an unsafe overlap. @runtime_checkable class ProtocolWithDunders(Protocol): __doc__: str | None @@ -1658,7 +1659,7 @@ class ProtocolWithDunders(Protocol): class Impl: def m(self) -> None: ... -issubclass(Impl, ProtocolWithDunders) +issubclass(Impl, ProtocolWithDunders) # E: Runtime checkable protocol `ProtocolWithDunders` has an unsafe overlap with type `Impl` "#, ); @@ -1682,7 +1683,8 @@ issubclass(Impl, DictSlot) ); // CPython skips `_abc_*` when collecting runtime protocol members, for both -// annotations and slot names. The names remain static protocol members. +// annotations and slot names. The names remain static protocol members, so the +// annotation form is an unsafe overlap when the implementation lacks it. testcase!( test_runtime_checkable_protocol_abc_marker, r#" @@ -1701,13 +1703,13 @@ class SlotNamed(Protocol): class Impl: def m(self) -> None: ... -issubclass(Impl, Annotated) +issubclass(Impl, Annotated) # E: Runtime checkable protocol `Annotated` has an unsafe overlap with type `Impl` issubclass(Impl, SlotNamed) "#, ); -// A runtime-excluded member is not required to exist, but when an -// implementation declares one it must still have a compatible type. +// A runtime-excluded member is not required to exist, but it remains part of +// the static contract, so an implementation that lacks it is an unsafe overlap. testcase!( test_runtime_checkable_unsafe_overlap_excluded_member, r#" @@ -1731,7 +1733,26 @@ class Missing: isinstance(AnnotatedWrong(), P) # E: Runtime checkable protocol `P` has an unsafe overlap with type `AnnotatedWrong` isinstance(AssignedWrong(), P) # E: Runtime checkable protocol `P` has an unsafe overlap with type `AssignedWrong` -isinstance(Missing(), P) +isinstance(Missing(), P) # E: Runtime checkable protocol `P` has an unsafe overlap with type `Missing` +"#, +); + +// A missing ordinary runtime member fails the runtime presence check, so no +// unsafe overlap is reported even though the static contract is violated. +testcase!( + test_runtime_checkable_unsafe_overlap_missing_runtime_member, + r#" +from typing import Protocol, runtime_checkable + +@runtime_checkable +class P(Protocol): + x: int + def m(self) -> None: ... + +class MissingData: + def m(self) -> None: ... + +isinstance(MissingData(), P) "#, ); @@ -2053,6 +2074,46 @@ issubclass(Impl, AnnotationForm) # E: Protocol `AnnotationForm` has non-method "#, ); +// On 3.12 `__match_args__` is a runtime member, so its absence fails the runtime +// presence check and no unsafe overlap is reported. +testcase!( + test_runtime_checkable_unsafe_overlap_missing_match_args_3_12, + TestEnv::new_with_version(PythonVersion::new(3, 12, 0)), + r#" +from typing import Protocol, runtime_checkable + +@runtime_checkable +class P(Protocol): + __match_args__: tuple[str, ...] + def m(self) -> None: ... + +class Impl: + def m(self) -> None: ... + +isinstance(Impl(), P) +"#, +); + +// On 3.13+ `__match_args__` is runtime-excluded, so a missing one is an unsafe +// overlap against the static contract. +testcase!( + test_runtime_checkable_unsafe_overlap_missing_match_args_3_13, + TestEnv::new_with_version(PythonVersion::new(3, 13, 0)), + r#" +from typing import Protocol, runtime_checkable + +@runtime_checkable +class P(Protocol): + __match_args__: tuple[str, ...] + def m(self) -> None: ... + +class Impl: + def m(self) -> None: ... + +isinstance(Impl(), P) # E: Runtime checkable protocol `P` has an unsafe overlap with type `Impl` +"#, +); + // On 3.8 `__class_getitem__` is not excluded from the runtime attribute set. // An explicit non-callable binding shadows the inherited // `Generic.__class_getitem__` and makes the protocol a data protocol. @@ -2116,7 +2177,8 @@ issubclass(Impl, P) # E: Protocol `P` has non-method members and cannot be used "#, ); -// `__annotate__` is excluded starting in 3.14. +// `__annotate__` is excluded starting in 3.14. An implementation that lacks it +// passes the runtime check but violates the static contract. testcase!( test_runtime_checkable_excluded_annotate_3_13, TestEnv::new_with_version(PythonVersion::new(3, 13, 0)), @@ -2149,7 +2211,7 @@ class P(Protocol): class Impl: def m(self) -> None: ... -issubclass(Impl, P) +issubclass(Impl, P) # E: Runtime checkable protocol `P` has an unsafe overlap with type `Impl` "#, ); From 2eeee0266dcd3e3b4a2f6652fe56ca92c011d16a Mon Sep 17 00:00:00 2001 From: amas127 Date: Mon, 16 Nov 2026 14:05:23 -0500 Subject: [PATCH 7/7] Update protocol runtime-check test names and comments Rename tests so the API in the name matches the call in the body: several `test_issubclass_*` tests actually call `isinstance`. Correct comments that gave the wrong reason for unsafe-overlap warnings or described runtime-excluded names as static members. Fix typos in the version-gated exclusion notes. Make the private-name test observable by adding a type mismatch: pyrefly now reports the false positive on the unmangled implementation and misses the warning on the mangled one, so the bug marker covers both directions. --- pyrefly/lib/test/protocol.rs | 552 ++++++++++------------------------- 1 file changed, 160 insertions(+), 392 deletions(-) diff --git a/pyrefly/lib/test/protocol.rs b/pyrefly/lib/test/protocol.rs index df6bb0891a..2c9c81d565 100644 --- a/pyrefly/lib/test/protocol.rs +++ b/pyrefly/lib/test/protocol.rs @@ -1532,73 +1532,127 @@ issubclass(Implementation, DataOnlyProtocol) # E: Protocol `DataOnlyProtocol` ); testcase!( - test_runtime_checkable_protocol_slots, + test_issubclass_with_empty_slots, r#" from collections.abc import Buffer -from typing import Protocol, SupportsInt, runtime_checkable +from typing import SupportsInt, runtime_checkable, Protocol + +# >>> stdlib + +issubclass(int, SupportsInt) +issubclass(bytes, Buffer) + +# >>> user-defined @runtime_checkable -class BufferProtocol(Protocol): +class MyBufferProtocol(Protocol): __slots__ = () def __buffer__(self, flags: int, /) -> memoryview: ... -class BytesLike: +class MyBufferImpl: def __buffer__(self, flags: int, /) -> memoryview: ... -# An empty `__slots__` declaration is not a protocol member. -issubclass(BytesLike, BufferProtocol) -issubclass(bytes, BufferProtocol) +issubclass(MyBufferImpl, MyBufferProtocol) +"#, +); -# typeshed declares these runtime-checkable protocols with `__slots__ = ()`. -issubclass(int, SupportsInt) -issubclass(bytes, Buffer) +testcase!( + test_issubclass_with_annotated_slots, + r#" +from typing import runtime_checkable, Protocol -# The unsafe-overlap check inspects runtime members, so the wrong-typed -# `__buffer__` is reported here. -class WrongBuffer: +@runtime_checkable +class MyBufferProtocol(Protocol): + __slots__: tuple[str, ...] + + def __buffer__(self, flags: int, /) -> memoryview: ... + +class MyBufferImpl: + def __buffer__(self, flags: int, /) -> memoryview: ... + +issubclass(MyBufferImpl, MyBufferProtocol) +"#, +); + +testcase!( + test_issubclass_with_generic_and_empty_slots, + TestEnv::new_with_version(PythonVersion::new(3, 12, 0)), + r#" +from typing import Protocol, runtime_checkable + +@runtime_checkable +class P[T](Protocol): + __slots__ = () + def m(self, value: T) -> T: ... + +class Impl: + def m(self, value: int) -> int: ... + +issubclass(Impl, P) +"#, +); + +testcase!( + test_isinstance_raise_unsafe_overlap_by_wrong_typing_with_empty_slots, + r#" +from collections.abc import Buffer + +class WrongBufferImpl: def __buffer__(self, flags: int, /) -> bytes: ... -isinstance(WrongBuffer(), BufferProtocol) # E: Runtime checkable protocol `BufferProtocol` has an unsafe overlap with type `WrongBuffer` + +isinstance(WrongBufferImpl(), Buffer) # E: Runtime checkable protocol `Buffer` has an unsafe overlap with type `WrongBufferImpl` +"#, +); + +testcase!( + test_issubclass_raise_non_method_members_by_static_typed_member_with_empty_slots, + r#" +from typing import Protocol, runtime_checkable @runtime_checkable -class DataWithEmptySlots(Protocol): +class MyDataProtocol(Protocol): __slots__ = () name: str -issubclass(BytesLike, DataWithEmptySlots) # E: Protocol `DataWithEmptySlots` has non-method members and cannot be used with issubclass() +issubclass(bytes, MyDataProtocol) # E: Protocol `MyDataProtocol` has non-method members and cannot be used with issubclass() +"#, +); -# Non-empty slots declare data attributes, so the protocol is a data protocol. -@runtime_checkable -class NonEmptySlots(Protocol): - __slots__ = ("name",) +testcase!( + test_issubclass_raise_non_method_members_by_non_empty_slots, + r#" +from typing import Protocol, runtime_checkable - def __buffer__(self, flags: int, /) -> memoryview: ... +@runtime_checkable +class MyDataProtocolStr(Protocol): + __slots__ = "name" -issubclass(BytesLike, NonEmptySlots) # E: Protocol `NonEmptySlots` has non-method members and cannot be used with issubclass() +@runtime_checkable +class MyDataProtocolTuple(Protocol): + __slots__ = ("name",) -# A dict `__slots__` contributes its keys as slot names. @runtime_checkable -class DictSlots(Protocol): +class MyDataProtocolDict(Protocol): __slots__ = {"name": "doc"} - def __buffer__(self, flags: int, /) -> memoryview: ... -issubclass(BytesLike, DictSlots) # E: Protocol `DictSlots` has non-method members and cannot be used with issubclass() +issubclass(bytes, MyDataProtocolStr) # E: Protocol `MyDataProtocolStr` has non-method members and cannot be used with issubclass() +issubclass(bytes, MyDataProtocolTuple) # E: Protocol `MyDataProtocolTuple` has non-method members and cannot be used with issubclass() +issubclass(bytes, MyDataProtocolDict) # E: Protocol `MyDataProtocolDict` has non-method members and cannot be used with issubclass() "#, ); -// Unresolved `__slots__` is not treated as data. The runtime value may be -// non-empty, but static analysis cannot prove it, so the diagnostic is suppressed. testcase!( - bug = "Unresolved `__slots__` is treated as non-data, so these runtime TypeErrors are not reported", - test_runtime_checkable_protocol_unknown_slots, + bug = "Unresolved `__slots__` cannot be simply treated as non-data or data", + test_issubclass_with_unresolved_slots, r#" from typing import Protocol, runtime_checkable def get_slots() -> tuple[str, ...]: return ("name",) -SLOTS = ("name",) +SLOTS: tuple[str, ...] = ("name",) @runtime_checkable class UnknownSlots(Protocol): @@ -1620,33 +1674,14 @@ issubclass(Impl, ConstantSlots) "#, ); -// A string `__slots__` declares one slot. testcase!( - test_runtime_checkable_protocol_string_slots, + test_issubclass_runtime_excluded_attributes, r#" -from typing import Protocol, runtime_checkable - -@runtime_checkable -class OneStringSlot(Protocol): - __slots__ = "name" - - def m(self) -> None: ... - -class Impl: - def m(self) -> None: ... - -issubclass(Impl, OneStringSlot) # E: Protocol `OneStringSlot` has non-method members and cannot be used with issubclass() -"#, -); +# `typing.EXCLUDED_ATTRIBUTES` names are excluded from runtime protocol members even when the +# protocol declares them explicitly. -testcase!( - test_runtime_checkable_protocol_excluded_attributes, - r#" from typing import Any, Protocol, runtime_checkable -# CPython's `typing.EXCLUDED_ATTRIBUTES` names are not runtime protocol members -# even when the protocol declares them explicitly. They remain static members, so -# an implementation that lacks them is an unsafe overlap. @runtime_checkable class ProtocolWithDunders(Protocol): __doc__: str | None @@ -1656,38 +1691,32 @@ class ProtocolWithDunders(Protocol): def m(self) -> None: ... -class Impl: - def m(self) -> None: ... - -issubclass(Impl, ProtocolWithDunders) # E: Runtime checkable protocol `ProtocolWithDunders` has an unsafe overlap with type `Impl` -"#, -); - -// CPython filters slot descriptor names through EXCLUDED_ATTRIBUTES before -// deciding whether a protocol has non-method members. -testcase!( - test_runtime_checkable_protocol_slots_excluded_names, - r#" -from typing import Protocol, runtime_checkable @runtime_checkable class DictSlot(Protocol): __slots__ = ("__dict__",) def m(self) -> None: ... + class Impl: def m(self) -> None: ... + +# Runtime-excluded names remain static protocol members. The runtime check ignores +# them, but an implementation that lacks them is still an unsafe overlap. +issubclass(Impl, ProtocolWithDunders) # E: Runtime checkable protocol `ProtocolWithDunders` has an unsafe overlap with type `Impl` + +# `__dict__` is runtime-excluded and has no static member declaration, so there +# is nothing to compare and no unsafe overlap is reported. issubclass(Impl, DictSlot) "#, ); -// CPython skips `_abc_*` when collecting runtime protocol members, for both -// annotations and slot names. The names remain static protocol members, so the -// annotation form is an unsafe overlap when the implementation lacks it. testcase!( - test_runtime_checkable_protocol_abc_marker, + test_issubclass_skips_abc_marker_at_runtime, r#" +# CPython skips `_abc_*` when collecting runtime protocol members. + from typing import Protocol, runtime_checkable @runtime_checkable @@ -1708,10 +1737,8 @@ issubclass(Impl, SlotNamed) "#, ); -// A runtime-excluded member is not required to exist, but it remains part of -// the static contract, so an implementation that lacks it is an unsafe overlap. testcase!( - test_runtime_checkable_unsafe_overlap_excluded_member, + test_isinstance_raise_unsafe_overlap_by_wrong_typing_with_abc_marker, r#" from typing import Protocol, runtime_checkable @@ -1737,111 +1764,11 @@ isinstance(Missing(), P) # E: Runtime checkable protocol `P` has an unsafe over "#, ); -// A missing ordinary runtime member fails the runtime presence check, so no -// unsafe overlap is reported even though the static contract is violated. testcase!( - test_runtime_checkable_unsafe_overlap_missing_runtime_member, + test_issubclass_with_child_slots_shadowing_inherited_method, r#" -from typing import Protocol, runtime_checkable - -@runtime_checkable -class P(Protocol): - x: int - def m(self) -> None: ... - -class MissingData: - def m(self) -> None: ... - -isinstance(MissingData(), P) -"#, -); - -// An annotation-only `__slots__` declares no slot descriptors and no data. -testcase!( - test_runtime_checkable_protocol_annotated_slots, - r#" -from typing import Protocol, runtime_checkable - -@runtime_checkable -class AnnotatedOnly(Protocol): - __slots__: tuple[str, ...] - def m(self) -> None: ... - -class Impl: - def m(self) -> None: ... - -issubclass(Impl, AnnotatedOnly) -"#, -); - -// A PEP 695 generic protocol with empty slots is not a data protocol. -testcase!( - test_runtime_checkable_protocol_generic_empty_slots, - TestEnv::new_with_version(PythonVersion::new(3, 12, 0)), - r#" -from typing import Protocol, runtime_checkable - -@runtime_checkable -class P[T](Protocol): - __slots__ = () - def m(self, value: T) -> T: ... - -class Impl: - def m(self, value: int) -> int: ... - -issubclass(Impl, P) -"#, -); - -// Data slots inherited from a base protocol still make the child a data protocol; -// excluded slot names do not. -testcase!( - test_runtime_checkable_protocol_slots_inheritance, - r#" -from typing import Protocol, runtime_checkable - -@runtime_checkable -class DataBase(Protocol): - __slots__ = ("value",) - def m(self) -> None: ... - -@runtime_checkable -class DataChild(DataBase, Protocol): - __slots__ = () - def m(self) -> None: ... - -@runtime_checkable -class ExcludedBase(Protocol): - __slots__ = ("__weakref__",) - def m(self) -> None: ... - -@runtime_checkable -class EmptyChild(ExcludedBase, Protocol): - __slots__ = () - def m(self) -> None: ... +# A slot in the child shadows an inherited method at runtime. -class Impl: - def m(self) -> None: ... - -# A slotted implementation exercises the implementation-side `__slots__` -# attribute, which is not a protocol member. -class SlottedImpl: - __slots__ = ("value", "__weakref__") - def m(self) -> None: ... - -issubclass(Impl, DataChild) # E: Protocol `DataChild` has non-method members and cannot be used with issubclass() -issubclass(Impl, EmptyChild) -issubclass(SlottedImpl, DataChild) # E: Protocol `DataChild` has non-method members and cannot be used with issubclass() -issubclass(SlottedImpl, EmptyChild) -"#, -); - -// A slot in the child shadows an inherited method at runtime. The member type -// alone resolves to the inherited callable, so the slot declaration must mark -// the child as a data protocol. -testcase!( - test_runtime_checkable_protocol_child_slot_shadows_inherited_method, - r#" from typing import Protocol, runtime_checkable @runtime_checkable @@ -1859,11 +1786,11 @@ issubclass(Impl, Child) # E: Protocol `Child` has non-method members and cannot "#, ); -// A method in the child shadows an inherited slot at runtime, so CPython accepts -// the check: `getattr` resolves to the nearer method, which is callable. testcase!( - test_runtime_checkable_protocol_inherited_slot_shadowed_by_method, + test_issubclass_with_child_method_shadowing_inherited_slots, r#" +# A method in the child shadows an inherited slot at runtime. + from typing import Protocol, runtime_checkable @runtime_checkable @@ -1881,11 +1808,12 @@ issubclass(Impl, MethodChild) "#, ); -// An annotation-only declaration does not create a class attribute, so `getattr` -// still finds the inherited slot descriptor at runtime. testcase!( - test_runtime_checkable_protocol_annotation_does_not_shadow_slot, + test_issubclass_with_annotation_never_shadowing_slots, r#" +# An annotation-only declaration does not create a class attribute, so `getattr` +# still finds the inherited slot descriptor at runtime. + from typing import Callable, Protocol, runtime_checkable @runtime_checkable @@ -1903,32 +1831,33 @@ issubclass(Impl, AnnotatedChild) # E: Protocol `AnnotatedChild` has non-method "#, ); -// A `property` object is not callable, so the nearest runtime binding is a data -// member even though the property's read type is the getter's return type. testcase!( - test_runtime_checkable_protocol_property_shadows_inherited_slot, + test_issubclass_with_shadowing_by_mro, r#" -from typing import Callable, Protocol, runtime_checkable +from typing import Protocol, runtime_checkable @runtime_checkable class SlotBase(Protocol): __slots__ = ("value",) @runtime_checkable -class PropertyChild(SlotBase, Protocol): - @property - def value(self) -> Callable[[], None]: ... +class MethodBase(Protocol): + def value(self) -> None: ... + +class MethodFirst(MethodBase, SlotBase, Protocol): ... + +class SlotFirst(SlotBase, MethodBase, Protocol): ... class Impl: def value(self) -> None: ... -issubclass(Impl, PropertyChild) # E: Protocol `PropertyChild` has non-method members and cannot be used with issubclass() +issubclass(Impl, MethodFirst) +issubclass(Impl, SlotFirst) # E: Protocol `SlotFirst` has non-method members and cannot be used with issubclass() "#, ); -// A property is a data member even when no slot declares the name. testcase!( - test_runtime_checkable_protocol_property_member, + test_issubclass_property_member_is_data_member, r#" from typing import Callable, Protocol, runtime_checkable @@ -1944,24 +1873,18 @@ issubclass(Impl, P) # E: Protocol `P` has non-method members and cannot be used "#, ); -// Decorated methods are class-body bindings whose runtime value is callable, so -// they shadow an inherited slot like a plain method does. testcase!( - test_runtime_checkable_protocol_decorated_methods_shadow_slot, + test_issubclass_decorated_method_is_non_data_member, r#" from typing import Protocol, runtime_checkable @runtime_checkable -class SlotBase(Protocol): - __slots__ = ("value",) - -@runtime_checkable -class ClassMethodChild(SlotBase, Protocol): +class ClassMethodChild(Protocol): @classmethod def value(cls) -> None: ... @runtime_checkable -class StaticMethodChild(SlotBase, Protocol): +class StaticMethodChild(Protocol): @staticmethod def value() -> None: ... @@ -1973,13 +1896,9 @@ issubclass(Impl, StaticMethodChild) "#, ); -// An annotation-only member typed callable is classified by its declared read type, -// so it counts as a method. CPython instead finds no class attribute and rejects the -// call; the checker follows the declaration, which is the type an implementation -// must provide. testcase!( - bug = "CPython rejects an annotation-only member, which has no class attribute", - test_runtime_checkable_protocol_callable_annotation_member, + bug = "CPython rejects an annotation-only member as callable at runtime", + test_issubclass_callable_annotation_member_considered_as_non_data_member, r#" from typing import Callable, Protocol, runtime_checkable @@ -1997,89 +1916,12 @@ issubclass(Impl, P) "#, ); -// A class-body value whose declared read type is callable counts as a method even -// when the value bound at runtime is not callable. -testcase!( - bug = "CPython rejects the non-callable value bound in the class body", - test_runtime_checkable_protocol_non_callable_value_with_callable_type, - r#" -from typing import Callable, Protocol, runtime_checkable, cast - -@runtime_checkable -class SlotBase(Protocol): - __slots__ = ("value",) - -@runtime_checkable -class AssignedChild(SlotBase, Protocol): - value: Callable[[], None] = cast(Callable[[], None], None) - -class Impl: - def value(self) -> None: ... - -issubclass(Impl, AssignedChild) -"#, -); - -// With the slot and the method on separate bases, the MRO decides which one -// shadows the other, matching `getattr` on the class. -testcase!( - test_runtime_checkable_protocol_slot_and_method_bases, - r#" -from typing import Protocol, runtime_checkable - -@runtime_checkable -class SlotBase(Protocol): - __slots__ = ("value",) - -@runtime_checkable -class MethodBase(Protocol): - def value(self) -> None: ... - -class MethodFirst(MethodBase, SlotBase, Protocol): ... - -class SlotFirst(SlotBase, MethodBase, Protocol): ... - -class Impl: - def value(self) -> None: ... - -issubclass(Impl, MethodFirst) -issubclass(Impl, SlotFirst) # E: Protocol `SlotFirst` has non-method members and cannot be used with issubclass() -"#, -); - -// `__match_args__` is a real protocol member on 3.12 and excluded on 3.13+. -// The slot and annotation forms share the same runtime predicate; the 3.13 side -// is covered by `test_runtime_checkable_protocol_excluded_attributes`. testcase!( - test_runtime_checkable_excluded_match_args_3_12, + test_issubclass_with_match_args_3_12, TestEnv::new_with_version(PythonVersion::new(3, 12, 0)), r#" -from typing import Protocol, runtime_checkable - -@runtime_checkable -class SlotForm(Protocol): - __slots__ = ("__match_args__",) - def m(self) -> None: ... - -@runtime_checkable -class AnnotationForm(Protocol): - __match_args__: tuple[str, ...] - def m(self) -> None: ... +# `__match_args__` is a real protocol member on 3.12 -class Impl: - def m(self) -> None: ... - -issubclass(Impl, SlotForm) # E: Protocol `SlotForm` has non-method members and cannot be used with issubclass() -issubclass(Impl, AnnotationForm) # E: Protocol `AnnotationForm` has non-method members and cannot be used with issubclass() -"#, -); - -// On 3.12 `__match_args__` is a runtime member, so its absence fails the runtime -// presence check and no unsafe overlap is reported. -testcase!( - test_runtime_checkable_unsafe_overlap_missing_match_args_3_12, - TestEnv::new_with_version(PythonVersion::new(3, 12, 0)), - r#" from typing import Protocol, runtime_checkable @runtime_checkable @@ -2090,16 +1932,16 @@ class P(Protocol): class Impl: def m(self) -> None: ... -isinstance(Impl(), P) +issubclass(Impl, P) # E: Protocol `P` has non-method members and cannot be used with issubclass() "#, ); -// On 3.13+ `__match_args__` is runtime-excluded, so a missing one is an unsafe -// overlap against the static contract. testcase!( - test_runtime_checkable_unsafe_overlap_missing_match_args_3_13, + test_isinstance_excludes_match_args_3_13, TestEnv::new_with_version(PythonVersion::new(3, 13, 0)), r#" +# `__match_args__` is an excluded attribute name on 3.13+ + from typing import Protocol, runtime_checkable @runtime_checkable @@ -2114,18 +1956,17 @@ isinstance(Impl(), P) # E: Runtime checkable protocol `P` has an unsafe overlap "#, ); -// On 3.8 `__class_getitem__` is not excluded from the runtime attribute set. -// An explicit non-callable binding shadows the inherited -// `Generic.__class_getitem__` and makes the protocol a data protocol. testcase!( - test_runtime_checkable_class_getitem_3_8_assignment, + test_issubclass_with_class_getitem_3_8, TestEnv::new_with_version(PythonVersion::new(3, 8, 0)), r#" +# `__class_getitem__` is a real protocol member on 3.8 + from typing import Protocol, runtime_checkable @runtime_checkable class P(Protocol): - __class_getitem__ = 1 + __class_getitem__ = None def m(self) -> None: ... class Impl: @@ -2135,54 +1976,35 @@ issubclass(Impl, P) # E: Protocol `P` has non-method members and cannot be used "#, ); -// On 3.9+, CPython excludes the name, so the same assignment does not report. testcase!( - test_runtime_checkable_excluded_class_getitem_3_9, + test_issubclass_excludes_class_getitem_3_9, TestEnv::new_with_version(PythonVersion::new(3, 9, 0)), r#" -from typing import Protocol, runtime_checkable - -@runtime_checkable -class P(Protocol): - __class_getitem__ = 1 - def m(self) -> None: ... - -class Impl: - def m(self) -> None: ... +# `__class_getitem__` is an excluded attribute name on 3.9+ -issubclass(Impl, P) -"#, -); - -// On 3.8 `__class_getitem__` is not reserved, so an annotation-only declaration -// is a protocol data member under the declaration-based model. CPython resolves -// the inherited `Generic.__class_getitem__` and accepts it, so this is a -// deliberate false positive. -testcase!( - bug = "3.8 annotation-only form reports even though CPython resolves the inherited method", - test_runtime_checkable_protocol_annotated_class_getitem_3_8, - TestEnv::new_with_version(PythonVersion::new(3, 8, 0)), - r#" from typing import Protocol, runtime_checkable @runtime_checkable class P(Protocol): - __class_getitem__: object + __class_getitem__: None def m(self) -> None: ... class Impl: def m(self) -> None: ... -issubclass(Impl, P) # E: Protocol `P` has non-method members and cannot be used with issubclass() +# `__class_getitem__` is runtime-excluded on 3.9+, and pyrefly also skips it +# in the unsafe-overlap type check. +issubclass(Impl, P) "#, ); -// `__annotate__` is excluded starting in 3.14. An implementation that lacks it -// passes the runtime check but violates the static contract. testcase!( - test_runtime_checkable_excluded_annotate_3_13, + test_issubclass_with_annotate_3_13, TestEnv::new_with_version(PythonVersion::new(3, 13, 0)), r#" +# `__annotate__` is not runtime-excluded on 3.13, so an explicit annotation is a +# non-method member and `issubclass()` rejects the protocol. + from typing import Protocol, runtime_checkable @runtime_checkable @@ -2198,9 +2020,11 @@ issubclass(Impl, P) # E: Protocol `P` has non-method members and cannot be used ); testcase!( - test_runtime_checkable_excluded_annotate_3_14, + test_issubclass_excludes_annotate_3_14, TestEnv::new_with_version(PythonVersion::new(3, 14, 0)), r#" +# `__annotate__` is an excluded attribute name on 3.14+ + from typing import Protocol, runtime_checkable @runtime_checkable @@ -2215,10 +2039,8 @@ issubclass(Impl, P) # E: Runtime checkable protocol `P` has an unsafe overlap w "#, ); -// Slot names participate in the runtime member-presence check, while real data -// members are still type-checked when they are present. testcase!( - test_runtime_checkable_unsafe_overlap_nonempty_slots, + test_isinstance_raise_unsafe_overlap_by_retyping_slot_attributes_and_misaligning_impl, r#" from typing import Protocol, runtime_checkable @@ -2229,85 +2051,31 @@ class P(Protocol): def m(self) -> int: ... class Mismatched: - name: str = "x" - def m(self) -> int: ... - -isinstance(Mismatched(), P) # E: Runtime checkable protocol `P` has an unsafe overlap with type `Mismatched` - -class DifferentSlots: - __slots__ = ("x", "name") - def m(self) -> int: ... - -isinstance(DifferentSlots(), P) - -class DifferentSlotsNoName: - __slots__ = ("x",) - def m(self) -> int: ... - -isinstance(DifferentSlotsNoName(), P) - -@runtime_checkable -class SlotOnly(Protocol): - __slots__ = ("name",) - def m(self) -> int: ... - -class MissingSlotBadMethod: - def m(self) -> str: ... - -isinstance(MissingSlotBadMethod(), SlotOnly) -"#, -); - -// A slot-only name participates in the presence check but has no -// protocol-side type, so it must not be compared; other members still are. -testcase!( - test_runtime_checkable_unsafe_overlap_slot_only_members, - r#" -from typing import Protocol, runtime_checkable - -@runtime_checkable -class SlotOnly(Protocol): - __slots__ = ("name",) - def m(self) -> int: ... - -class Annotated: name: str def m(self) -> int: ... -class Assigned: - name: int = 1 - def m(self) -> int: ... - -isinstance(Annotated(), SlotOnly) -isinstance(Assigned(), SlotOnly) - -class BadMethod: - name: int = 1 - def m(self) -> str: ... - -isinstance(BadMethod(), SlotOnly) # E: Runtime checkable protocol `SlotOnly` has an unsafe overlap with type `BadMethod` +isinstance(Mismatched(), P) # E: Runtime checkable protocol `P` has an unsafe overlap with type `Mismatched` "#, ); -// Private slot names are mangled at runtime, but the runtime member set stores -// the literal name. The presence check therefore looks for `__x` and misses -// `_P__x`, which suppresses the unsafe-overlap diagnostic. testcase!( - bug = "Private slot names are not mangled, so the presence check misses `_P__x`", - test_runtime_checkable_protocol_private_slot_name, + bug = "Private attribute names are not mangled, so both presence and type check use the wrong name", + test_isinstance_with_private_attribute_name, r#" from typing import Protocol, runtime_checkable @runtime_checkable class P(Protocol): - __slots__ = ("__x",) - def m(self) -> None: ... + __x: int -class Impl: - __slots__ = ("_P__x",) - def m(self) -> str: ... +class WrongImpl: + __x = None + +class WorkableImpl: + _P__x = None -isinstance(Impl(), P) +isinstance(WrongImpl(), P) # E: Runtime checkable protocol `P` has an unsafe overlap with type `WrongImpl` +isinstance(WorkableImpl(), P) "#, );