Skip to content

Commit

Permalink
Fixed a bug that resulted in an incorrect type evaluation when a Type…
Browse files Browse the repository at this point in the history
…Var with a default (PEP 696) was used in an overload but was not solved. This addresses microsoft#7173. (microsoft#7174)
  • Loading branch information
erictraut committed Feb 1, 2024
1 parent e01b0fe commit a81633d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
1 change: 0 additions & 1 deletion packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11385,7 +11385,6 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
let specializedReturnType = applySolvedTypeVars(returnType, typeVarContext, {
unknownIfNotFound,
unknownExemptTypeVars: getUnknownExemptTypeVarsForReturnType(type, returnType),
useUnknownOverDefault: skipUnknownArgCheck,
eliminateUnsolvedInUnions,
applyInScopePlaceholders: true,
});
Expand Down
3 changes: 1 addition & 2 deletions packages/pyright-internal/src/analyzer/typeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ export const enum AssignTypeFlags {
export interface ApplyTypeVarOptions {
unknownIfNotFound?: boolean;
unknownExemptTypeVars?: TypeVarType[];
useUnknownOverDefault?: boolean;
useNarrowBoundOnly?: boolean;
eliminateUnsolvedInUnions?: boolean;
typeClassType?: Type;
Expand Down Expand Up @@ -4226,7 +4225,7 @@ class ApplySolvedTypeVarsTransformer extends TypeVarTransformer {

if (useDefaultOrUnknown) {
// Use the default value if there is one.
if (typeVar.details.defaultType && !this._options.useUnknownOverDefault) {
if (typeVar.details.defaultType) {
return this._solveDefaultType(typeVar.details.defaultType, recursionCount);
}

Expand Down
13 changes: 13 additions & 0 deletions packages/pyright-internal/src/tests/samples/typeVarDefault5.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# with a constructor that defines an __init__ but no __new__.

from dataclasses import dataclass
from typing import Any, overload

class ClassA: ...

Expand All @@ -12,3 +13,15 @@ class ClassB[T: ClassA = ClassA]:
def post_comment[T: ClassA](owner: T) -> ClassB[T]:
return ClassB(owner)


class ClassC: ...


@overload
def func1(x: ClassA) -> ClassA: ...
@overload
def func1[T1 = str](x: ClassC | T1) -> T1: ...
def func1(x: Any) -> Any: ...


reveal_type(func1(ClassC()), expected_text="str")

0 comments on commit a81633d

Please sign in to comment.