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 19, 2023
1 parent 0c5c7b6 commit cd257f7
Show file tree
Hide file tree
Showing 80 changed files with 357 additions and 427 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def func1(future: Future[_T]) -> Future[_T]:
...


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


Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This sample tests the type checker's handling of named tuples.

from collections import defaultdict, namedtuple
from typing import NamedTuple, Tuple
from collections import namedtuple
from typing import NamedTuple

NamedTuple1 = namedtuple("NamedTuple1", "field1 field2")
NamedTuple1(1, 2)
Expand All @@ -19,10 +19,10 @@
# should be two parameters.
NamedTuple1(1, 2, 3)

s1: Tuple[float, float] = NamedTuple1(3, 4)
s1: tuple[float, float] = NamedTuple1(3, 4)

# This should generate an error because there are not enough entries.
s2: Tuple[float, float, float] = NamedTuple1(3, 4)
s2: tuple[float, float, float] = NamedTuple1(3, 4)

NamedTuple2 = namedtuple("NamedTuple2", "field1, field2")
NamedTuple2.__new__.__defaults__ = ([],)
Expand Down Expand Up @@ -53,13 +53,13 @@
# type mismatch.
NamedTuple3(field2=1, field1=2)

t1: Tuple[str, float] = NamedTuple3("hello", 2)
t1: tuple[str, float] = NamedTuple3("hello", 2)

# This should generate an error because the types are incompatible.
t2: Tuple[float, float] = NamedTuple3("hello", 2)
t2: tuple[float, float] = NamedTuple3("hello", 2)

# This should generate an error because the lengths are incompatible.
t3: Tuple[str, float, str] = NamedTuple3("hello", 2)
t3: tuple[str, float, str] = NamedTuple3("hello", 2)

t4: NamedTuple = NamedTuple3("hello", 2)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

from typing import Generic, NamedTuple, TypeVar

# This should generate an error

# This should generate an error.
class A(NamedTuple, object):
x: int

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ class NT1(NamedTuple, Generic[_T1]):
reveal_type(NT1(3.4, 4, [1, 2]), expected_text="NT1[float]")
reveal_type(NT1(3.4, 4, [2j]), expected_text="NT1[complex]")

class NT2(NT1[str]): ...

class NT2(NT1[str]):
...


reveal_type(NT2("", 4, []), expected_text="NT2")

# This should generate an error.
NT2(1, 4, [])

7 changes: 6 additions & 1 deletion packages/pyright-internal/src/tests/samples/never2.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,23 @@
U = TypeVar("U")

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


class ClassA(Generic[T_co]):
pass


def func1(x: U) -> ClassA[U]:
return ClassA[Never]()


T = TypeVar("T")


class ClassB(Generic[T]):
pass


def func2(x: U) -> ClassB[U]:
# This should generate an error because T is invariant.
return ClassB[Never]()

4 changes: 2 additions & 2 deletions packages/pyright-internal/src/tests/samples/newType1.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This sample tests the type handler's handling of the
# built-in NewType function.

from typing import NewType, Type, TypeVar
from typing import NewType, TypeVar

MyString = NewType("MyString", str)

Expand All @@ -27,7 +27,7 @@ def must_take_my_string(p1: MyString):
_T = TypeVar("_T")


def func1(x: Type[_T]) -> Type[_T]:
def func1(x: type[_T]) -> type[_T]:
return x


Expand Down
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/tests/samples/newType3.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This sample tests that the type checker is properly synthesizing
# a constructor for a NewType.

from typing import NewType, Union
from typing import NewType


UserId = NewType("UserId", int)
Expand Down
8 changes: 8 additions & 0 deletions packages/pyright-internal/src/tests/samples/newType4.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
from typing import Literal, NewType, Sized, Union


# This should generate an error.
A = NewType("A", Union[int, str])

# This should generate an error.
B = NewType("B", Literal[1])

# This should generate an error.
C = NewType("B", Sized)

# This should generate an error.
D = NewType("A", int | str)
4 changes: 2 additions & 2 deletions packages/pyright-internal/src/tests/samples/none1.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This sample tests properties of the special NoneType.

from typing import Hashable, Iterable, Optional
from typing import Hashable, Iterable

a: Hashable = None

Expand All @@ -12,7 +12,7 @@
c.__doc__


def func1(a: Optional[int]):
def func1(a: int | None):
a.__class__
a.__doc__

Expand Down
5 changes: 1 addition & 4 deletions packages/pyright-internal/src/tests/samples/none2.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
# This sample checks that type[None] is handled correctly.


from typing import Type


def func1(a: Type[None]) -> Type[str] | Type[None]:
def func1(a: type[None]) -> type[str] | type[None]:
reveal_type(a, expected_text="type[None]")

# This should generate an error because None is
Expand Down
16 changes: 8 additions & 8 deletions packages/pyright-internal/src/tests/samples/noreturn2.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
# This sample tests that the NoReturn logic is able to handle
# union types in call expressions.

from typing import NoReturn, Union
from typing import NoReturn


def f() -> NoReturn:
def func1() -> NoReturn:
raise TypeError


class B(object):
class B:
def always_noreturn(self) -> NoReturn:
f()
func1()

def sometimes_noreturn(self) -> NoReturn:
raise TypeError


class C(object):
class C:
def always_noreturn(self) -> NoReturn:
f()
func1()

