diff --git a/packages/pyright-internal/src/analyzer/types.ts b/packages/pyright-internal/src/analyzer/types.ts index f618c16504ce..16301c310043 100644 --- a/packages/pyright-internal/src/analyzer/types.ts +++ b/packages/pyright-internal/src/analyzer/types.ts @@ -4501,7 +4501,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)) { @@ -4530,6 +4530,32 @@ 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], { + honorIsTypeArgExplicit: true, + honorTypeForm: true, + honorCallBehavior: true, + honorTypedDictNarrowedValues: true, + }) + ); +} + function _addLiteralTypeIfUnique( unionType: UnionType, literalMap: Map, 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/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 105ee26d28b3..0a97a2114ddd 100644 --- a/packages/pyright-internal/src/tests/typeEvaluator7.test.ts +++ b/packages/pyright-internal/src/tests/typeEvaluator7.test.ts @@ -2053,6 +2053,33 @@ test('TypedDict30', () => { TestUtils.validateResults(analysisResults, 0); }); +test('TypedDict34', () => { + const analysisResults = TestUtils.typeAnalyzeSampleFiles(['typedDict34.py']); + + 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']);