Skip to content

Fix exponential union growth when narrowing generic TypedDicts - #11850

Open
Karan Rajeshbhai Mungara (withkarann) wants to merge 4 commits into
microsoft:mainfrom
withkarann:fix/generic-typeddict-union-merge
Open

Karan Rajeshbhai Mungara (withkarann) wants to merge 4 commits into
microsoft:mainfrom
withkarann:fix/generic-typeddict-union-merge

Conversation

@withkarann

Copy link
Copy Markdown

Summary

Fixes #11228.

When control flow joins two narrowed copies of the same TypedDict, _addTypeIfUnique merges them into one union entry. It only did this when neither copy had type arguments. Copies of a generic TypedDict such as TD[int] were never merged, so every conditional key assignment doubled the union: 14 if ...: td["k"] = v blocks build a union of 2^14 entries.

This change merges them when the type arguments are the same, which is what the comment above the check describes. Copies with different type arguments, such as TD[int] | TD[str], are still kept apart.

It also removes false assert_type errors after such narrowing (expected "TD1[int]" but received "TD1[int]"), and the join behavior from #11745 now applies to generic TypedDicts too.

Validation

  • New sample typedDict31.py (test TypedDict31). On main it reports 4 errors: 3 false assert_type mismatches and the expected one. With this change only the expected one remains.
  • pnpm test in packages/pyright-internal: all pass (one call hierarchy case in languageServer.test.ts timed out once under load and passes on rerun).
  • pnpm run check: clean.
  • Same diagnostics as main on click, requests, pytest, openai-python and Home Assistant (9,850 files).

Timing with the production CLI bundle, Apple M5 Pro, macOS 26.5, Node 24.19, median of 3 runs (one run for main at 16 keys):

File main this PR
Repro from #11228 (pydantic installed) 44.2 s 0.48 s
Generated, 10 conditional keys 0.38 s 0.25 s
Generated, 12 conditional keys 1.70 s 0.25 s
Generated, 14 conditional keys 22.4 s 0.25 s
Generated, 16 conditional keys 512 s 0.55 s
Generator
import sys

n = int(sys.argv[1])
keys = [f"k{i}" for i in range(n)]
print("from typing import TypedDict\n")
print("class TD[T](TypedDict, total=False):")
for k in keys:
    print(f"    {k}: T")
print("\ndef f(v: int | None) -> TD[int]:\n    td: TD[int] = {}")
for k in keys:
    print(f"    if v is not None:\n        td[\"{k}\"] = v")
print("    return td")

python gen.py 14 > keys14.py && pyright keys14.py

No change on ordinary code (CPU time, paired runs of the production bundles): pytest -1.8% +/- 2.5% over 15 runs, Home Assistant mean +0.7% / median -3.4% over 5 runs.

Note on build/perfCompare.py: it runs dist/pyright.js directly, so global.__rootDirectory is not set and the bundle does not find typeshed (Import "sys" could not be resolved). I used its method but started each bundle through an index.js wrapper like packages/pyright/index.js.

@withkarann

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@heejaechang

Heejae Chang (heejaechang) commented Oct 9, 2026 •

Copy link
Copy Markdown
Collaborator

🔴 Pyright CLI QA — 5f82eecd — verdict: red

Automated CLI checks compared this change with its base revision.
24 focused check(s) completed.

Needs review: reportTypedDictNotRequiredAccess newly reported

The PR reports reportTypedDictNotRequiredAccess for this example; the base does not. Is this change intended?

Reproduction and observed results

Create these files in an empty directory:

main.py

from typing import TypedDict

class Record[T](TypedDict, total=False):
    common: T
    left: T
    right: T

def joined(flag: bool) -> int:
    record: Record[int] = {"common": 1}
    if flag:
        record["left"] = 2
    else:
        record["right"] = 3
    return record["common"]

pyrightconfig.json

{"pythonVersion": "3.13", "typeCheckingMode": "strict"}

From that directory, run each built Pyright revision:

node <pyright-cli-entrypoint> --outputjson -p .

Base 79cd772619fa33f28d3e274665d6355193850f6f

{
  "exit_code": 0,
  "diagnostics": []
}

PR 5f82eecd024170336bc41d6f9a46b63026d68b35

