Skip to content

[ty] Preserve protocol method signatures until receiver binding - #28671

Draft
sharkdp wants to merge 9 commits into
mainfrom
codex/protocol-member-receivers
Draft

sharkdp wants to merge 9 commits into
mainfrom
codex/protocol-member-receivers

Conversation

@sharkdp

@sharkdp sharkdp commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Protocol methods can restrict which receivers they accept, and classmethods can use Self to describe the instance type associated with their receiver. Binding these methods before their receiver is known loses information needed for compatibility checks and member lookup.

Retain complete method signatures and bind each overload to the receivers permitted by the protocol. Resolve Self using the actual class-object receiver during member lookup, and materialize types after binding. Stored bound methods retain their captured receivers.

This accepts implementations with matching receiver restrictions and preserves intersection types when binding Self in protocol classmethods.

Depends on #28822, which separates protocol declarations from instance and class access.

Fixes astral-sh/ty#4544.
Fixes astral-sh/ty#4075.

Validation

  • Regression coverage for restricted instance methods, classmethods, callback protocols, overloads, stored bound methods, and generic receivers.
  • All 959 semantic tests pass; 34 skipped.
  • Workspace Clippy with all targets and features passes.
  • Formatting and lint hooks pass.

The conformance comparison still has three additional false positives in protocols_generic.py and protocols_self.py, including with python/typing#2363 (8f9607ec). Passing files decrease from 131/146 to 130/146. These compatibility failures involving type variables in receiver annotations remain unresolved.

@sharkdp sharkdp added the ty The ty type checker label Sep 17, 2026
@astral-sh-bot

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

Copy link
Copy Markdown

Typing conformance results regressed ❌

The percentage of diagnostics emitted that were expected errors decreased from 98.24% to 97.98%. The percentage of expected errors that received a diagnostic held steady at 98.24%. The number of fully passing files regressed from 134/146 to 133/146.

Summary

How are test cases classified?

Each test case represents one expected error annotation or a group of annotations sharing a tag. Counts are per test case, not per diagnostic — multiple diagnostics on the same line count as one. Required annotations (E) are true positives when ty flags the expected location and false negatives when it does not. Optional annotations (E?) are true positives when flagged but true negatives (not false negatives) when not. Tagged annotations (E[tag]) require ty to flag exactly one of the tagged lines; tagged multi-annotations (E[tag+]) allow any number up to the tag count. Flagging unexpected locations counts as a false positive.

Metric Old New Diff Outcome
True Positives 1117 1117 +0
False Positives 20 23 +3 ⏫ (❌)
False Negatives 20 20 +0
Total Diagnostics 1181 1184 +3 ⏫
Precision 98.24% 97.98% -0.26% ⏬ (❌)
Recall 98.24% 98.24% +0.00%
Passing Files 134/146 133/146 -1 ⏬ (❌)

Test file breakdown

2 files altered
File True Positives False Positives False Negatives Status
protocols_generic.py 8 2 (+2) ❌ 1 📉 Regressing
protocols_self.py 0 1 (+1) ❌ 0 ❌ Newly Failing ☹️
Total (all files) 1117 23 (+3) ❌ 20 133/146

False positives added (3)

3 diagnostics
Test case Diff

protocols_generic.py:106

+error[invalid-argument-type] Argument to function `generic_get_parent` is incorrect: Argument type `ConcreteHasParent` does not satisfy upper bound `HasParent` of type variable `GenericHasParent`

protocols_generic.py:107

+error[type-assertion-failure] Type `Unknown` does not match asserted type `ConcreteHasParent`

protocols_self.py:32

+error[invalid-assignment] Object of type `One` is not assignable to `Copyable`

@astral-sh-bot

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

Copy link
Copy Markdown

Memory usage report

Summary

Project Old New Diff Outcome
flake8 38.24MB 38.17MB -0.19% (72.54kB) ⬇️
trio 92.91MB 92.68MB -0.25% (235.94kB) ⬇️
sphinx 172.34MB 171.68MB -0.39% (683.75kB) ⬇️
prefect 476.04MB 474.24MB -0.38% (1.80MB) ⬇️

