Skip to content

Commit

Permalink
Next batch of text case cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
msfterictraut committed Jun 19, 2023
1 parent 899799d commit 0c5c7b6
Show file tree
Hide file tree
Showing 103 changed files with 1,256 additions and 1,286 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This sample tests type checking for list comprehensions.

from typing import Any, Generator, Iterable, List, Literal
from typing import Generator, Iterable, Literal

a = [1, 2, 3, 4]

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


def func2() -> List[int]:
def func2() -> list[int]:
c = [elem for elem in a]
return c


def func3() -> List[str]:
def func3() -> list[str]:
c = [elem for elem in a]

# This should generate an error because
Expand All @@ -37,7 +37,7 @@ def generate():
FooOrBar = Literal["foo", "bar"]


def to_list(values: Iterable[FooOrBar]) -> List[FooOrBar]:
def to_list(values: Iterable[FooOrBar]) -> list[FooOrBar]:
return [value for value in values]

x = 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
# pyright: strict


class Foo:
class ClassA:
input: str
output: str


def minify1(foo: Foo):
def func1(foo: ClassA):
foo.output = "".join(
stripped for line in foo.input.splitlines() if (stripped := line.strip())
)
4 changes: 4 additions & 0 deletions packages/pyright-internal/src/tests/samples/hashability1.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
# This should generate two errors because {} and [] are not hashable.
s2: set[Any] = {{}, 2, dict, frozenset(), []}


class StrList(list[str]):
def __hash__(self) -> int:
...


s3 = {StrList()}


Expand All @@ -31,10 +33,12 @@ def func1(x: str | dict[Any, Any], y: Any, z: None):

d4 = {y: "hi", z: "hi"}


@dataclass
class DC1:
a: int


@dataclass(frozen=True)
class DC2:
a: int
Expand Down
1 change: 0 additions & 1 deletion packages/pyright-internal/src/tests/samples/import16.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@
import html.entities

x = html.escape

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# This sample tests the ability of the type checker to infer
# the types of instance variables based on their assigned values.


class ClassA:
def __init__(self):
self.value = None
Expand All @@ -13,4 +14,3 @@ def func(self, param: int):
self.value.bit_length()

self.value = param

61 changes: 6 additions & 55 deletions packages/pyright-internal/src/tests/samples/isinstance1.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,8 @@
# This sample tests basic type narrowing behavior for
# the isinstance call.
# This sample tests the use of "self.__class__" and "__class__"
# in an isinstance call.

from typing import Any, List, Optional, Type, TypedDict, Union


def func1(x: Union[List[str], int]):
if isinstance(x, list):
reveal_type(x, expected_text="List[str]")
else:
reveal_type(x, expected_text="int")


def func2(x: Any):
if isinstance(x, list):
reveal_type(x, expected_text="list[Unknown]")
else:
reveal_type(x, expected_text="Any")


def func3(x):
if isinstance(x, list):
reveal_type(x, expected_text="list[Unknown]")
else:
reveal_type(x, expected_text="Unknown")


class SomeTypedDict(TypedDict):
name: str


def func4(x: Union[int, SomeTypedDict]):
if isinstance(x, dict):
reveal_type(x, expected_text="SomeTypedDict")
else:
reveal_type(x, expected_text="int")


def func5(x: int | str | complex):
if isinstance(x, (int, str)):
reveal_type(x, expected_text="int | str")
else:
reveal_type(x, expected_text="complex")


def func6(x: Type[int] | Type[str] | Type[complex]):
if issubclass(x, (int, str)):
reveal_type(x, expected_text="type[int] | type[str]")
else:
reveal_type(x, expected_text="type[complex]")


def func7(x: Optional[Union[int, SomeTypedDict]]):
if isinstance(x, (dict, type(None))):
reveal_type(x, expected_text="SomeTypedDict | None")
else:
reveal_type(x, expected_text="int")
class Foo:
def bar(self):
a = isinstance(object(), self.__class__)
b = isinstance(object(), __class__)
8 changes: 0 additions & 8 deletions packages/pyright-internal/src/tests/samples/isinstance5.py

This file was deleted.

4 changes: 2 additions & 2 deletions packages/pyright-internal/src/tests/samples/lambda1.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This sample tests type checking for lambdas and their parameters.
from typing import Any, Callable, Iterable, Optional, TypeVar
from typing import Callable, Iterable, TypeVar

#------------------------------------------------------
# Test basic lambda matching
Expand Down Expand Up @@ -64,7 +64,7 @@ def needs_function2(callback: Callable[[str, int], str]):

_T1 = TypeVar('_T1')

def may_need_function_generic(callback: Optional[Callable[[_T1], _T1]]):
def may_need_function_generic(callback: Callable[[_T1], _T1] | None):
pass

may_need_function_generic(lambda x: x)
Expand Down
6 changes: 3 additions & 3 deletions packages/pyright-internal/src/tests/samples/lambda4.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# This sample tests the case where a lambda is assigned to
# a union type that contains multiple callables.

from typing import Callable, Protocol, Union
from typing import Callable, Protocol


U1 = Union[Callable[[int, str], bool], Callable[[str], bool]]
U1 = Callable[[int, str], bool] | Callable[[str], bool]


