Skip to content

Commit

Permalink
Next batch of test case cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
msfterictraut committed Jun 20, 2023
1 parent efea9b9 commit b0d4080
Show file tree
Hide file tree
Showing 93 changed files with 398 additions and 413 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This sample tests type checking for set comprehensions.

from typing import Generator, Set
from typing import Generator

a = [1, 2, 3, 4]

Expand All @@ -10,12 +10,12 @@ def func1() -> Generator[int, None, None]:
return b


def func2() -> Set[int]:
def func2() -> set[int]:
c = {elem for elem in a}
return c


def func3() -> Set[str]:
def func3() -> set[str]:
c = {elem for elem in a}

# This should generate an error because
Expand Down
8 changes: 4 additions & 4 deletions packages/pyright-internal/src/tests/samples/protocol1.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This sample tests the type checker's handling of generic protocol types.

from typing import Generic, List, Optional, TypeVar, Protocol
from typing import Generic, TypeVar, Protocol

T = TypeVar("T")
T_co = TypeVar("T_co", covariant=True)
Expand Down Expand Up @@ -73,16 +73,16 @@ class NotProto2:

# This should generate an error because "Protocol" cannot be used
# as a type argument.
var2: List[Protocol] = []
var2: list[Protocol] = []


class Abstract1(Protocol[T_contra]):
def do(self, x: Optional[T_contra]):
def do(self, x: T_contra | None):
...


class Concrete1:
def do(self, x: Optional[int]):
def do(self, x: int | None):
pass


Expand Down
11 changes: 3 additions & 8 deletions packages/pyright-internal/src/tests/samples/protocol13.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,16 @@
# include named-only parameters that match to positional parameters
# within class that is being tested for protocol compatibility.

from typing import Optional, Protocol
from typing import Protocol


class CollectionProtocol(Protocol):
def watch(
self,
*,
max_time: Optional[int] = ...,
key: Optional[str] = ...,
) -> None:
def watch(self, *, max_time: int | None = ..., key: str | None = ...) -> None:
...


class Collection:
def watch(self, key: Optional[str] = None, max_time: Optional[int] = None) -> None:
def watch(self, key: str | None = None, max_time: int | None = None) -> None:
...


Expand Down
5 changes: 3 additions & 2 deletions packages/pyright-internal/src/tests/samples/protocol17.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This sample tests for generic protocol variance consistency.

from typing import Protocol, TypeVar, Union
from typing import Protocol, TypeVar
from typing_extensions import ParamSpec

# pyright: strict
Expand All @@ -13,7 +13,7 @@


class Protocol1(Protocol[_T1, _T2, _T3]):
def m1(self, p0: _T1, p1: _T2, p2: _T3) -> Union[_T1, _T2]:
def m1(self, p0: _T1, p1: _T2, p2: _T3) -> _T1 | _T2:
...

def m2(self) -> _T1:
Expand Down Expand Up @@ -93,6 +93,7 @@ class Protocol9(Protocol[_T1_co]):
def prop1(self) -> _T1_co:
...


class Protocol10(Protocol[_T1_co]):
def m1(self) -> type[_T1_co]:
...
6 changes: 3 additions & 3 deletions packages/pyright-internal/src/tests/samples/protocol2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# generic protocols with invariant, constrained, and contravariant
# type arguments.

from typing import Optional, TypeVar, Protocol
from typing import TypeVar, Protocol


T = TypeVar("T")
Expand All @@ -24,11 +24,11 @@ def f(writer: Writer[bytes]):
pass


def g(writer: Writer[T], v: Optional[T] = None):
def g(writer: Writer[T], v: T | None = None):
pass


def h(writer: Writer[StrLike], v: Optional[StrLike] = None):
def h(writer: Writer[StrLike], v: StrLike | None = None):
pass


Expand Down
4 changes: 2 additions & 2 deletions packages/pyright-internal/src/tests/samples/protocol20.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This sample tests the case where a TypeVar is bound to a
# protocol class.

from typing import Protocol, Type, TypeVar
from typing import Protocol, TypeVar


class ClsProtocol(Protocol):
Expand All @@ -14,7 +14,7 @@ def __init__(self):

class Sample:
@classmethod
def test(cls: Type[T1]) -> T1:
def test(cls: type[T1]) -> T1:
return cls()


Expand Down
4 changes: 2 additions & 2 deletions packages/pyright-internal/src/tests/samples/protocol21.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# to indicate a read-only attribute. It also tests that a member access through
# a protocol class (not an instance) is flagged as an error.

from typing import Protocol, Type
from typing import Protocol


class A(Protocol):
Expand All @@ -15,7 +15,7 @@ class B:
name: str


def do_something(a: A, class_a: Type[A]) -> None:
def do_something(a: A, class_a: type[A]) -> None:
val1 = a.name
reveal_type(val1, expected_text="str")

Expand Down
10 changes: 5 additions & 5 deletions packages/pyright-internal/src/tests/samples/protocol22.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# of multiple type variables is treated as covariant with the
# union type, thus affecting the variance restriction.

from typing import Protocol, Tuple, TypeVar, Union
from typing import Protocol, TypeVar

# pyright: strict

Expand All @@ -18,26 +18,26 @@
# This is right, as `_T1_co` and `_T2_co` are only covariant with
# return type.
class P1(Protocol[_T1_co, _T2_co]):
def m1(self) -> Union[_T1_co, _T2_co]:
def m1(self) -> _T1_co | _T2_co:
...