Significant changes

Click to expand detailed breakdown

flake8

Name Old New Diff Outcome
protocol_apply_self_with_receiver::interned_arguments 41.74kB 520.00B -98.78% (41.23kB) ⬇️
protocol_apply_self_with_receiver 16.30kB 200.00B -98.80% (16.11kB) ⬇️
protocol_bind_self::interned_arguments 14.27kB 0.00B -100.00% (14.27kB) ⬇️
CallableType 340.16kB 353.48kB +3.91% (13.32kB) ⏫
BoundMethodType<'db>::callables_ 19.54kB 12.73kB -34.87% (6.81kB) ⬇️
protocol_bind_self 6.48kB 0.00B -100.00% (6.48kB) ⬇️
when_constraint_set_assignable_to_owned_impl 81.65kB 80.58kB -1.31% (1.07kB) ⬇️
infer_definition_types 1.17MB 1.17MB +0.03% (320.00B) ⏫
infer_expression_types_impl 783.01kB 783.29kB +0.04% (288.00B) ⏫
Type<'db>::cached_materialization_::interned_arguments 9.61kB 9.38kB -2.44% (240.00B) ⬇️
non_recursive_protocol_interface::interned_arguments 2.58kB 2.41kB -6.67% (176.00B) ⬇️
infer_scope_types_impl 535.53kB 535.67kB +0.03% (144.00B) ⏫
Type<'db>::cached_materialization_ 6.80kB 6.66kB -2.07% (144.00B) ⬇️
non_recursive_protocol_interface 1.17kB 1.09kB -6.67% (80.00B) ⬇️
is_constraint_set_subtype_of_impl 2.99kB 2.95kB -1.57% (48.00B) ⬇️
... 7 more

trio

Name Old New Diff Outcome
protocol_apply_self_with_receiver::interned_arguments 133.86kB 1.02kB -99.24% (132.84kB) ⬇️
protocol_apply_self_with_receiver 52.80kB 400.00B -99.26% (52.41kB) ⬇️
CallableType 1.63MB 1.67MB +2.40% (40.11kB) ⏫
protocol_bind_self::interned_arguments 36.44kB 0.00B -100.00% (36.44kB) ⬇️
protocol_bind_self 16.56kB 0.00B -100.00% (16.56kB) ⬇️
when_constraint_set_assignable_to_owned_impl 538.50kB 522.05kB -3.05% (16.45kB) ⬇️
BoundMethodType<'db>::callables_ 91.66kB 78.34kB -14.53% (13.32kB) ⬇️
Type<'db>::cached_materialization_::interned_arguments 87.58kB 84.22kB -3.84% (3.36kB) ⬇️
ProtocolInterface 197.36kB 195.05kB -1.17% (2.31kB) ⬇️
Type<'db>::cached_materialization_ 65.29kB 63.25kB -3.12% (2.04kB) ⬇️
infer_expression_types_impl 5.95MB 5.95MB +0.02% (1.31kB) ⏫
Specialization 1.05MB 1.05MB -0.08% (864.00B) ⬇️
infer_scope_types_impl 2.83MB 2.83MB +0.03% (808.00B) ⏫
GenericAlias<'db>::try_mro_ 731.12kB 730.38kB -0.10% (768.00B) ⬇️
cached_protocol_interface 72.39kB 71.70kB -0.95% (704.00B) ⬇️
... 45 more

sphinx