def sometimes_noreturn(self) -> int:
return 0


class A(object):
class A:
def __init__(self):
# Note the union type declaration here.
self._B_or_C: Union[B, C] = B()
self._B_or_C: B | C = B()

def m3(self) -> NoReturn:
self._B_or_C.always_noreturn()
Expand Down
4 changes: 1 addition & 3 deletions packages/pyright-internal/src/tests/samples/noreturn3.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@


class MyClass:
def no_return(
self,
) -> NoReturn:
def no_return(self) -> NoReturn:
...


Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
# This sample tests the type checker's ability to check
# custom operator overrides.

from typing import Union


class A(object):
class A:
def __eq__(self, Foo):
return "equal"


class B(object):
class B:
def __ne__(self, Bar):
return self

Expand All @@ -30,7 +28,7 @@ def needs_a_string(val: str):
pass


def needs_a_string_or_bool(val: Union[bool, str]):
def needs_a_string_or_bool(val: bool | str):
pass


Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# This sample tests bidirectional type inference for | operators. This
# should apply only to TypedDict types.

from typing import Literal, TypeVar, Generic, Callable, Union
from typing import Literal, TypeVar, Generic, Callable

T1 = TypeVar("T1")
T2 = TypeVar("T2")


class S(Generic[T1]):
def __or__(self, other: "S[T2]") -> "S[Union[T1, T2]]":
def __or__(self, other: "S[T2]") -> "S[T1 | T2]":
...


Expand Down
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/tests/samples/overload1.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This sample tests the type checker's handling of the overload decorator.

from typing import overload, Optional
from typing import overload
from datetime import datetime, timezone, timedelta


Expand Down
13 changes: 6 additions & 7 deletions packages/pyright-internal/src/tests/samples/overload12.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# This sample tests overload matching in cases where the match
# is ambiguous due to an Any or Unknown argument.

from __future__ import annotations
from typing import Any, Generic, Literal, TypeVar, overload
from typing_extensions import LiteralString

Expand Down Expand Up @@ -144,14 +143,14 @@ def func6(y: list[Any]):

class ClassA(Generic[_T]):
@overload
def m1(self: ClassA[int]) -> ClassA[int]:
def m1(self: "ClassA[int]") -> "ClassA[int]":
...

@overload
def m1(self: ClassA[str]) -> ClassA[str]:
def m1(self: "ClassA[str]") -> "ClassA[str]":
...

def m1(self) -> ClassA[Any]:
def m1(self) -> "ClassA[Any]":
return self


Expand All @@ -161,14 +160,14 @@ def func7(a: ClassA[Any]):

class ClassB(Generic[_T]):
@overload
def m1(self: ClassB[int], obj: int | ClassB[int]) -> ClassB[int]:
def m1(self: "ClassB[int]", obj: "int | ClassB[int]") -> "ClassB[int]":
...

@overload
def m1(self: ClassB[str], obj: str | ClassB[str]) -> ClassB[str]:
def m1(self: "ClassB[str]", obj: "str | ClassB[str]") -> "ClassB[str]":
...

def m1(self, obj: Any) -> ClassB[Any]:
def m1(self, obj: Any) -> "ClassB[Any]":
return self


Expand Down
1 change: 0 additions & 1 deletion packages/pyright-internal/src/tests/samples/overload13.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@

l: list[str] = []
"{s}".format(s="\n".join(sorted(l)))

1 change: 1 addition & 0 deletions packages/pyright-internal/src/tests/samples/overload14.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@

import subprocess


def my_method(cmd, *args, **kwargs):
return subprocess.run(cmd, *args, **kwargs)
20 changes: 6 additions & 14 deletions packages/pyright-internal/src/tests/samples/overload2.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This sample verifies that overloads work in
# conjunction with async methods.

from typing import Union, overload
from typing import overload


@overload
Expand All @@ -14,24 +14,16 @@ async def func(x: str) -> str:
...


async def func(x) -> Union[int, str]:
async def func(x) -> int | str:
if isinstance(x, int):
return 32
else:
return "that"


def requires_str(a: str):
pass


def requires_int(a: int):
pass


async def test_function():
should_be_str = await func("2")
requires_str(should_be_str)
v1 = await func("2")
reveal_type(v1, expected_text="str")

should_be_int = await func(2)
requires_int(should_be_int)
v2 = await func(2)
reveal_type(v2, expected_text="int")
8 changes: 4 additions & 4 deletions packages/pyright-internal/src/tests/samples/overload3.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# final function that omits the @overload decorator when matching
# a caller against an overloaded function.

from typing import Union, TypeVar, overload, Tuple, Optional
from typing import TypeVar, overload

T = TypeVar("T")

Expand All @@ -13,13 +13,13 @@ def mouse_event(x1: int, y1: int) -> int:


@overload
def mouse_event(x1: int, y1: int, x2: int, y2: int) -> Tuple[int, int]:
def mouse_event(x1: int, y1: int, x2: int, y2: int) -> tuple[int, int]:
...


def mouse_event(
x1: int, y1: int, x2: Optional[int] = None, y2: Optional[int] = None
) -> Union[int, Tuple[int, int]]:
x1: int, y1: int, x2: int | None = None, y2: int | None = None
) -> int | tuple[int, int]:
return 1


Expand Down
Loading

0 comments on commit cd257f7

Please sign in to comment.