def accepts_u1(cb: U1) -> U1:
Expand Down Expand Up @@ -54,7 +54,7 @@ def __call__(self, p0: int, p1: str, *p2: str) -> bool:
...


U2 = Union[Callable1, Callable2, Callable3, Callable4]
U2 = Callable1 | Callable2 | Callable3 | Callable4


def accepts_u2(cb: U2) -> U2:
Expand Down
1 change: 1 addition & 0 deletions packages/pyright-internal/src/tests/samples/lambda7.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@

# pyright: strict


def func1(keys: list[str]):
filter(lambda s: s.startswith(""), keys)
29 changes: 9 additions & 20 deletions packages/pyright-internal/src/tests/samples/list1.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,7 @@

# pyright: strict, reportUnknownVariableType=false

from typing import (
Any,
Collection,
Dict,
Generic,
List,
Literal,
Optional,
Sequence,
TypeVar,
Union,
)
from typing import Any, Collection, Generic, Literal, Sequence, TypeVar


v1 = [1, 2, 3]
Expand All @@ -25,7 +14,7 @@
v3 = []
reveal_type(v3, expected_text="list[Unknown]")

v4: List[object] = []
v4: list[object] = []

v5: object = []

Expand Down Expand Up @@ -58,22 +47,22 @@ class Bar:
v10.baz = [Foo()]
reveal_type(v10.baz, expected_text="list[Foo]")

v11: List[Any] = [["hi", ["hi"], [[{}]]]]
v11: list[Any] = [["hi", ["hi"], [[{}]]]]
reveal_type(v11, expected_text="list[Any]")

v12: List[Optional[int]] = [None] * 3
v12: list[int | None] = [None] * 3
reveal_type(v12, expected_text="list[int | None]")

v13: List[Optional[str]] = ["3", None] * 2
v13: list[str | None] = ["3", None] * 2
reveal_type(v13, expected_text="list[str | None]")

x1 = 3
v14: List[Optional[str]] = [None] * x1
v14: list[str | None] = [None] * x1

x2 = [1, 2, 3]
v15: List[Optional[str]] = [None] * sum(x2)
v15: list[str | None] = [None] * sum(x2)

v16: Dict[str, List[Optional[str]]] = {n: [None] * len(n) for n in ["a", "aa", "aaa"]}
v16: dict[str, list[str | None]] = {n: [None] * len(n) for n in ["a", "aa", "aaa"]}


ScalarKeysT = TypeVar("ScalarKeysT", bound=Literal["name", "country"])
Expand All @@ -91,7 +80,7 @@ def func1(by: list[ScalarKeysT]) -> ScalarKeysT:
func1(["id"])


def func2(thing: Union[str, List[Union[str, int]], List[List[Union[str, int]]]]):
def func2(thing: str | list[str | int] | list[list[str | int]]):
...


Expand Down
1 change: 1 addition & 0 deletions packages/pyright-internal/src/tests/samples/list2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import random
from typing_extensions import LiteralString


# The join method is overloaded with both LiteralString and str variants.
# We need to use the str overload here.
def func(x: LiteralString):
Expand Down
1 change: 1 addition & 0 deletions packages/pyright-internal/src/tests/samples/list3.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# This sample tests list inference in a loop where the type of
# the inferred list changes each time through the loop.


def func1(k: str):
keys = ["a", "b", "c"]
value = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def func6(a: LiteralString):

v3: list[str] = "1 2 3".split(" ")


def func7(a: Literal["a", "b"], b: Literal["a", 1]):
v1: LiteralString = f"{a}"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,44 @@
# inferences within loop constructs.


def bar(a: list):
def func1(a: list):
pass


def func1():
def func2():
data = None

for x in [2, 3]:
if not data:
data = [1, 2]
else:
# This should not generate an error because the
# type checker should be able to determine that
# data must be a list at this point in the code.
bar(data)
reveal_type(data, expected_text="list[int]")
func1(data)
else:
reveal_type(data, expected_text="list[int] | None")

# This should generate an error because the
# type checker should be able to determine that
# data must contain None at this point.
bar(data)
func1(data)


x = 20 + 20


def func2():
def func3():
data = None

while x:
if not data:
data = [1, 2]
else:
# This should not generate an error because the
# type checker should be able to determine that
# data must be a list at this point in the code.
bar(data)
reveal_type(data, expected_text="list[int]")
func1(data)
else:
reveal_type(data, expected_text="list[int] | None")

# This should generate an error because the
# type checker should be able to determine that
# data must contain None at this point.
bar(data)
func1(data)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# within a loop body.


class Foo:
class ClassA:
def non_property(self) -> int:
...

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# This sample tests a loop that modifies a variable through type narrowing.

from typing import Union


class State:
def confirm_dialog(self) -> Union["State", bool]:
def confirm_dialog(self) -> "State | bool":
return False


Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# This sample tests a piece of code that involves lots
# of cyclical dependencies for type resolution.

from typing import Tuple


def needs_str(a: str) -> Tuple[str, str]:
def needs_str(a: str) -> tuple[str, str]:
...


Expand Down
Loading

0 comments on commit 0c5c7b6

Please sign in to comment.