Skip to content

[ty] Experiment: Receiver refinements - #29692

Draft
sharkdp wants to merge 3 commits into
astral-sh:mainfrom
sharkdp:david/receiver-refine
Draft

sharkdp wants to merge 3 commits into
astral-sh:mainfrom
sharkdp:david/receiver-refine

Conversation

@sharkdp

@sharkdp sharkdp commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Add a new Refines[Receiver, Return] typing construct that can be used in return annotation position to signal that a method returns Return, but also refines/narrows the type of the receiver to Receiver.

Example 1:

image

Example 2:

image

Example 3:

from typing import Self, Never, reveal_type
from ty_extensions.experimental import Refines

class TypedStack[Elements = tuple[()]]:
    def __init__(self):
        self._elements = []

    def push[*Ts, T](self: TypedStack[tuple[*Ts]], x: T) -> Refines[TypedStack[tuple[*Ts, T]], None]:
        self._elements.append(x)

    def pop[*Ts, T](self: TypedStack[tuple[*Ts, T]]) -> Refines[TypedStack[tuple[*Ts]], T]:
        return self._elements.pop()

c = TypedStack()

c.push(1)
c.push("foo")
c.push(b"bar")

reveal_type(c.pop())  # Literal[b"bar"]
reveal_type(c.pop())  # Literal["foo"]
reveal_type(c.pop())  # Literal[1]

c.pop()  # error, stack is empty

In combination with intersection types, this can be used to write stubs for APIs like argparse, where ArgumentParser can now keep track of the added arguments at the type system level, to allow for something like this (see attached mdtest):

parser = ArgumentParser()
parser.add_argument("host", type=str)
parser.add_argument("port", type=int)
args = parser.parse_args()

reveal_type(args.host)  # str
reveal_type(args.port)  # int

Related discussions

Test Plan

...

@sharkdp sharkdp added the ty The ty type checker label Oct 24, 2026
@sharkdp sharkdp changed the title [ty] Prototype receiver refinement with Refines [ty] Experiment: Receiver refinements Oct 24, 2026
@codspeed

codspeed Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Merging this PR will degrade performance by 42.18%

⚡ 1 improved benchmark
❌ 14 regressed benchmarks
✅ 187 untouched benchmarks
⏩ 74 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ CPU Simulation ty_micro[repeated_statement_calls_in_try] 707 ms 71,415.2 ms -99.01%
❌ Memory ty_micro[repeated_statement_calls_in_try_with_if_branches] 13 MB 62.6 MB -79.3%
❌ CPU Simulation ty_micro[repeated_statement_calls_in_try_with_if_branches] 1.4 s 2.5 s -44.99%
❌ CPU Simulation ty_micro[repeated_statement_calls] 470.1 ms 843 ms -44.24%
❌ CPU Simulation ty_micro[repeated_statement_calls_fixed_reachability] 458.4 ms 818.4 ms -43.99%
❌ CPU Simulation ty_micro[repeated_statement_calls_pre_narrowed] 476.2 ms 819.5 ms -41.89%
❌ Memory ty_micro[repeated_statement_calls_fixed_reachability] 10.7 MB 12.5 MB -14.41%
❌ Memory ty_micro[repeated_statement_calls] 10.4 MB 12.1 MB -14.33%
❌ Memory ty_micro[repeated_statement_calls_pre_narrowed] 10.7 MB 12.4 MB -13.75%
❌ Walltime static_frame 37.6 s 41.2 s -8.8%
❌ Walltime multithreaded 1.2 s 1.3 s -7.57%
❌ Walltime pydantic 9.1 s 9.6 s -5.73%
❌ CPU Simulation ty_micro[typeis_narrowing] 374.5 ms 390.6 ms -4.12%
❌ Walltime freqtrade 12.6 s 13.1 s -4.03%
⚡ Memory ty_micro[repeated_statement_calls_in_try] 35.7 MB 12.5 MB ×2.9

Tip

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


Comparing sharkdp:david/receiver-refine (132698d) with main (ea18658)

Open in CodSpeed

Footnotes

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

This branch has not been deployed

No deployments
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.

1 participant