# This is right, as `_T1_contra` and `_T2_contra` are only covariant
# with the argument type.
class P2(Protocol[_T1_contra, _T2_contra]):
def m1(self, a: Union[_T1_contra, _T2_contra]) -> None:
def m1(self, a: _T1_contra | _T2_contra) -> None:
...


# This is right, as `_T1` and `_T2` are both covariant with the
# argument type and the return type.
class P3(Protocol[_T1, _T2]):
def m1(self, a: _T1, b: _T2) -> Union[_T1, _T2]:
def m1(self, a: _T1, b: _T2) -> _T1 | _T2:
...


# This is right, as `_T1` and `_T2` are both covariant with the
# argument type and the return type.
class P4(Protocol[_T1, _T2]):
def m2(self, a: Union[_T1, _T2]) -> Tuple[_T1, _T2]:
def m2(self, a: _T1 | _T2) -> tuple[_T1, _T2]:
...
8 changes: 4 additions & 4 deletions packages/pyright-internal/src/tests/samples/protocol23.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# can't be assigned to Type[Proto].

from abc import abstractmethod
from typing import Protocol, Type
from typing import Protocol


class Proto(Protocol):
Expand All @@ -16,7 +16,7 @@ def meth(self) -> int:
return 42


def func1(cls: Type[Proto]) -> int:
def func1(cls: type[Proto]) -> int:
return cls().meth()


Expand All @@ -26,15 +26,15 @@ def func1(cls: Type[Proto]) -> int:
# not a concrete class type that implements the protocol.
func1(Proto)

val1: Type[Proto]
val1: type[Proto]
val1 = Concrete
val1().meth()

# This should generate an error because Proto is a protocol class.
val1 = Proto


def func2() -> Type[Proto]:
def func2() -> type[Proto]:
...


Expand Down
13 changes: 6 additions & 7 deletions packages/pyright-internal/src/tests/samples/protocol26.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# This sample tests protocol class assignment in a case that involves tricky
# recursion.

from __future__ import annotations
from typing import Protocol, Sequence, TypeVar, Union, overload
from typing import Protocol, Sequence, TypeVar, overload

_T_co = TypeVar("_T_co", covariant=True)

Expand All @@ -18,23 +17,23 @@ def __getitem__(self, __x: SupportsIndex) -> _T_co:
...

@overload
def __getitem__(self, __x: slice) -> TupleLike[_T_co]:
def __getitem__(self, __x: slice) -> "TupleLike[_T_co]":
...

def __getitem__(self, __x: slice | SupportsIndex) -> _T_co | TupleLike[_T_co]:
def __getitem__(self, __x: slice | SupportsIndex) -> "_T_co | TupleLike[_T_co]":
...


class NestedSequence(Protocol[_T_co]):
@overload
def __getitem__(self, index: int, /) -> _T_co | NestedSequence[_T_co]:
def __getitem__(self, index: int, /) -> "_T_co | NestedSequence[_T_co]":
...

@overload
def __getitem__(self, index: slice, /) -> NestedSequence[_T_co]:
def __getitem__(self, index: slice, /) -> "NestedSequence[_T_co]":
...


def func(t: TupleLike[int]):
x: Union[int, NestedSequence[int]] = t
x: int | NestedSequence[int] = t
y: NestedSequence[int] = t
19 changes: 10 additions & 9 deletions packages/pyright-internal/src/tests/samples/protocol28.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
# This sample tests a complicated use case involving multiple
# callback protocols.

from typing import Callable, Protocol, TypeVar, Any
from typing import Protocol, TypeVar, Any


_T1 = TypeVar("_T1", contravariant=True)
_T2 = TypeVar("_T2", covariant=True)
_T3 = TypeVar("_T3", covariant=True)
Tv_my_callable = TypeVar("Tv_my_callable", bound="MyCallable[Any]")


class MyCallable(Protocol[_T1]):
class Callable1(Protocol[_T1]):
def __call__(self, __x: _T1) -> Any:
...


class MyDecorator(Protocol[_T2]):
def __call__(self, __x: MyCallable[_T2]) -> Any:
_T4 = TypeVar("_T4", bound=Callable1[Any])


class Decorator1(Protocol[_T2]):
def __call__(self, __x: Callable1[_T2]) -> Any:
...


def decorates_my_callable(__x: MyDecorator[_T3]) -> MyDecorator[_T3]:
def decorator1(__x: Decorator1[_T3]) -> Decorator1[_T3]:
...


def my_decorator_inner(__x: Tv_my_callable) -> Tv_my_callable:
def func1(__x: _T4) -> _T4:
...


decorates_my_callable(my_decorator_inner)

decorator1(func1)
8 changes: 4 additions & 4 deletions packages/pyright-internal/src/tests/samples/protocol29.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from functools import partial
from typing_extensions import Protocol, Self
from typing import Any, Callable, Type, TypeVar
from typing import Any, Callable, TypeVar

_T = TypeVar("_T", covariant=True)

Expand All @@ -14,13 +14,13 @@ def func(self) -> Callable[..., _T]:
...

def __new__(
cls: Type[Self], __func: Callable[..., _T], *args: Any, **kwargs: Any
cls: type[Self], __func: Callable[..., _T], *args: Any, **kwargs: Any
) -> Self:
...


def f(x: Partial[int]):
def func1(x: Partial[int]):
...


f(partial(int))
func1(partial(int))
Loading

0 comments on commit b0d4080

Please sign in to comment.