Skip to content

Slow contextual checking of nested literals against recursive TypedDict unions #3663

@sunny-zuo

Description

@sunny-zuo

Summary

On ty 0.0.43, checking a nested dictionary/list literal against a recursive union of discriminated TypedDict types takes several seconds. The minimal reproducer here takes ~2.1s on my machine; the real application code this was based off of takes ~5.5s to type check. This seems possibly related to #2026, but does not use Pydantic.

Reproducer

from typing import Literal, TypedDict


class A(TypedDict): type: Literal["a"]; children: list["Node"]
class B(TypedDict): type: Literal["b"]; children: list["Node"]
class C(TypedDict): type: Literal["c"]; children: list["Node"]
class D(TypedDict): type: Literal["d"]; children: list["Node"]
class E(TypedDict): type: Literal["e"]; children: list["Node"]
class F(TypedDict): type: Literal["f"]; children: list["Node"]
class G(TypedDict): type: Literal["g"]; children: list["Node"]
class H(TypedDict): type: Literal["h"]; children: list["Node"]
class I(TypedDict): type: Literal["i"]; children: list["Node"]
class J(TypedDict): type: Literal["j"]; children: list["Node"]
class K(TypedDict): type: Literal["k"]; children: list["Node"]
class Leaf(TypedDict): type: Literal["leaf"]; text: str


type Node = A | B | C | D | E | F | G | H | I | J | K | Leaf


value: list[Node] = [
    {"type": "a", "children": [
        {"type": "b", "children": [
            {"type": "c", "children": [{"type": "leaf", "text": "x"}]},
            {"type": "d", "children": [{"type": "leaf", "text": "y"}]},
        ]},
        {"type": "e", "children": [
            {"type": "f", "children": [{"type": "leaf", "text": "z"}]},
            {"type": "g", "children": [{"type": "leaf", "text": "w"}]},
        ]},
    ]},
]

Performance

The time taken scales with the number of node variants, even if value itself doesn't change. This seems to have gotten slightly slower since 0.0.38.

Node variants ty 0.0.38 ty 0.0.43
12 0.58s 2.18s
16 1.90s 10.24s
20 4.16s 26.14s
24 8.36s >60s timeout

Version

ty 0.0.43

Metadata

Metadata

Assignees

Labels

performancePotential performance improvement

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions