8000 Extended support for union expansion of constrained type variables wh… · codean-io/scip-python@eaac1d3 · GitHub
[go: up one dir, main page]

Skip to content

Commit eaac1d3

Browse files
committed
Extended support for union expansion of constrained type variables when performing overload matching.
1 parent ed13daf commit eaac1d3

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

packages/pyright-internal/src/analyzer/typeEvaluator.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7770,12 +7770,15 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
77707770
return undefined;
77717771
}
77727772

7773-
let unionToExpand: UnionType | undefined;
7773+
let unionToExpand: Type | undefined;
77747774
while (indexToExpand < contextFreeArgTypes.length) {
77757775
// Is this a union type? If so, we can expand it.
77767776
const argType = contextFreeArgTypes[indexToExpand];
77777777
if (isUnion(argType)) {
7778-
unionToExpand = argType;
7778+
unionToExpand = makeTopLevelTypeVarsConcrete(argType);
7779+
break;
7780+
} else if (isTypeVar(argType) && argType.details.constraints.length > 1) {
7781+
unionToExpand = makeTopLevelTypeVarsConcrete(argType);
77797782
break;
77807783
}
77817784
indexToExpand++;

packages/pyright-internal/src/tests/samples/overload8.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,24 @@ def func2(a: LargeUnion, b: Literal[2, 3, 4], c: Literal[2, 3, 4, 9, 10]):
8787
# will exceed the max number of expansions (64).
8888
v3 = overloaded2(a, c)
8989
reveal_type(v2, expected_text="str | float")
90+
91+
92+
_T2 = TypeVar("_T2", str, bytes)
93+
94+
95+
@overload
96+
def overloaded3(x: str) -> str:
97+
...
98+
99+
100+
@overload
101+
def overloaded3(x: bytes) -> bytes:
102+
...
103+
104+
105+
def overloaded3(x: str | bytes) -> str | bytes:
106+
...
107+
108+
109+
def func3(y: _T2):
110+
overloaded3(y)

0 commit comments

Comments
 (0)
0