Name Old New Diff Outcome
protocol_apply_self_with_receiver::interned_arguments 428.70kB 9.95kB -97.68% (418.74kB) ⬇️
protocol_apply_self_with_receiver 167.41kB 3.83kB -97.71% (163.58kB) ⬇️
CallableType 2.05MB 2.13MB +4.11% (86.23kB) ⏫
protocol_bind_self::interned_arguments 81.64kB 0.00B -100.00% (81.64kB) ⬇️
BoundMethodType<'db>::callables_ 185.49kB 132.91kB -28.35% (52.59kB) ⬇️
protocol_bind_self 37.11kB 0.00B -100.00% (37.11kB) ⬇️
infer_definition_types 11.69MB 11.68MB -0.06% (6.86kB) ⬇️
when_constraint_set_assignable_to_owned_impl 969.61kB 963.03kB -0.68% (6.58kB) ⬇️
infer_expression_types_impl 11.63MB 11.62MB -0.05% (5.81kB) ⬇️
receiver_violates_typevar_domain::interned_arguments 0.00B 4.73kB +4.73kB (new) ⏫
receiver_violates_typevar_domain 0.00B 3.55kB +3.55kB (new) ⏫
infer_statement_types_impl 530.95kB 528.61kB -0.44% (2.34kB) ⬇️
Type<'db>::cached_materialization_::interned_arguments 76.88kB 74.92kB -2.54% (1.95kB) ⬇️
infer_scope_types_impl 6.19MB 6.19MB -0.03% (1.82kB) ⬇️
Type<'db>::cached_materialization_ 53.18kB 52.02kB -2.17% (1.16kB) ⬇️
... 19 more

prefect

Name Old New Diff Outcome
protocol_apply_self_with_receiver::interned_arguments 820.12kB 23.97kB -97.08% (796.15kB) ⬇️
when_constraint_set_assignable_to_owned_impl 3.72MB 3.17MB -14.72% (560.53kB) ⬇️
protocol_apply_self_with_receiver 317.44kB 9.23kB -97.09% (308.21kB) ⬇️
CallableType 8.95MB 9.10MB +1.75% (160.66kB) ⏫
protocol_bind_self::interned_arguments 146.87kB 88.00B -99.94% (146.78kB) ⬇️
BoundMethodType<'db>::callables_ 436.70kB 350.27kB -19.79% (86.43kB) ⬇️
protocol_bind_self 66.76kB 40.00B -99.94% (66.72kB) ⬇️
Type<'db>::cached_materialization_::interned_arguments 402.27kB 390.00kB -3.05% (12.27kB) ⬇️
ProtocolInterface 914.74kB 905.43kB -1.02% (9.31kB) ⬇️
Type<'db>::cached_materialization_ 337.25kB 329.89kB -2.18% (7.36kB) ⬇️
receiver_violates_typevar_domain::interned_arguments 0.00B 6.70kB +6.70kB (new) ⏫
infer_scope_types_impl 19.62MB 19.63MB +0.02% (5.02kB) ⏫
Type<'db>::apply_specialization_inner_::interned_arguments 4.10MB 4.09MB -0.12% (4.92kB) ⬇️
Type<'db>::apply_specialization_inner_ 2.96MB 2.95MB -0.13% (3.88kB) ⬇️
for_constraint_pair_inner 3.42MB 3.42MB -0.10% (3.64kB) ⬇️
... 55 more

@astral-sh-bot

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

Copy link
Copy Markdown

ecosystem-analyzer results

Lint rule Added Removed Changed
invalid-argument-type 17 0 0
dynamic-function-decorator-return 0 4 0
type-assertion-failure 0 3 0
invalid-return-type 0 1 0
Total 17 8 0

Large timing changes:

Project Old Time New Time Change
pytest-autoprofile 0.16s 19.39s +12199%

Flaky changes detected. This PR summary excludes flaky changes; see the HTML report for details.

