Skip to content

Commit

Permalink
Fixed bug that resulted in incorrect type evaluation when a TypeAlias…
Browse files Browse the repository at this point in the history
…Type is used in a value expression. This addresses #7109.
  • Loading branch information
erictraut committed Jan 28, 2024
1 parent 928d1cf commit 509bf69
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15302,7 +15302,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
if (isPep695Syntax || isPep695TypeVarType) {
const typeAliasTypeClass = getTypingType(errorNode, 'TypeAliasType');
if (typeAliasTypeClass && isInstantiableClass(typeAliasTypeClass)) {
typeAlias = TypeBase.cloneAsSpecialForm(typeAlias, typeAliasTypeClass);
typeAlias = TypeBase.cloneAsSpecialForm(typeAlias, ClassType.cloneAsInstance(typeAliasTypeClass));
}
}

Expand Down
15 changes: 12 additions & 3 deletions packages/pyright-internal/src/tests/samples/typeAliasStatement1.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

type TA1[T1] = int


class ClassA[T2]:
type TA2 = int; type TA3 = str
type TA2 = int
type TA3 = str

type TA4 = int

Expand Down Expand Up @@ -38,6 +40,7 @@ class ClassA[T2]:
def func1() -> type[int]:
...


# This should generate an error because a call expression is not
# allowed in a type alias definition.
type TA8 = func1()
Expand All @@ -57,19 +60,25 @@ def func1() -> type[int]:

list[TA10]()


# This should generate an error.
class DerivedInt(TA10): pass
class DerivedInt(TA10):
pass


def func2(x: object):
# This should generate an error.
# This should generate two errors.
if isinstance(x, TA10):
reveal_type(x)


type TA11 = Callable[..., Any]


def func3(cb: TA11):
cb()


def func4():
# This should generate an error.
type TA12 = int
7 changes: 7 additions & 0 deletions packages/pyright-internal/src/tests/samples/typeAliasType2.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,10 @@ class A(Generic[T]):

# This should generate an error because S is not in scope.
TA8 = TypeAliasType("TA8", list[S])


def identity[T](t: T) -> T:
return t


reveal_type(identity(TA1), expected_text="TypeAliasType")
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/tests/typeEvaluator5.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ test('TypeAliasStatement1', () => {
configOptions.defaultPythonVersion = PythonVersion.V3_12;

const analysisResults = TestUtils.typeAnalyzeSampleFiles(['typeAliasStatement1.py'], configOptions);
TestUtils.validateResults(analysisResults, 8);
TestUtils.validateResults(analysisResults, 9);
});

test('TypeAliasStatement2', () => {
Expand Down

0 comments on commit 509bf69

Please sign in to comment.