From 56f2fd537389938c910e9c5088c3e03c2cebe450 Mon Sep 17 00:00:00 2001 From: detachhead Date: Sun, 6 Oct 2024 21:40:25 +1000 Subject: [PATCH] add tests for typevar narrowing changes --- .../samples/typeNarrowingIsinstance13.py | 4 +- .../tests/samples/typeNarrowingIsinstance6.py | 2 +- .../tests/samples/typeNarrowingToBounds.py | 43 +++++++++++++++++++ .../src/tests/typeEvaluatorBased.test.ts | 9 ++++ 4 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 packages/pyright-internal/src/tests/samples/typeNarrowingToBounds.py diff --git a/packages/pyright-internal/src/tests/samples/typeNarrowingIsinstance13.py b/packages/pyright-internal/src/tests/samples/typeNarrowingIsinstance13.py index 5715c6cafc..b979d0229a 100644 --- a/packages/pyright-internal/src/tests/samples/typeNarrowingIsinstance13.py +++ b/packages/pyright-internal/src/tests/samples/typeNarrowingIsinstance13.py @@ -8,8 +8,8 @@ def func1(v: Any) -> bool: if isinstance(v, Iterable): - reveal_type(v, expected_text="Iterable[Unknown]") + reveal_type(v, expected_text="Iterable[object]") if isinstance(v, Sized): - reveal_type(v, expected_text="") + reveal_type(v, expected_text="") return True return False diff --git a/packages/pyright-internal/src/tests/samples/typeNarrowingIsinstance6.py b/packages/pyright-internal/src/tests/samples/typeNarrowingIsinstance6.py index 898a5d663f..54178f382f 100644 --- a/packages/pyright-internal/src/tests/samples/typeNarrowingIsinstance6.py +++ b/packages/pyright-internal/src/tests/samples/typeNarrowingIsinstance6.py @@ -51,7 +51,7 @@ class ChildB1(ParentB[_T2]): def func4(var: ParentB[int]): if isinstance(var, ChildB1): - reveal_type(var, expected_text="ChildB1[int]") + reveal_type(var, expected_text="") def func5(var: ParentB[Any]): diff --git a/packages/pyright-internal/src/tests/samples/typeNarrowingToBounds.py b/packages/pyright-internal/src/tests/samples/typeNarrowingToBounds.py new file mode 100644 index 0000000000..0175125228 --- /dev/null +++ b/packages/pyright-internal/src/tests/samples/typeNarrowingToBounds.py @@ -0,0 +1,43 @@ +from typing import Never, assert_type, Iterable + + +class Covariant[T]: + def foo(self, other: object): + if isinstance(other, Covariant): + assert_type(other, Covariant[object]) + + def bar(self) -> T: ... + +class CovariantByDefault[T]: + """by default if there are no usages of a type var on a class, it's treated as covariant. + imo this should be an error. see https://github.com/DetachHead/basedpyright/issues/744""" + def foo(self, other: object): + if isinstance(other, CovariantByDefault): + assert_type(other, CovariantByDefault[object]) + +class CovariantWithBound[T: int | str]: + def foo(self, other: object): + if isinstance(other, CovariantWithBound): + assert_type(other, CovariantWithBound[int | str]) + + def bar(self) -> T: ... + +class Contravariant[T]: + def foo(self, other: object): + if isinstance(other, Contravariant): + assert_type(other, Contravariant[Never]) + + def bar(self, other: T): ... + +class ContravariantWithBound[T: int | str]: + def foo(self, other: object): + if isinstance(other, ContravariantWithBound): + assert_type(other, ContravariantWithBound[Never]) + + def bar(self, other: T): ... + + +def foo(value: object): + match value: + case Iterable(): + assert_type(value, Iterable[object]) \ No newline at end of file diff --git a/packages/pyright-internal/src/tests/typeEvaluatorBased.test.ts b/packages/pyright-internal/src/tests/typeEvaluatorBased.test.ts index 6070fba6f3..c227860514 100644 --- a/packages/pyright-internal/src/tests/typeEvaluatorBased.test.ts +++ b/packages/pyright-internal/src/tests/typeEvaluatorBased.test.ts @@ -116,3 +116,12 @@ test('subscript context manager types on 3.8', () => { ], }); }); + +test('narrowing type vars to their bounds', () => { + const configOptions = new ConfigOptions(Uri.empty()); + configOptions.diagnosticRuleSet.reportUnusedParameter = 'none'; + const analysisResults = typeAnalyzeSampleFiles(['typeNarrowingToBounds.py'], configOptions); + validateResultsButBased(analysisResults, { + errors: [], + }); +});