Raw diff (25 changes)
antidote (https://github.com/Finistere/antidote)
+ src/antidote/lib/interface_ext/_interface.py:540:36 error[invalid-argument-type] Argument to bound method `list.append` is incorrect: Expected `Weight@__register_implementation`, found `(Predicate[NeutralWeight] & Top[ImplementationWeight] & ~Predicate[Top[ImplementationWeight]]) | (Weight@__register_implementation & ~Predicate[Top[ImplementationWeight]] & ~NeutralWeight)`

psycopg (https://github.com/psycopg/psycopg)
- psycopg/psycopg/rows.py:137:16 error[invalid-return-type] Return type does not match returned value: expected `RowMaker[tuple[object, ...] & NamedTupleLike]`, found `(iterable: Iterable[Any]) -> Self@_make`

pyochain (https://github.com/OutSquareCapital/pyochain)
- tests/test_extern/test_cpython/test_abc/test_collection.py:60:1 error[dynamic-function-decorator-return] Decorator returns `Unknown`
- tests/test_extern/test_cpython/test_abc/test_iterator.py:34:1 error[dynamic-function-decorator-return] Decorator returns `Unknown`
- tests/test_extern/test_cpython/test_abc/test_reversible.py:56:1 error[dynamic-function-decorator-return] Decorator returns `Unknown`
- benchmarks/test_sequences.py:77:1 error[dynamic-function-decorator-return] Decorator returns `Unknown`
- tests/test_typing/check_iter.py:54:11 error[type-assertion-failure] Type `Unknown` does not match asserted type `PyoIterator[PyoIterator[list[int]]]`
- tests/test_typing/check_iter.py:55:11 error[type-assertion-failure] Type `Unknown` does not match asserted type `PyoIterator[list[int]]`
- tests/test_typing/check_iter.py:56:9 error[type-assertion-failure] Type `Unknown` does not match asserted type `PyoIterator[int]`

sympy (https://github.com/sympy/sympy)
+ sympy/polys/matrices/tests/test_dense.py:28:14 error[invalid-argument-type] Argument to function `ddm_iadd` is incorrect: Argument type `int` does not satisfy upper bound `RingElement` of type variable `R`
+ sympy/polys/matrices/tests/test_dense.py:28:17 error[invalid-argument-type] Argument to function `ddm_iadd` is incorrect: Argument type `int` does not satisfy upper bound `RingElement` of type variable `R`
+ sympy/polys/matrices/tests/test_dense.py:35:14 error[invalid-argument-type] Argument to function `ddm_isub` is incorrect: Argument type `int` does not satisfy upper bound `RingElement` of type variable `R`
+ sympy/polys/matrices/tests/test_dense.py:35:17 error[invalid-argument-type] Argument to function `ddm_isub` is incorrect: Argument type `int` does not satisfy upper bound `RingElement` of type variable `R`
+ sympy/polys/matrices/tests/test_dense.py:41:14 error[invalid-argument-type] Argument to function `ddm_ineg` is incorrect: Argument type `int` does not satisfy upper bound `RingElement` of type variable `R`
+ sympy/polys/matrices/tests/test_dense.py:47:14 error[invalid-argument-type] Argument to function `ddm_imul` is incorrect: Argument type `int` does not satisfy upper bound `RingElement` of type variable `R`
+ sympy/polys/matrices/tests/test_dense.py:51:14 error[invalid-argument-type] Argument to function `ddm_imul` is incorrect: Argument type `int` does not satisfy upper bound `RingElement` of type variable `R`
+ sympy/polys/matrices/tests/test_dense.py:60:17 error[invalid-argument-type] Argument to function `ddm_imatmul` is incorrect: Argument type `int` does not satisfy upper bound `RingElement` of type variable `R`
+ sympy/polys/matrices/tests/test_dense.py:60:21 error[invalid-argument-type] Argument to function `ddm_imatmul` is incorrect: Argument type `int` does not satisfy upper bound `RingElement` of type variable `R`
+ sympy/polys/matrices/tests/test_dense.py:60:24 error[invalid-argument-type] Argument to function `ddm_imatmul` is incorrect: Argument type `int` does not satisfy upper bound `RingElement` of type variable `R`
+ sympy/polys/matrices/tests/test_dense.py:64:17 error[invalid-argument-type] Argument to function `ddm_imatmul` is incorrect: Argument type `int` does not satisfy upper bound `RingElement` of type variable `R`
+ sympy/polys/matrices/tests/test_dense.py:64:21 error[invalid-argument-type] Argument to function `ddm_imatmul` is incorrect: Argument type `int` does not satisfy upper bound `RingElement` of type variable `R`
+ sympy/polys/matrices/tests/test_dense.py:64:24 error[invalid-argument-type] Argument to function `ddm_imatmul` is incorrect: Argument type `int` does not satisfy upper bound `RingElement` of type variable `R`
+ sympy/polys/matrices/tests/test_dense.py:69:17 error[invalid-argument-type] Argument to function `ddm_imatmul` is incorrect: Argument type `int` does not satisfy upper bound `RingElement` of type variable `R`
+ sympy/polys/matrices/tests/test_dense.py:69:21 error[invalid-argument-type] Argument to function `ddm_imatmul` is incorrect: Argument type `int` does not satisfy upper bound `RingElement` of type variable `R`
+ sympy/polys/matrices/tests/test_dense.py:69:24 error[invalid-argument-type] Argument to function `ddm_imatmul` is incorrect: Argument type `int` does not satisfy upper bound `RingElement` of type variable `R`

Full report with detailed diff (timing results)

@codspeed

codspeed Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

Merging this PR will regress 5 benchmarks

⚡ 5 improved benchmarks
❌ 5 regressed benchmarks
✅ 152 untouched benchmarks
⏩ 60 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ WallTime colour_science 43.9 s 52.1 s -15.65%
❌ Simulation DateType 579.1 ms 639 ms -9.36%
❌ WallTime pandas 95.2 s 103.5 s -7.99%
❌ WallTime static_frame 36.6 s 39 s -6.3%
❌ WallTime freqtrade 12.2 s 13 s -5.71%
⚡ Simulation ty_micro[inherited_recursive_protocol_diagnostic] 432.1 ms 162.2 ms ×2.7
⚡ Simulation ty_micro[inherited_recursive_protocol_constructor] 432.4 ms 165.5 ms ×2.6
⚡ Memory ty_micro[inherited_recursive_protocol_diagnostic] 13 MB 7.5 MB +72.19%
⚡ Memory ty_micro[inherited_recursive_protocol_constructor] 13 MB 7.6 MB +71.54%
⚡ Simulation ty_micro[many_protocol_members_mismatch] 1.4 s 1.2 s +11.34%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing codex/protocol-member-receivers (266b82f) with main (55129c1)

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. ↩

Base automatically changed from david/method-receiver-tests to main September 18, 2026 10:04
@sharkdp
sharkdp force-pushed the codex/protocol-member-receivers branch 3 times, most recently from 4450640 to ba5535c Compare September 23, 2026 12:03
@sharkdp
sharkdp changed the base branch from main to codex/protocol-member-refactor September 23, 2026 12:03
@sharkdp
sharkdp added this pull request to stack #28823 September 23, 2026 12:05
@sharkdp
sharkdp force-pushed the codex/protocol-member-receivers branch 5 times, most recently from 00540aa to 5986a53 Compare September 23, 2026 20:16
Base automatically changed from codex/protocol-member-refactor to main September 24, 2026 10:42
@sharkdp
sharkdp force-pushed the codex/protocol-member-receivers branch from 5986a53 to de466ee Compare September 24, 2026 10:42
@sharkdp
sharkdp force-pushed the codex/protocol-member-receivers branch from d4e9ea7 to d147101 Compare September 29, 2026 15:22
Comment on lines +1393 to +1401
info: type `Concrete[T@diagnose]` is not assignable to protocol `Outer[int]`
info: └── protocol member `expose` is incompatible
info: └── incompatible return types: `Concrete[T@diagnose]` is not assignable to `Chain[int]`
info: └── type `Concrete[T@diagnose]` is not assignable to protocol `Chain[int]`
info: └── protocol member `child` is incompatible
info: └── incompatible return types: `Concrete[str]` is not assignable to `Chain[int]`
info: └── type `Concrete[str]` is not assignable to protocol `Chain[int]`
info: └── protocol member `value` is incompatible
info: └── incompatible return types: `str` is not assignable to `int`

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.

We switch from reporting child to expose. This is a known limitation of error context trees for protocols: we can only report one incompatible member right now.

This branch was previously deployed

1 inactive deployment
automations — 266b82fe Deployed Oct 2, 2026 by sharkdp via security-review / security review #90580
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

1 participant