Skip to content

Commit

Permalink
Finished cleanup of test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
msfterictraut committed Jun 20, 2023
1 parent b0d4080 commit 43c5fae
Show file tree
Hide file tree
Showing 99 changed files with 519 additions and 496 deletions.
6 changes: 3 additions & 3 deletions packages/pyright-internal/src/tests/samples/typeAlias1.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# This sample tests that type aliasing works.

from typing import Any, Literal, Tuple
from typing import Any, Literal

# Make sure it works with and without forward references.
TupleAlias = Tuple["int", int]
TupleAlias = tuple["int", int]

foo1: Tuple[int, int]
foo1: tuple[int, int]
bar1: TupleAlias

foo1 = (1, 2)
Expand Down
4 changes: 2 additions & 2 deletions packages/pyright-internal/src/tests/samples/typeAlias11.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This sample tests the simple aliasing of a generic class with no
# type arguments.

from typing import Generic, TypeVar, Union
from typing import Generic, TypeVar
import collections
from collections import OrderedDict

Expand Down Expand Up @@ -30,7 +30,7 @@ def __init__(self, x: _T):
TA3[int, int]


TA4 = Union[dict, OrderedDict]
TA4 = dict | OrderedDict

# This should generate two errors because the two types in TA4
# are already specialized.
Expand Down
6 changes: 3 additions & 3 deletions packages/pyright-internal/src/tests/samples/typeAlias12.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# This sample tests the handling of a generic type alias that uses
# a union that collapses to a single type when specialized.

from typing import List, TypeVar, Union
from typing import TypeVar

V = TypeVar("V")
U = TypeVar("U")

Alias = Union[V, U]
Alias = V | U


def fn(x: Alias[V, V]) -> V:
return x


def fn2(x: List[Alias[V, V]]) -> List[V]:
def fn2(x: list[Alias[V, V]]) -> list[V]:
return x


Expand Down
14 changes: 7 additions & 7 deletions packages/pyright-internal/src/tests/samples/typeAlias13.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

Method = Callable[Concatenate[T, P], U]
MaybeMethod = Union[Method[T, P, U], Callable[P, U]]
Coro = Coroutine[Any, Any, T]
MaybeCoro = Union[T, Coro[T]]
CoroFunc = Callable[P, Coro[T]]
CoroMethod = Method[T, P, Coro[U]]
CoroMaybeMethod = Union[CoroMethod[T, P, U], CoroFunc[P, U]]
Co = Coroutine[Any, Any, T]
MaybeCo = Union[T, Co[T]]
CoFunc = Callable[P, Co[T]]
CoMethod = Method[T, P, Co[U]]
CoMaybeMethod = Union[CoMethod[T, P, U], CoFunc[P, U]]


class D:
Expand All @@ -32,7 +32,7 @@ class F:

DT = TypeVar("DT", bound=D)

