Skip to content

[ty] Infer typevar specializations for implicit generic protocols#21902

Merged
dcreager merged 46 commits intomainfrom
dcreager/genprop
Feb 11, 2026
Merged

[ty] Infer typevar specializations for implicit generic protocols#21902
dcreager merged 46 commits intomainfrom
dcreager/genprop

Conversation

@dcreager
Copy link
Member

@dcreager dcreager commented Dec 10, 2025

We now infer specializations that involve generic protocols. This includes recursing into the methods of the protocol, matching up the signatures of the class's methods with the signatures of the protocol, and adding bindings for any typevars that appear in the protocol signatures.

This required several performance updates, which were pulled out and merged as separate PRs. There is still a performance and memory hit, but I think a reasonable one, given the new functionality that this opens up.

There are still some ecosystem false positives that relate to how this interacts with overload resolution, but I plan to tackle that in follow-on PRs.

@dcreager dcreager added the ty Multi-file analysis & type inference label Dec 10, 2025
@astral-sh-bot
Copy link

astral-sh-bot bot commented Dec 10, 2025

Typing conformance results improved 🎉

The percentage of diagnostics emitted that were expected errors increased from 83.14% to 83.49%. The percentage of expected errors that received a diagnostic held steady at 74.24%.

Summary

Metric Old New Diff Outcome
True Positives 804 804 +0
False Positives 163 159 -4 ⏬ (✅)
False Negatives 279 279 +0
Total Diagnostics 967 963 -4
Precision 83.14% 83.49% +0.35% ⏫ (✅)
Recall 74.24% 74.24% +0.00%

False positives removed

Details
Location Name Message
generics_base_class.py:45:5 type-assertion-failure Type Iterator[int] does not match asserted type Unknown
generics_basic.py:139:5 type-assertion-failure Type int does not match asserted type Unknown
generics_basic.py:140:5 type-assertion-failure Type int does not match asserted type Unknown
generics_basic.py:199:5 type-assertion-failure Type Iterator[Any] does not match asserted type Unknown

@dcreager dcreager force-pushed the dcreager/callable-return branch 2 times, most recently from 874dc7e to 8871fdd Compare December 14, 2025 00:45
@codspeed-hq
Copy link

codspeed-hq bot commented Dec 16, 2025

Merging this PR will degrade performance by 19.23%

⚡ 1 improved benchmark
❌ 5 (👁 5) regressed benchmarks
✅ 48 untouched benchmarks

Performance Changes

Mode Benchmark BASE HEAD Efficiency
👁 WallTime pandas 73 s 77.8 s -6.14%
👁 WallTime sympy 52.4 s 55.3 s -5.26%
👁 WallTime static_frame 26.7 s 29.9 s -10.81%
👁 WallTime colour_science 104.3 s 129.1 s -19.23%
👁 Simulation attrs 435.7 ms 461.3 ms -5.56%
Simulation DateType 232.9 ms 210 ms +10.88%

Comparing dcreager/genprop (b66cf8b) with main (8cfeae8)

Open in CodSpeed

@dcreager dcreager changed the base branch from dcreager/callable-return to main December 16, 2025 01:26
@astral-sh-bot
Copy link

astral-sh-bot bot commented Dec 16, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@dcreager dcreager force-pushed the dcreager/genprop branch 2 times, most recently from 7145810 to 6fb49ec Compare December 16, 2025 17:34
@astral-sh-bot
Copy link

astral-sh-bot bot commented Feb 10, 2026

Memory usage report

Summary

Project Old New Diff Outcome
sphinx 270.10MB 270.83MB +0.27% (743.08kB)
flake8 49.45MB 49.50MB +0.10% (49.15kB)
trio 118.29MB 118.05MB -0.20% (242.36kB) ⬇️
prefect 681.16MB 680.31MB -0.12% (861.68kB) ⬇️

Significant changes

Click to expand detailed breakdown

sphinx

Name Old New Diff Outcome
infer_expression_types_impl 21.26MB 21.53MB +1.31% (284.18kB)
for_constraint_pair_inner 56.36kB 182.46kB +223.77% (126.11kB)
solutions_inner 21.50kB 103.55kB +381.56% (82.05kB)
CallableType 1.20MB 1.12MB -6.25% (76.64kB)
for_constraint_inner 32.88kB 102.57kB +211.94% (69.69kB)
InteriorNode 27.93kB 86.33kB +209.09% (58.40kB)
StaticClassLiteral<'db>::try_mro_ 2.12MB 2.17MB +2.17% (47.15kB)
infer_definition_types 23.34MB 23.39MB +0.20% (47.00kB)
ConstrainedTypeVar 17.72kB 53.25kB +200.53% (35.53kB)
InteriorNode<'db>::or_::interned_arguments 560.00B 30.78kB +5528.57% (30.23kB)
for_constraint_pair_inner::interned_arguments 14.13kB 43.52kB +207.96% (29.39kB)
InteriorNode<'db>::or_ 476.00B 28.85kB +6105.88% (28.38kB)
Type<'db>::try_call_dunder_get_ 5.52MB 5.49MB -0.46% (25.70kB)
Specialization 1.03MB 1.05MB +1.87% (19.77kB)
is_equivalent_to_object_inner 105.09kB 122.50kB +16.57% (17.41kB)
... 54 more

