Skip to content

Commit

Permalink
Fixed a bug that led to a false positive error under certain circumst…
Browse files Browse the repository at this point in the history
…ances when a literal type argument was used in conjunction with a protocol that used a covariant type parameter and an implementation of that protocol that used an invariant type parameter. This addresses microsoft#5282. (microsoft#5332)

Co-authored-by: Eric Traut <erictr@microsoft.com>
  • Loading branch information
erictraut and msfterictraut authored Jun 18, 2023
1 parent 2b0f8d2 commit e3080b1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
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 @@ -21780,7 +21780,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
assignmentDiag,
destTypeVarContext,
srcTypeVarContext,
flags | AssignTypeFlags.EnforceInvariance,
flags | AssignTypeFlags.EnforceInvariance | AssignTypeFlags.RetainLiteralsForTypeVar,
recursionCount
)
) {
Expand Down
28 changes: 28 additions & 0 deletions packages/pyright-internal/src/tests/samples/genericTypes119.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This sample tests the case where a protocol uses a covariant
# type parameter but a corresponding implementation uses an
# invariant type parameter. Literal types need to be handled
# carefully in this case.

from typing import Awaitable, Literal, TypeVar

_T = TypeVar("_T")


class Future(Awaitable[_T]):
...


def func1(future: Future[_T]) -> Future[_T]:
...


def func2(coro: Awaitable[_T]) -> Future[_T]:
...


def func3() -> Awaitable[Literal[True]]:
...


v1 = func1(func2(func3()))
reveal_type(v1, expected_text="Future[Literal[True]]")
6 changes: 6 additions & 0 deletions packages/pyright-internal/src/tests/typeEvaluator2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,12 @@ test('GenericTypes118', () => {
TestUtils.validateResults(analysisResults, 0);
});

test('GenericTypes119', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['genericTypes119.py']);

TestUtils.validateResults(analysisResults, 0);
});

test('Protocol1', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['protocol1.py']);

Expand Down

0 comments on commit e3080b1

Please sign in to comment.