Error = CoroMaybeMethod[DT, [F, E], Any]
Error = CoMaybeMethod[DT, [F, E], Any]
reveal_type(
Error,
expected_text="type[(DT@Error, F, E) -> Coroutine[Any, Any, Any]] | type[(F, E) -> Coroutine[Any, Any, Any]]",
Expand All @@ -54,7 +54,7 @@ class C:
BT = TypeVar("BT", bound=B)


Something = CoroMaybeMethod[A, [BT, C], Any]
Something = CoMaybeMethod[A, [BT, C], Any]
reveal_type(
Something,
expected_text="type[(A, BT@Something, C) -> Coroutine[Any, Any, Any]] | type[(BT@Something, C) -> Coroutine[Any, Any, Any]]",
Expand Down
10 changes: 5 additions & 5 deletions packages/pyright-internal/src/tests/samples/typeAlias15.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
# This sample tests the case where a generic type alias is specialized
# with an instantiable class rather than a class instance.

from typing import TypeVar, Sequence, Type
from typing import TypeVar, Sequence

_T = TypeVar("_T", bound=Type[Exception])
_MaybeSequence = _T | Sequence[_T]
T = TypeVar("T", bound=type[Exception])
MaybeSequence = T | Sequence[T]


class HttpError(Exception):
pass


def func1(errs: _MaybeSequence[type[Exception]]):
def func1(errs: MaybeSequence[type[Exception]]):
pass


func1(HttpError)
func1(Exception)

reveal_type(
_MaybeSequence[type[HttpError]],
MaybeSequence[type[HttpError]],
expected_text="type[type[HttpError]] | type[Sequence[type[HttpError]]]",
)
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/tests/samples/typeAlias18.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# declaration. We want to ensure that the variance of type variables
# is compatible with the usage within the type alias.

from typing import Callable, Generic, TypeVar, TypeAlias
from typing import Generic, TypeVar, TypeAlias

T1 = TypeVar("T1")
T2 = TypeVar("T2", covariant=True)
Expand Down
6 changes: 3 additions & 3 deletions packages/pyright-internal/src/tests/samples/typeAlias2.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# This sample tests that forward references to type aliases work.

from typing import Dict, Any, Union
from typing import Any, Union


class Base:
@staticmethod
def create(data: Dict[str, Any]) -> "Mix":
def create(data: dict[str, Any]) -> "Mix":
return A()


Expand All @@ -22,5 +22,5 @@ class B(Base):

class S:
@staticmethod
def create(data: Dict[str, Any]) -> "Mix":
def create(data: dict[str, Any]) -> "Mix":
return A()
4 changes: 2 additions & 2 deletions packages/pyright-internal/src/tests/samples/typeAlias3.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

# pyright: strict

from typing import Callable, Generic, Tuple, Optional, TypeVar
from typing import Callable, Generic, Optional, TypeVar
from typing_extensions import ParamSpec

T = TypeVar("T")
P = ParamSpec("P")

ValidationResult = Tuple[bool, Optional[T]]
ValidationResult = tuple[bool, Optional[T]]


def foo() -> ValidationResult[str]:
Expand Down
12 changes: 6 additions & 6 deletions packages/pyright-internal/src/tests/samples/typeAlias5.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
# TypeVars.

from datetime import datetime
from typing import IO, Dict, Generic, List, Literal, Type, TypeVar, Union
from typing import IO, Generic, Literal, TypeVar, Union

_T1 = TypeVar("_T1")
_T2 = TypeVar("_T2")

MyUnion1 = Union[int, _T1, str, _T2, List[_T1]]
MyUnion1 = Union[int, _T1, str, _T2, list[_T1]]

MyUnion2 = Union[float, datetime]

Expand All @@ -24,7 +24,7 @@
MyUnion6 = MyUnion1[Literal[0], Literal["a"]]
reveal_type(
MyUnion6,
expected_text="type[int] | type[str] | type[List[Literal[0]]] | type[Literal[0, 'a']]",
expected_text="type[int] | type[str] | type[list[Literal[0]]] | type[Literal[0, 'a']]",
)


Expand All @@ -46,13 +46,13 @@ def __int__(self) -> int:
v2: FooIsh[Bar] = Bar()

# This should generate an error.
v3: FooIsh[Type[Bar]] = 42
v3: FooIsh[type[Bar]] = 42


MyTypeAlias = Dict[_T1, _T2]
MyTypeAlias = dict[_T1, _T2]


class MyClass1(Generic[_T1, _T2]):
# This should generate an error because S and T are bound
# type variables.
MyTypeAlias = Dict[_T1, _T2]
MyTypeAlias = dict[_T1, _T2]
8 changes: 4 additions & 4 deletions packages/pyright-internal/src/tests/samples/typeAlias6.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# This sample tests that certain type aliases cannot be used within
# call expressions.

from typing import Callable, Optional, Tuple, Type, TypeVar, Union
from typing import Callable, TypeVar


T_Union = Union[int, float]
T_Union = int | float

# This should generate an error
T_Union(3)
Expand All @@ -15,15 +15,15 @@
T_Callable(1)


T_Type1 = Type[int]
T_Type1 = type[int]

# This should generate an error
T_Type1(object)

T_Type2 = type
T_Type2(object)

T_Optional = Optional[str]
T_Optional = str | None

# This should generate an error
T_Optional(3)
Expand Down
1 change: 0 additions & 1 deletion packages/pyright-internal/src/tests/samples/typeAlias7.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from typing import Awaitable, Callable, Generic, TypeVar


TSource = TypeVar("TSource")
TError = TypeVar("TError")
TResult = TypeVar("TResult")
Expand Down
8 changes: 4 additions & 4 deletions packages/pyright-internal/src/tests/samples/typeAlias8.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
F = Callable[[T], T]


def f() -> F[T]:
def func1() -> F[T]:
def g(x: T) -> T:
...

return g


g = f()
v1 = g("foo")
func2 = func1()
v1 = func2("foo")
reveal_type(v1, expected_text="str")

v2 = g(1)
v2 = func2(1)
reveal_type(v2, expected_text="int")
14 changes: 7 additions & 7 deletions packages/pyright-internal/src/tests/samples/typeAlias9.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,33 @@

# pyright: reportUnknownParameterType=true, reportMissingTypeArgument=false

from typing import Dict, List, TypeVar
from typing import TypeVar

T = TypeVar("T")
Foo = List[T]
TA1 = list[T]


# This should generate an error because Foo is missing a type argument,
# so the type of `f` is partially unknown.
def foo1(f: Foo) -> None:
def func1(f: TA1) -> None:
pass


Bar = Foo
TA2 = TA1


# This should generate an error because Bar doesn't specialize
# Foo appropriately.
def foo2(f: Bar) -> None:
def func2(f: TA2) -> None:
pass


K = TypeVar("K")
V = TypeVar("V")

Baz = Dict[K, V]
TA3 = dict[K, V]


# This should generate two errors because Baz is only partially specialized.
def foo3(f: Baz[int]) -> None:
def func3(f: TA3[int]) -> None:
pass
18 changes: 9 additions & 9 deletions packages/pyright-internal/src/tests/samples/typeGuard1.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,34 @@
# pyright: reportMissingModuleSource=false

import os
from typing import Any, List, Tuple, TypeVar, Union
from typing import Any, TypeVar
from typing_extensions import TypeGuard

_T = TypeVar("_T")


def is_two_element_tuple(a: Tuple[_T, ...]) -> TypeGuard[Tuple[_T, _T]]:
def is_two_element_tuple(a: tuple[_T, ...]) -> TypeGuard[tuple[_T, _T]]:
return True


def func1(a: Tuple[int, ...]):
def func1(a: tuple[int, ...]):
if is_two_element_tuple(a):
reveal_type(a, expected_text="Tuple[int, int]")
reveal_type(a, expected_text="tuple[int, int]")
else:
reveal_type(a, expected_text="Tuple[int, ...]")
reveal_type(a, expected_text="tuple[int, ...]")


def is_string_list(val: List[Any], allow_zero_entries: bool) -> TypeGuard[List[str]]:
def is_string_list(val: list[Any], allow_zero_entries: bool) -> TypeGuard[list[str]]:
if allow_zero_entries and len(val) == 0:
return True
return all(isinstance(x, str) for x in val)


def func2(a: List[Union[str, int]]):
def func2(a: list[str | int]):
if is_string_list(a, True):
reveal_type(a, expected_text="List[str]")
reveal_type(a, expected_text="list[str]")
else:
reveal_type(a, expected_text="List[str | int]")
reveal_type(a, expected_text="list[str | int]")


# This should generate an error because TypeGuard
Expand Down
Loading

0 comments on commit 43c5fae

Please sign in to comment.