flake8

Name Old New Diff Outcome
CallableType 205.17kB 182.39kB -11.10% (22.78kB)
StaticClassLiteral<'db>::try_mro_ 333.77kB 344.66kB +3.26% (10.89kB)
solutions_inner 2.85kB 12.46kB +336.85% (9.61kB)
for_constraint_pair_inner 4.11kB 12.90kB +213.68% (8.79kB)
for_constraint_inner 3.77kB 12.34kB +227.02% (8.57kB)
FunctionType 435.38kB 441.31kB +1.36% (5.94kB)
InteriorNode 3.45kB 9.24kB +167.65% (5.79kB)
is_equivalent_to_object_inner 23.33kB 28.31kB +21.35% (4.98kB)
ConstrainedTypeVar 2.16kB 6.66kB +208.70% (4.50kB)
infer_scope_types_impl 1.09MB 1.08MB -0.32% (3.59kB)
Type<'db>::class_member_with_policy_ 577.16kB 573.62kB -0.61% (3.54kB)
Type<'db>::try_call_dunder_get_ 415.30kB 411.95kB -0.81% (3.35kB)
InteriorNode<'db>::or_::interned_arguments 320.00B 3.52kB +1025.00% (3.20kB)
infer_deferred_types 717.88kB 714.79kB -0.43% (3.09kB)
InteriorNode<'db>::or_ 272.00B 3.25kB +1122.06% (2.98kB)
... 50 more

trio

Name Old New Diff Outcome
infer_scope_types_impl 5.56MB 5.45MB -1.97% (112.37kB) ⬇️
CallableType 700.79kB 597.36kB -14.76% (103.43kB) ⬇️
FunctionType 1.57MB 1.54MB -2.11% (34.09kB) ⬇️
FunctionType<'db>::signature_ 1.11MB 1.09MB -2.26% (25.82kB) ⬇️
for_constraint_inner 36.11kB 60.23kB +66.83% (24.13kB) ⬇️
cached_protocol_interface 153.93kB 133.39kB -13.34% (20.53kB) ⬇️
for_constraint_pair_inner 40.04kB 59.20kB +47.85% (19.16kB) ⬇️
solutions_inner 20.74kB 38.67kB +86.48% (17.93kB) ⬇️
InteriorNode 25.70kB 43.16kB +67.98% (17.47kB) ⬇️
Type<'db>::apply_specialization_ 786.48kB 770.04kB -2.09% (16.44kB) ⬇️
infer_definition_types 7.18MB 7.17MB -0.22% (16.43kB) ⬇️
infer_unpack_types 163.69kB 150.60kB -8.00% (13.09kB) ⬇️
ProtocolInterface 106.82kB 94.70kB -11.35% (12.12kB) ⬇️
ConstrainedTypeVar 18.84kB 30.84kB +63.68% (12.00kB) ⬇️
BoundMethodType<'db>::into_callable_type_ 81.80kB 71.95kB -12.03% (9.84kB) ⬇️
... 55 more

prefect

Name Old New Diff Outcome
CallableType 2.31MB 1.88MB -18.67% (442.16kB) ⬇️
FunctionType 8.71MB 8.43MB -3.21% (286.47kB) ⬇️
for_constraint_pair_inner 150.76kB 424.28kB +181.42% (273.52kB) ⬇️
FunctionType<'db>::signature_ 4.08MB 3.92MB -3.96% (165.14kB) ⬇️
cached_protocol_interface 524.92kB 382.52kB -27.13% (142.39kB) ⬇️
infer_expression_types_impl 51.14MB 51.25MB +0.22% (113.27kB) ⬇️
solutions_inner 79.25kB 192.12kB +142.42% (112.87kB) ⬇️
Type<'db>::class_member_with_policy_ 16.91MB 16.80MB -0.65% (111.73kB) ⬇️
InteriorNode 86.23kB 195.71kB +126.97% (109.48kB) ⬇️
for_constraint_inner 111.91kB 217.38kB +94.24% (105.47kB) ⬇️
Type<'db>::try_call_dunder_get_ 11.25MB 11.15MB -0.91% (105.18kB) ⬇️
infer_scope_types_impl 57.37MB 57.27MB -0.18% (104.09kB) ⬇️
Type<'db>::apply_specialization_ 3.79MB 3.70MB -2.48% (96.25kB) ⬇️
ProtocolInterface 358.12kB 274.76kB -23.28% (83.35kB) ⬇️
Type<'db>::class_member_with_policy_::interned_arguments 8.90MB 8.83MB -0.81% (73.94kB) ⬇️
... 65 more

@dcreager dcreager marked this pull request as ready for review February 10, 2026 16:26
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

Awesome work!! This looks very reasonable to me

Copy link
Contributor

@sharkdp sharkdp left a comment

Choose a reason for hiding this comment

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

Very cool — thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ecosystem-analyzer ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants