Skip to content

[ty] Widen nested literals during inference for huge collections#24716

Draft
charliermarsh wants to merge 2 commits intomainfrom
charlie/promote-perf
Draft

[ty] Widen nested literals during inference for huge collections#24716
charliermarsh wants to merge 2 commits intomainfrom
charlie/promote-perf

Conversation

@charliermarsh
Copy link
Copy Markdown
Member

Summary

For a collection like:

from django.utils.translation import gettext_lazy as _

DK_POSTALCODES = (
    ("0555", _("Scanning")),
    ("0783", _("Facility")),
    ("0800", _("Høje Taastrup")),
    # ...~1,400 more entries
)

We created a huge union of the key literals, which timed out in inference after 12 seconds (according to Codex), but now completes in 0.040s.

We now fallback to inferring str rather than a Literal for each key here and elsewhere.

Discovered as part of: astral-sh/ty#1393.

@charliermarsh charliermarsh added the performance Potential performance improvement label Apr 19, 2026
@astral-sh-bot astral-sh-bot bot added the ty Multi-file analysis & type inference label Apr 19, 2026
@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot bot commented Apr 19, 2026

Typing conformance results

No changes detected ✅

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

@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot bot commented Apr 19, 2026

Memory usage report

Summary

Project Old New Diff Outcome
flake8 47.94MB 47.94MB -
trio 117.64MB 117.64MB -
prefect 717.22MB 717.21MB -0.00% (15.58kB) ⬇️
sphinx 262.78MB 262.74MB -0.02% (43.11kB) ⬇️

Significant changes

Click to expand detailed breakdown

prefect

Name Old New Diff Outcome
Type<'db>::class_member_with_policy_ 17.63MB 17.62MB -0.05% (8.20kB) ⬇️
Type<'db>::member_lookup_with_policy_ 17.25MB 17.25MB -0.02% (4.34kB) ⬇️
Type<'db>::member_lookup_with_policy_::interned_arguments 5.95MB 5.95MB -0.03% (1.52kB) ⬇️
Type<'db>::class_member_with_policy_::interned_arguments 9.79MB 9.79MB -0.02% (1.52kB) ⬇️
Type<'db>::apply_specialization_::interned_arguments 3.00MB 3.00MB -0.01% (400.00B) ⬇️
FunctionType 8.90MB 8.90MB +0.00% (368.00B) ⬇️
FunctionType<'db>::last_definition_signature_ 811.10kB 811.43kB +0.04% (340.00B) ⬇️
infer_definition_types 90.43MB 90.43MB -0.00% (300.00B) ⬇️
Type<'db>::apply_specialization_ 3.71MB 3.71MB -0.01% (280.00B) ⬇️
FunctionType<'db>::signature_ 4.09MB 4.09MB +0.00% (200.00B) ⬇️
BoundMethodType 1.47MB 1.47MB +0.01% (80.00B) ⬇️

sphinx

Name Old New Diff Outcome
TupleType 563.28kB 528.89kB -6.11% (34.39kB) ⬇️
infer_deferred_types 5.53MB 5.53MB -0.04% (2.12kB) ⬇️
function_known_decorators 2.46MB 2.46MB -0.08% (1.94kB) ⬇️
Type<'db>::try_call_dunder_get_ 4.88MB 4.88MB -0.03% (1.56kB) ⬇️
infer_definition_types 23.63MB 23.63MB -0.00% (728.00B) ⬇️
try_call_bin_op_return_type_impl 207.61kB 206.92kB -0.33% (700.00B) ⬇️
Type<'db>::class_member_with_policy_ 7.63MB 7.63MB -0.01% (588.00B) ⬇️
Type<'db>::member_lookup_with_policy_ 6.86MB 6.85MB -0.01% (408.00B) ⬇️
FunctionType 3.11MB 3.11MB +0.01% (368.00B) ⬇️
Type<'db>::member_lookup_with_policy_::interned_arguments 2.67MB 2.67MB -0.01% (312.00B) ⬇️
Type<'db>::class_member_with_policy_::interned_arguments 4.03MB 4.03MB -0.01% (312.00B) ⬇️
Type<'db>::try_call_dunder_get_::interned_arguments 1.16MB 1.16MB -0.03% (312.00B) ⬇️
BoundMethodType 707.97kB 707.73kB -0.03% (240.00B) ⬇️
FunctionType<'db>::last_definition_signature_ 221.45kB 221.67kB +0.10% (220.00B) ⬇️
CallableType 1.12MB 1.12MB +0.02% (216.00B) ⬇️
... 6 more

@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot bot commented Apr 19, 2026

ecosystem-analyzer results

Lint rule Added Removed Changed
invalid-argument-type 0 0 2
no-matching-overload 0 0 1
Total 0 0 3

Raw diff:

freqtrade (https://github.com/freqtrade/freqtrade)
- freqtrade/optimize/optimize_reports/optimize_reports.py:670:48 error[invalid-argument-type] Argument to function `calculate_trade_volume` is incorrect: Expected `list[dict[str, Any]]`, found `list[dict[Hashable, Any]]`
+ freqtrade/optimize/optimize_reports/optimize_reports.py:670:48 error[invalid-argument-type] Argument is incorrect: Expected `list[dict[str, Any]]`, found `list[dict[Hashable, Any]]`
- freqtrade/optimize/optimize_reports/optimize_reports.py:687:47 error[invalid-argument-type] Argument to function `generate_wallet_stats` is incorrect: Expected `DataFrame`, found `DataFrame | None`
+ freqtrade/optimize/optimize_reports/optimize_reports.py:687:47 error[invalid-argument-type] Argument is incorrect: Expected `DataFrame`, found `DataFrame | None`

zulip (https://github.com/zulip/zulip)
- zproject/urls.py:316:5 error[no-matching-overload] No overload of function `path` matches arguments
+ zproject/urls.py:316:5 error[no-matching-overload] No overload matches arguments

Full report with detailed diff (timing results)

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Apr 19, 2026

Merging this PR will degrade performance by 10.33%

❌ 1 regressed benchmark
✅ 48 untouched benchmarks
⏩ 60 skipped benchmarks1

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
WallTime colour_science 53.4 s 59.5 s -10.33%

Comparing charlie/promote-perf (80d61d1) with main (5062d0d)

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.

@charliermarsh charliermarsh force-pushed the charlie/promote-perf branch 2 times, most recently from afd662c to ee4b6e2 Compare April 20, 2026 04:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Potential performance improvement ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant