Skip to content

[ty] Retain receiver parameters in protocol access - #28822

Merged
sharkdp merged 11 commits into
mainfrom
codex/protocol-member-refactor
Sep 24, 2026
Merged

sharkdp merged 11 commits into
mainfrom
codex/protocol-member-refactor

Conversation

@sharkdp

@sharkdp sharkdp commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR is a refactoring that allows us to keep track of the declared receiver type for protocol members. For example, consider this protocol:

from typing import Protocol

class Copyable(Protocol):
    def copy[T](self: T) -> T: ...

Previously, when accessing the read/write access properties of the protocol member, we would eagerly bind the self parameter, losing track of the self: T annotation which is necessary to properly resolve calls to this method. With this PR, we instead retain a reference to the full generic signature [T](self: T) -> T.

I made various improvements to the read/write access API, but didn't attempt to solve all problems that I saw. Also added some TODO comments.

This refactoring is in preparation for fixes to astral-sh/ty#4544 and astral-sh/ty#4075.

@astral-sh-bot

astral-sh-bot Bot commented Sep 23, 2026

Copy link
Copy Markdown

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 98.15%. The percentage of expected errors that received a diagnostic held steady at 98.07%. The number of fully passing files held steady at 133/146.

@sharkdp
sharkdp added this pull request to stack #28823 September 23, 2026 12:05
@astral-sh-bot

astral-sh-bot Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Memory usage report

Summary

Project Old New Diff Outcome
flake8 38.22MB 38.22MB -0.00% (1.80kB) ⬇️
sphinx 167.57MB 167.56MB -0.01% (10.47kB) ⬇️
trio 93.14MB 93.11MB -0.03% (30.09kB) ⬇️
prefect 466.65MB 466.55MB -0.02% (96.72kB) ⬇️

Significant changes

Click to expand detailed breakdown

flake8

Name Old New Diff Outcome
CallableType 353.23kB 352.38kB -0.24% (864.00B) ⬇️
Type<'db>::cached_materialization_::interned_arguments 9.84kB 9.61kB -2.38% (240.00B) ⬇️
MaterializedProtocolType 936.00B 720.00B -23.08% (216.00B) ⬇️
TypeVarInstance 39.28kB 39.09kB -0.48% (192.00B) ⬇️
BoundTypeVarInstance 83.27kB 83.10kB -0.21% (176.00B) ⬇️
Type<'db>::cached_materialization_ 6.95kB 6.80kB -2.02% (144.00B) ⬇️
is_redundant_with_impl 86.72kB 86.70kB -0.02% (16.00B) ⬇️

sphinx

Name Old New Diff Outcome
CallableType 2.13MB 2.12MB -0.22% (4.80kB) ⬇️
Type<'db>::cached_materialization_::interned_arguments 78.59kB 77.11kB -1.89% (1.48kB) ⬇️
BoundTypeVarInstance 792.17kB 791.23kB -0.12% (968.00B) ⬇️
Type<'db>::cached_materialization_ 54.27kB 53.38kB -1.64% (912.00B) ⬇️
TypeVarInstance 276.84kB 276.00kB -0.30% (864.00B) ⬇️
MaterializedProtocolType 4.99kB 4.29kB -14.08% (720.00B) ⬇️
member_lookup_with_policy_and_receiver_inner 1.69MB 1.69MB -0.02% (272.00B) ⬇️
member_lookup_with_policy_inner 3.66MB 3.66MB -0.00% (168.00B) ⬇️
simplify_intersection_pair_impl 231.27kB 231.17kB -0.04% (96.00B) ⬇️
is_redundant_with_impl 665.31kB 665.25kB -0.01% (64.00B) ⬇️
infer_expression_types_impl 11.59MB 11.59MB -0.00% (56.00B) ⬇️
infer_definition_types 11.54MB 11.54MB -0.00% (40.00B) ⬇️
infer_scope_types_impl 6.16MB 6.16MB -0.00% (32.00B) ⬇️
Type<'db>::class_member_with_policy_inner_ 3.30MB 3.30MB -0.00% (16.00B) ⬇️
intersection_from_two_elements 202.98kB 202.97kB -0.01% (16.00B) ⬇️
... 5 more

trio