{
  "exit_code": 1,
  "diagnostics": [
    {
      "file": "main.py",
      "severity": "error",
      "rule": "reportTypedDictNotRequiredAccess",
      "message": "Could not access item in TypedDict\n  \"common\" is not a required key in \"Record[int]\", so access may result in runtime exception",
      "range": {
        "start": [
          13,
          11
        ],
        "end": [
          13,
          27
        ]
      }
    }
  ]
}

Diagnostic ranges use zero-based line and character positions.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@withkarann

Copy link
Copy Markdown
Author

Yes, this is intended. It is the join rule from #11745: when two narrowed forms of the same TypedDict do not contain each other, the join falls back to the declared TypedDict. Before this PR, the check that applies that rule skipped any TypedDict with type arguments. It tested for no type arguments, while its comment says "different type arguments". Generic TypedDicts now get the same result as non-generic ones.

The same code with a non-generic Record reports the same error on main (2a9c700):

from typing import TypedDict

class Record(TypedDict, total=False):
    common: int
    left: int
    right: int

def joined(flag: bool) -> int:
    record: Record
    if flag:
        record = {"common": 1, "left": 2}
    else:
        record = {"common": 3, "right": 4}
    return record["common"]  # reportTypedDictNotRequiredAccess on main

Keeping both narrowed forms, as main does for generic TypedDicts, is what makes the union grow. Each if/else that provides different keys doubles it:

if/else blocks main this PR
10 0.37 s 0.22 s
12 2.36 s 0.27 s
14 33.4 s 0.23 s
Generator
import sys

n = int(sys.argv[1])
print("from typing import TypedDict\n")
print("class TD[T](TypedDict, total=False):")
for i in range(n):
    print(f"    a{i}: T\n    b{i}: T")
print("\ndef f(v: int, c: bool) -> TD[int]:\n    td: TD[int] = {}")
for i in range(n):
    print(f"    if c:\n        td[\"a{i}\"] = v\n    else:\n        td[\"b{i}\"] = v")
print("    return td")

Production CLI bundles, Apple M5 Pro, Node 24.19, one run each.

