-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests for typevar narrowing changes
- Loading branch information
1 parent
5ea940f
commit 56f2fd5
Showing
4 changed files
with
55 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
packages/pyright-internal/src/tests/samples/typeNarrowingToBounds.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters