Use modern syntax in test-case files where possible (#9261)

This commit is contained in:
Alex Waygood
2022-11-23 19:58:11 +00:00
committed by GitHub
parent 956690dec2
commit 01e78c7e81
19 changed files with 73 additions and 33 deletions

View File

@@ -1,13 +1,15 @@
from __future__ import annotations
from asyncio import iscoroutinefunction
from collections.abc import Awaitable, Callable, Coroutine
from typing import Any, Union
from typing import Any
from typing_extensions import assert_type
def test_iscoroutinefunction(
x: Callable[[str, int], Coroutine[str, int, bytes]],
y: Callable[[str, int], Awaitable[bytes]],
z: Callable[[str, int], Union[str, Awaitable[bytes]]],
z: Callable[[str, int], str | Awaitable[bytes]],
xx: object,
) -> None:

View File

@@ -1,3 +1,5 @@
from __future__ import annotations
import asyncio
from typing import Any, Awaitable, List, Tuple, Union
from typing_extensions import assert_type
@@ -21,7 +23,7 @@ async def test_gather(awaitable1: Awaitable[int], awaitable2: Awaitable[str]) ->
c = await asyncio.gather(awaitable1, awaitable2, awaitable1, awaitable1, awaitable1, awaitable1)
assert_type(c, List[Any])
awaitables_list: List[Awaitable[int]] = [awaitable1]
awaitables_list: list[Awaitable[int]] = [awaitable1]
d = await asyncio.gather(*awaitables_list)
assert_type(d, List[Any])

View File

@@ -1,4 +1,6 @@
from typing import Dict, Generic, Iterable, Tuple, TypeVar
from __future__ import annotations
from typing import Dict, Generic, Iterable, TypeVar
from typing_extensions import assert_type
# These do follow `__init__` overloads order:
@@ -6,7 +8,7 @@ from typing_extensions import assert_type
# mypy raises: 'Need type annotation for "bad"'
# pyright is fine with it.
# bad = dict()
good: Dict[str, str] = dict()
good: dict[str, str] = dict()
assert_type(good, Dict[str, str])
assert_type(dict(arg=1), Dict[str, int])
@@ -16,7 +18,7 @@ _VT = TypeVar("_VT")
class KeysAndGetItem(Generic[_KT, _VT]):
data: Dict[_KT, _VT]
data: dict[_KT, _VT]
def keys(self) -> Iterable[_KT]:
return self.data.keys()
@@ -33,15 +35,15 @@ kt2: KeysAndGetItem[str, int] = KeysAndGetItem()
assert_type(dict(kt2, arg=1), Dict[str, int])
def test_iterable_tuple_overload(x: Iterable[Tuple[int, str]]) -> Dict[int, str]:
def test_iterable_tuple_overload(x: Iterable[tuple[int, str]]) -> dict[int, str]:
return dict(x)
i1: Iterable[Tuple[int, str]] = [(1, "a"), (2, "b")]
i1: Iterable[tuple[int, str]] = [(1, "a"), (2, "b")]
test_iterable_tuple_overload(i1)
dict(i1, arg="a") # type: ignore
i2: Iterable[Tuple[str, int]] = [("a", 1), ("b", 2)]
i2: Iterable[tuple[str, int]] = [("a", 1), ("b", 2)]
assert_type(dict(i2, arg=1), Dict[str, int])
i3: Iterable[str] = ["a.b"]

View File

@@ -1,3 +1,5 @@
from __future__ import annotations
from typing import Iterator
from typing_extensions import assert_type

View File

@@ -1,3 +1,5 @@
from __future__ import annotations
from typing import List, Union
from typing_extensions import assert_type

View File

@@ -1,9 +1,11 @@
from typing import Any, Tuple, Union
from __future__ import annotations
from typing import Any
# The following should pass without error (see #6661):
class Diagnostic:
def __reduce__(self) -> Union[str, Tuple[Any, ...]]:
def __reduce__(self) -> str | tuple[Any, ...]:
res = super().__reduce__()
if isinstance(res, tuple) and len(res) >= 3:
res[2]["_info"] = 42

View File

@@ -1,3 +1,5 @@
from __future__ import annotations
from decimal import Decimal
from fractions import Fraction
from typing import Any

View File

@@ -1,26 +1,28 @@
from __future__ import annotations
from typing import Any, List, Union
from typing_extensions import Literal, assert_type
class Foo:
def __add__(self, other: Any) -> "Foo":
def __add__(self, other: Any) -> Foo:
return Foo()
class Bar:
def __radd__(self, other: Any) -> "Bar":
def __radd__(self, other: Any) -> Bar:
return Bar()
class Baz:
def __add__(self, other: Any) -> "Baz":
def __add__(self, other: Any) -> Baz:
return Baz()
def __radd__(self, other: Any) -> "Baz":
def __radd__(self, other: Any) -> Baz:
return Baz()
literal_list: List[Literal[0, 1]] = [0, 1, 1]
literal_list: list[Literal[0, 1]] = [0, 1, 1]
assert_type(sum([2, 4]), int)
assert_type(sum([3, 5], 4), int)

View File

@@ -1,3 +1,5 @@
from __future__ import annotations
from typing import Tuple
from typing_extensions import assert_type

View File

@@ -1,3 +1,5 @@
from __future__ import annotations
import codecs
from typing_extensions import assert_type

View File

@@ -1,3 +1,5 @@
from __future__ import annotations
from contextlib import ExitStack
from typing_extensions import assert_type

View File

@@ -1,3 +1,5 @@
from __future__ import annotations
import logging
from typing import Any

View File

@@ -1,3 +1,5 @@
from __future__ import annotations
import _threading_local
import threading

View File

@@ -1,3 +1,5 @@
from __future__ import annotations
import unittest
from datetime import datetime, timedelta
from decimal import Decimal
@@ -58,14 +60,14 @@ class Eggs:
class Ham:
def __lt__(self, other: "Ham") -> bool:
def __lt__(self, other: Ham) -> bool:
if not isinstance(other, Ham):
return NotImplemented
return True
class Bacon:
def __gt__(self, other: "Bacon") -> bool:
def __gt__(self, other: Bacon) -> bool:
if not isinstance(other, Bacon):
return NotImplemented
return True

View File

@@ -1,3 +1,5 @@
from __future__ import annotations
# pyright: reportWildcardImportFromLibrary=false
"""

View File

@@ -1,3 +1,5 @@
from __future__ import annotations
from typing import Match, Optional, Pattern
from typing_extensions import assert_type