Keeping only the keys that both sides provide would avoid this error for both kinds of TypedDict. But assert_type compares narrowed keys, so assert_type(a, AwesomeDict) in typedDict30.py (#11743) fails again. I tried it, and that test fails. I can send it as a separate PR if you want that trade-off.

Another option is to merge generic copies only when one contains the other. That still fixes #11228, where each if only adds a key. But generic if/else chains like the one above stay as slow as on main.

@bschnurr

Copy link
Copy Markdown
Member

Copilot resolve the merge conflicts in this pull request

Narrowed copies of a generic TypedDict were never merged at control flow
join points, so each narrowing doubled the size of the union. Merge them
when their type arguments match, as is already done for non-generic
TypedDicts.

Fixes microsoft#11228.
@withkarann

Copy link
Copy Markdown
Author

Bill Schnurr (@bschnurr) Rebased on main and resolved the conflict. The only conflict was the sample file name: main added its own typedDict31.py, so this sample is now typedDict34.py. The code change is unchanged, and the typeEvaluator tests pass locally.

@bschnurr

Copy link
Copy Markdown
Member

/benchmark

@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@github-actions

Copy link
Copy Markdown
Contributor

Compared candidate 9405c8f137a2f118a4c65ba35f1fb449164e2ea2 against its first parent 3196e53e8c702c5e9f65bb79f7c71c4d71cb77cf.

View the full release history charts with the base and PR results.

Execution time history

Execution time history

Peak memory history

Peak memory history

Download the chart artifact.

Type checker benchmark

🟢 No performance regressions detected.

Regression threshold: 20.0%
Statistic: median
Variance guard: >1.0s time and >100.0 MB memory

Package Checker Files checked Time Time delta Peak memory Memory delta Status
ansible pyright 583 19.645s +0.2% 1086.5 MB -0.3% 🟢 Pass
click pyright 17 2.912s -0.7% 357.8 MB +0.1% 🟢 Pass
homeassistant pyright 9850 106.471s +1.0% 6094.8 MB +0.2% 🟢 Pass
numpy pyright 356 35.438s -0.5% 1310.0 MB +1.2% 🟢 Pass
pandas pyright 1459 129.425s -2.3% 3657.8 MB +4.6% 🟢 Pass
pytest pyright 243 10.084s -0.6% 853.4 MB +1.3% 🟢 Pass
requests pyright 19 1.959s +1.1% 344.4 MB +0.1% 🟢 Pass
torch pyright 2400 95.759s +1.9% 4723.6 MB +0.8% 🟢 Pass
transformers pyright 2901 109.935s +7.7% 5033.3 MB -4.7% 🟢 Pass

Pyright stats

Package Parsed/bound Checked Find Read Tokenize Parse Imports Bind Check Cycles
ansible 988 583 0.043s 0.077s 0.383s 0.750s 0.217s 0.957s 16.493s 0.000s
click 111 17 0.000s 0.013s 0.127s 0.213s 0.030s 0.257s 1.977s 0.000s
homeassistant 11150 9850 0.513s 0.607s 2.207s 3.427s 1.510s 4.987s 91.083s 0.000s
numpy 605 356 0.010s 0.067s 0.370s 0.727s 0.267s 0.927s 32.847s 0.000s
pandas 1898 1459 0.070s 0.210s 1.113s 2.390s 0.287s 2.487s 121.203s 0.000s
pytest 545 243 0.010s 0.050s 0.277s 0.457s 0.093s 0.610s 8.237s 0.000s
requests 167 19 0.000s 0.013s 0.090s 0.160s 0.050s 0.227s 1.147s 0.000s
torch 3078 2400 0.107s 0.320s 1.397s 2.660s 0.523s 3.960s 82.047s 0.000s
transformers 3536 2901 0.140s 0.410s 2.053s 3.303s 0.793s 8.080s 96.167s 0.000s

@bschnurr

Copy link
Copy Markdown
Member

Replayed TypedDict34 on pinned main (d95fe63b) and with e129a23d integrated. The candidate removes three assert_type mismatches for the same specialization (including a generic T). The different-specialization union stays distinct and the optional-key read still reports reportTypedDictNotRequiredAccess on both revisions. The fixture confirms the join behavior, not a measured runtime speedup.

@bschnurr

Copy link
Copy Markdown
Member

🤖 AI-assisted review posted on behalf of Bill Schnurr (@bschnurr)

The new same-type-arguments check can merge callables that differ in whether an argument is required. That lets a callback be replaced with an incompatible one:

from typing import Generic, NotRequired, TypeVar, TypedDict

T = TypeVar("T")

class Box(TypedDict, Generic[T]):
    callback: T
    left: NotRequired[int]
    right: NotRequired[int]

def wrap(callback: T) -> Box[T]:
    return {"callback": callback}

def required(x: int) -> int:
    return x

def optional(x: int = 0) -> int:
    return x

def test(flag: bool) -> int:
    safe = wrap(optional)
    if flag:
        value = wrap(required)
        value["left"] = 1
    else:
        value = safe
        value["right"] = 1
    value["callback"] = required  # Should be an error
    return safe["callback"]()

print(test(False))  # TypeError: required() missing argument 'x'

On base 317abe4e, the assignment reports reportGeneralTypeIssues because safe permits calling its callback without an argument. Head e129a23d reports no errors. It collapses the two Box specializations to the first one; _isTypedDictTypeArgsSame calls isTypeSame without distinguishing callable defaults. The same issue reproduces with positional-only and keyword-only parameters. Reversing the branches retains the error.

Could the join distinguish callable type arguments with different call behavior, with regression tests for both branch orders? The focused TypedDict and callable-identity tests passed locally.

Classification B: the new join used an insufficiently strict type comparison. Honor callable behavior and related type metadata to preserve distinct type arguments, including nested forms, without reducing type precision. No typeshed change is involved.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@bschnurr

Bill Schnurr (bschnurr) commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

AI-assisted follow-up: pushed 98f84d8c to preserve callable behavior and nested type metadata when comparing generic TypedDict arguments. The previously reported unsafe write is diagnosed again; nested TypedDict/list arguments, both branch orders, a second type argument, and compatible writes are covered too.

Validation: the core build, all 8 evaluator suites (1,462 tests), ESLint, and Prettier passed. A separate merge with main at 7e29199f built and passed 32 focused TypedDict/LiteralString tests. Exact base/head/repair replays confirmed the restored diagnostic locations and union types.

The 12-key growth probe took a median 0.332 s of reported analysis time on both the original head and the repair (five paired runs with the same range-compatible dependencies). This is only a synthetic case, not a representative performance or memory benchmark.

A separate pre-existing general union fast path can still collapse callable defaults when the TypedDict presence states are identical; this occurs on the pre-PR base too and is outside this repair. The PR's previously discussed common-key-presence precision tradeoff is unchanged.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Slow code analysis

3 participants