Name Old New Diff Outcome
CallableType 1.69MB 1.68MB -1.00% (17.31kB) ⬇️
Type<'db>::cached_materialization_::interned_arguments 91.41kB 86.64kB -5.21% (4.77kB) ⬇️
Type<'db>::cached_materialization_ 67.55kB 64.55kB -4.44% (3.00kB) ⬇️
BoundTypeVarInstance 330.34kB 328.62kB -0.52% (1.72kB) ⬇️
TypeVarInstance 148.88kB 147.66kB -0.82% (1.22kB) ⬇️
MaterializedProtocolType 7.66kB 6.82kB -11.01% (864.00B) ⬇️
infer_definition_types 4.66MB 4.66MB -0.01% (384.00B) ⬇️
infer_expression_types_impl 6.08MB 6.08MB -0.01% (320.00B) ⬇️
Specialization 1.05MB 1.05MB -0.02% (240.00B) ⬇️
GenericAlias 371.60kB 371.46kB -0.04% (144.00B) ⬇️
protocol_bind_self::interned_arguments 36.09kB 36.01kB -0.24% (88.00B) ⬇️
protocol_bind_self 16.41kB 16.37kB -0.24% (40.00B) ⬇️
infer_scope_types_impl 2.82MB 2.82MB -0.00% (16.00B) ⬇️
try_call_dunder_get_inner 104.96kB 104.95kB -0.01% (16.00B) ⬇️
Type<'db>::apply_specialization_inner_ 652.50kB 652.49kB -0.00% (8.00B) ⬇️
... 1 more

prefect

Name Old New Diff Outcome
CallableType 8.98MB 8.92MB -0.64% (58.57kB) ⬇️
Type<'db>::cached_materialization_::interned_arguments 394.38kB 382.34kB -3.05% (12.03kB) ⬇️
Type<'db>::cached_materialization_ 332.14kB 324.27kB -2.37% (7.88kB) ⬇️
infer_definition_types 37.06MB 37.06MB -0.01% (4.93kB) ⬇️
protocol_bind_self::interned_arguments 145.66kB 142.66kB -2.06% (3.01kB) ⬇️
BoundTypeVarInstance 1.60MB 1.60MB -0.16% (2.58kB) ⬇️
TypeVarInstance 648.28kB 646.50kB -0.27% (1.78kB) ⬇️
protocol_bind_self 66.21kB 64.84kB -2.06% (1.37kB) ⬇️
MaterializedProtocolType 23.84kB 22.57kB -5.31% (1.27kB) ⬇️
is_redundant_with_impl 3.07MB 3.07MB -0.02% (760.00B) ⬇️
Specialization 4.69MB 4.69MB -0.01% (528.00B) ⬇️
when_constraint_set_assignable_to_owned_impl 3.53MB 3.53MB -0.01% (376.00B) ⬇️
GenericAlias 1.71MB 1.71MB -0.02% (288.00B) ⬇️
Type<'db>::apply_specialization_inner_ 2.83MB 2.83MB -0.01% (232.00B) ⬇️
infer_expression_types_impl 23.85MB 23.85MB -0.00% (208.00B) ⬇️
... 18 more

@astral-sh-bot

astral-sh-bot Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Full report with detailed diff (timing results)

@codspeed

codspeed Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 153 untouched benchmarks
⏩ 60 skipped benchmarks1


Comparing codex/protocol-member-refactor (c1e5e3d) with main (eb7d7da)

Open in CodSpeed

Footnotes

  1. 60 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@sharkdp sharkdp changed the title [ty] Separate protocol declarations from member access [ty] Retain receiver parameters in protocol access Sep 23, 2026
ProtocolMemberKind::Method(member, kind) => {
let instance_method = match (member.ty(), kind) {
(Type::Callable(callable), ProtocolMethodKind::Instance) => member.with_ty(
Type::Callable(protocol_bind_self(db, env.program(db), callable, None)),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is the eager receiver binding that we avoid with the new API.

Comment on lines +718 to +722
let runtime_type = if kind == ProtocolMethodKind::Class {
receiver_ty.to_meta_type(db, env)
} else {
receiver_ty
};

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In the former version, classmethods already had their cls receiver removed (and instance methods had self removed), so this didn't need to be computed.

@sharkdp
sharkdp marked this pull request as ready for review September 23, 2026 20:22
@sharkdp
sharkdp requested a review from a team as a code owner September 23, 2026 20:22
@astral-sh-bot
astral-sh-bot Bot requested a review from dhruvmanila September 23, 2026 20:22
@sharkdp
sharkdp merged commit b538efe into main Sep 24, 2026
122 of 126 checks passed
@sharkdp
sharkdp deleted the codex/protocol-member-refactor branch September 24, 2026 10:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty The ty type checker

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants