From 5f82eecd024170336bc41d6f9a46b63026d68b35 Mon Sep 17 00:00:00 2001 From: withkarann <31102185+withkarann@users.noreply.github.com> Date: Fri, 9 Oct 2026 03:10:22 +0200 Subject: [PATCH 1/2] Fix exponential union growth when narrowing generic TypedDicts 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 #11228. --- .../pyright-internal/src/analyzer/types.ts | 21 +++++- .../src/tests/samples/typedDict34.py | 74 +++++++++++++++++++ .../src/tests/typeEvaluator7.test.ts | 6 ++ 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 packages/pyright-internal/src/tests/samples/typedDict34.py diff --git a/packages/pyright-internal/src/analyzer/types.ts b/packages/pyright-internal/src/analyzer/types.ts index 1e837c88bfff..a18361809cb6 100644 --- a/packages/pyright-internal/src/analyzer/types.ts +++ b/packages/pyright-internal/src/analyzer/types.ts @@ -4235,7 +4235,7 @@ function _addTypeIfUnique(unionType: UnionType, typeToAdd: UnionableType, elideR // existing type, see if one of them is a proper subset of the other. if (ClassType.isTypedDictClass(type) && ClassType.isSameGenericClass(type, typeToAdd)) { // Do not proceed if the TypedDicts are generic and have different type arguments. - if (!type.priv.typeArgs && !typeToAdd.priv.typeArgs) { + if (_isTypedDictTypeArgsSame(type, typeToAdd)) { if (ClassType.isTypedDictNarrower(typeToAdd, type)) { return; } else if (ClassType.isTypedDictNarrower(type, typeToAdd)) { @@ -4263,3 +4263,22 @@ function _addTypeIfUnique(unionType: UnionType, typeToAdd: UnionableType, elideR UnionType.addType(unionType, typeToAdd); } + +// Determines whether two TypedDict classes have the same type arguments. +// Narrowed copies of a generic TypedDict share their type args, so they +// can be combined like those of a non-generic TypedDict. Without this, +// each "in" or assignment narrowing doubles the size of the union. +function _isTypedDictTypeArgsSame(type1: ClassType, type2: ClassType): boolean { + const typeArgs1 = type1.priv.typeArgs; + const typeArgs2 = type2.priv.typeArgs; + + if (typeArgs1 === typeArgs2) { + return true; + } + + if (!typeArgs1 || !typeArgs2 || typeArgs1.length !== typeArgs2.length) { + return false; + } + + return typeArgs1.every((typeArg, index) => isTypeSame(typeArg, typeArgs2[index])); +} diff --git a/packages/pyright-internal/src/tests/samples/typedDict34.py b/packages/pyright-internal/src/tests/samples/typedDict34.py new file mode 100644 index 000000000000..101056dbf9a8 --- /dev/null +++ b/packages/pyright-internal/src/tests/samples/typedDict34.py @@ -0,0 +1,74 @@ +# This sample tests that narrowed forms of a generic TypedDict with the +# same type arguments are combined at control flow join points, as they +# are for a non-generic TypedDict. + +from typing import TypedDict, assert_type + + +class TD1[T](TypedDict, total=False): + a: T + b: T + c: T + d: T + + +def func1(val: int | None) -> TD1[int]: + td: TD1[int] = {} + if val is not None: + td["a"] = val + if val is not None: + td["b"] = val + if val is not None: + td["c"] = val + if val is not None: + td["d"] = val + + assert_type(td, TD1[int]) + return td + + +def func2(val: bool) -> None: + td: TD1[int] = {} + if val: + td = {"a": 1, "b": 2} + else: + td = {"a": 1, "c": 3} + + assert_type(td, TD1[int]) + + +def func3(val: bool) -> None: + td: TD1[int] | TD1[str] + if val: + td = TD1[int](a=1) + else: + td = TD1[str](a="") + + assert_type(td, TD1[int] | TD1[str]) + + +def func4[T](val: T, cond: bool) -> TD1[T]: + td: TD1[T] = {} + if cond: + td["a"] = val + if cond: + td["b"] = val + + assert_type(td, TD1[T]) + return td + + +def func5(val: bool) -> None: + td: TD1[int] = {} + if val: + td["a"] = 1 + + # This should generate an error because "a" may not be present. + td["a"] + + if val: + td["b"] = 1 + else: + td["b"] = 2 + + td["b"] diff --git a/packages/pyright-internal/src/tests/typeEvaluator7.test.ts b/packages/pyright-internal/src/tests/typeEvaluator7.test.ts index 2f9125b66766..36eac87339c8 100644 --- a/packages/pyright-internal/src/tests/typeEvaluator7.test.ts +++ b/packages/pyright-internal/src/tests/typeEvaluator7.test.ts @@ -1294,6 +1294,12 @@ test('TypedDict30', () => { TestUtils.validateResults(analysisResults, 0); }); +test('TypedDict34', () => { + const analysisResults = TestUtils.typeAnalyzeSampleFiles(['typedDict34.py']); + + TestUtils.validateResults(analysisResults, 1); +}); + test('ClassVar1', () => { const analysisResults = TestUtils.typeAnalyzeSampleFiles(['classVar1.py']); From 98f84d8c52ec5b4196a4788aba6e162bd75ef049 Mon Sep 17 00:00:00 2001 From: Bill Schnurr Date: Fri, 20 Nov 2026 12:24:08 -0700 Subject: [PATCH 2/2] Preserve callable identity in generic TypedDict joins 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> --- .../pyright-internal/src/analyzer/types.ts | 9 +- .../tests/samples/typedDictCallableJoin1.py | 175 ++++++++++++++++++ .../src/tests/typeEvaluator7.test.ts | 21 +++ 3 files changed, 204 insertions(+), 1 deletion(-) create mode 100644 packages/pyright-internal/src/tests/samples/typedDictCallableJoin1.py diff --git a/packages/pyright-internal/src/analyzer/types.ts b/packages/pyright-internal/src/analyzer/types.ts index 22c009344146..16301c310043 100644 --- a/packages/pyright-internal/src/analyzer/types.ts +++ b/packages/pyright-internal/src/analyzer/types.ts @@ -4546,7 +4546,14 @@ function _isTypedDictTypeArgsSame(type1: ClassType, type2: ClassType): boolean { return false; } - return typeArgs1.every((typeArg, index) => isTypeSame(typeArg, typeArgs2[index])); + return typeArgs1.every((typeArg, index) => + isTypeSame(typeArg, typeArgs2[index], { + honorIsTypeArgExplicit: true, + honorTypeForm: true, + honorCallBehavior: true, + honorTypedDictNarrowedValues: true, + }) + ); } function _addLiteralTypeIfUnique( diff --git a/packages/pyright-internal/src/tests/samples/typedDictCallableJoin1.py b/packages/pyright-internal/src/tests/samples/typedDictCallableJoin1.py new file mode 100644 index 000000000000..b2ea7dd4e199 --- /dev/null +++ b/packages/pyright-internal/src/tests/samples/typedDictCallableJoin1.py @@ -0,0 +1,175 @@ +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 positional_required(x: int, /) -> int: + return x + + +def positional_optional(x: int = 0, /) -> int: + return x + + +def keyword_required(*, x: int) -> int: + return x + + +def keyword_optional(*, x: int = 0) -> int: + return x + + +def standard(flag: bool) -> int: + safe = wrap(optional) + if flag: + value = wrap(required) + value["left"] = 1 + else: + value = safe + value["right"] = 1 + value["callback"] = required # This should generate an error. + return safe["callback"]() + + +def positional(flag: bool) -> int: + safe = wrap(positional_optional) + if flag: + value = wrap(positional_required) + value["left"] = 1 + else: + value = safe + value["right"] = 1 + value["callback"] = positional_required # This should generate an error. + return safe["callback"]() + + +def keyword(flag: bool) -> int: + safe = wrap(keyword_optional) + if flag: + value = wrap(keyword_required) + value["left"] = 1 + else: + value = safe + value["right"] = 1 + value["callback"] = keyword_required # This should generate an error. + return safe["callback"]() + + +def reverse_order(flag: bool) -> int: + safe = wrap(optional) + if flag: + value = safe + value["left"] = 1 + else: + value = wrap(required) + value["right"] = 1 + value["callback"] = required # This should generate an error. + return safe["callback"]() + + +def compatible(flag: bool) -> int: + safe = wrap(optional) + if flag: + value = wrap(required) + value["left"] = 1 + else: + value = safe + value["right"] = 1 + value["callback"] = optional + return safe["callback"]() + + +def nested_box(flag: bool) -> int: + safe = wrap(wrap(optional)) + if flag: + value = wrap(wrap(required)) + value["left"] = 1 + else: + value = safe + value["right"] = 1 + reveal_type(value, expected_text="Box[Box[(x: int) -> int]] | Box[Box[(x: int = 0) -> int]]") + value["callback"] = wrap(required) # This should generate an error. + return safe["callback"]["callback"]() + + +def nested_box_reverse_order(flag: bool) -> int: + safe = wrap(wrap(optional)) + if flag: + value = safe + value["left"] = 1 + else: + value = wrap(wrap(required)) + value["right"] = 1 + value["callback"] = wrap(required) # This should generate an error. + return safe["callback"]["callback"]() + + +def nested_list(flag: bool) -> int: + safe = wrap([optional]) + if flag: + value = wrap([required]) + value["left"] = 1 + else: + value = safe + value["right"] = 1 + reveal_type(value, expected_text="Box[list[(x: int) -> int]] | Box[list[(x: int = 0) -> int]]") + value["callback"] = [required] # This should generate an error. + return safe["callback"][0]() + + +def nested_compatible(flag: bool) -> int: + safe = wrap(wrap(optional)) + if flag: + value = wrap(wrap(required)) + value["left"] = 1 + else: + value = safe + value["right"] = 1 + value["callback"] = wrap(optional) + return safe["callback"]["callback"]() + + +U = TypeVar("U") + + +class Pair(TypedDict, Generic[T, U]): + first: T + second: U + left: NotRequired[int] + right: NotRequired[int] + + +def pair(first: T, second: U) -> Pair[T, U]: + return {"first": first, "second": second} + + +def second_type_argument(flag: bool) -> int: + safe = pair(1, optional) + if flag: + value = pair(1, required) + value["left"] = 1 + else: + value = safe + value["right"] = 1 + reveal_type(value, expected_text="Pair[int, (x: int) -> int] | Pair[int, (x: int = 0) -> int]") + value["second"] = required # This should generate an error. + return safe["second"]() diff --git a/packages/pyright-internal/src/tests/typeEvaluator7.test.ts b/packages/pyright-internal/src/tests/typeEvaluator7.test.ts index 5d177bd5a014..0a97a2114ddd 100644 --- a/packages/pyright-internal/src/tests/typeEvaluator7.test.ts +++ b/packages/pyright-internal/src/tests/typeEvaluator7.test.ts @@ -2059,6 +2059,27 @@ test('TypedDict34', () => { TestUtils.validateResults(analysisResults, 1); }); +test('TypedDictCallableJoin1', () => { + const analysisResults = TestUtils.typeAnalyzeSampleFiles(['typedDictCallableJoin1.py']); + + TestUtils.validateResults(analysisResults, 8); + expect( + analysisResults[0].errors + .slice() + .sort((a, b) => a.range.start.line - b.range.start.line) + .map((diagnostic) => [diagnostic.range.start.line + 1, diagnostic.getRule()]) + ).toEqual([ + [49, DiagnosticRule.reportGeneralTypeIssues], + [61, DiagnosticRule.reportGeneralTypeIssues], + [73, DiagnosticRule.reportGeneralTypeIssues], + [85, DiagnosticRule.reportGeneralTypeIssues], + [110, DiagnosticRule.reportGeneralTypeIssues], + [122, DiagnosticRule.reportGeneralTypeIssues], + [135, DiagnosticRule.reportGeneralTypeIssues], + [174, DiagnosticRule.reportGeneralTypeIssues], + ]); +}); + test('ClassVar1', () => { const analysisResults = TestUtils.typeAnalyzeSampleFiles(['classVar1.py']);