Skip to content

Conversation

AlexWaygood
Copy link
Member

Summary

As well as excluding a hardcoded set of special attributes, CPython at runtime also excludes any attributes or declarations starting with _abc_ from the set of members that make up a protocol interface. I missed this in my initial implementation.

This is a bit of a CPython implementation detail, but I do think it's important that we try to model the runtime as best we can here. The closer we are to the runtime behaviour, the closer we come to sound behaviour when narrowing types from isinstance() checks against runtime-checkable protocols (for example)

Test Plan

Extended an existing mdtest

@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Jun 4, 2025
@AlexWaygood AlexWaygood requested a review from dcreager as a code owner June 4, 2025 18:30
Copy link
Contributor

github-actions bot commented Jun 4, 2025

mypy_primer results

No ecosystem changes detected ✅

Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neither mypy nor pyright implement this, and I'm not sure how much the static typing understanding of protocols should be built around implementation details of runtime-checkable protocols.

But I guess this is not likely to hurt compatibility, since such attributes should be unusual. So it seems fine to do this and re-evaluate if it causes any actual issues.

@AlexWaygood AlexWaygood merged commit 3a81915 into main Jun 4, 2025
35 checks passed
@AlexWaygood AlexWaygood deleted the alex/protocol-members-abc branch June 4, 2025 19:34
carljm added a commit to mtshiba/ruff that referenced this pull request Jun 4, 2025
* main:
  [ty] Only consider a type `T` a subtype of a protocol `P` if all of `P`'s members are fully bound on `T` (astral-sh#18466)
  [ty] Exclude members starting with `_abc_` from a protocol interface (astral-sh#18467)
  [ty] Add subdiagnostic suggestion to `unresolved-reference` diagnostic when variable exists on `self` (astral-sh#18444)
  [ty] IDE: only provide declarations and bindings as completions (astral-sh#18456)
  [ty] ty_ide: Hotfix for `expression_scope_id` panics (astral-sh#18455)
  [ty] Improve docs for Class{Literal,Type}::instance_member (astral-sh#18454)
  [ty] Add meta-type tests for legavy TypeVars (astral-sh#18453)
  update to salsa that doesn't panic silently on cycles (astral-sh#18450)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ty Multi-file analysis & type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants