Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions stdlib/@tests/test_cases/asyncio/check_gather.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import asyncio
from typing import Awaitable, List, Tuple, Union
from collections.abc import Awaitable
from typing_extensions import assert_type


Expand All @@ -15,20 +15,20 @@ async def coro2() -> str:

async def test_gather(awaitable1: Awaitable[int], awaitable2: Awaitable[str]) -> None:
a = await asyncio.gather(awaitable1)
assert_type(a, Tuple[int])
assert_type(a, tuple[int])

b = await asyncio.gather(awaitable1, awaitable2, return_exceptions=True)
assert_type(b, Tuple[Union[int, BaseException], Union[str, BaseException]])
assert_type(b, tuple[int | BaseException, str | BaseException])

c = await asyncio.gather(awaitable1, awaitable2, awaitable1, awaitable1, awaitable1, awaitable1)
assert_type(c, Tuple[int, str, int, int, int, int])
assert_type(c, tuple[int, str, int, int, int, int])

d = await asyncio.gather(awaitable1, awaitable1, awaitable1, awaitable1, awaitable1, awaitable1, awaitable1)
assert_type(d, List[int])
assert_type(d, list[int])

awaitables_list: list[Awaitable[int]] = [awaitable1]
e = await asyncio.gather(*awaitables_list)
assert_type(e, List[int])
assert_type(e, list[int])

# this case isn't reliable between typecheckers, no one would ever call it with no args anyway
# f = await asyncio.gather()
Expand Down
33 changes: 17 additions & 16 deletions stdlib/@tests/test_cases/builtins/check_dict.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from __future__ import annotations

import os
from typing import Any, Dict, Generic, Iterable, Mapping, TypeVar, Union
from collections.abc import Iterable, Mapping
from typing import Any, Generic, TypeVar
from typing_extensions import Self, assert_type

###################################################################
Expand All @@ -15,9 +16,9 @@
# https://github.com/python/mypy/issues/12358
# bad = dict()
good: dict[str, str] = dict()
assert_type(good, Dict[str, str])
assert_type(good, dict[str, str])

assert_type(dict(arg=1), Dict[str, int])
assert_type(dict(arg=1), dict[str, int])

_KT = TypeVar("_KT")
_VT = TypeVar("_VT")
Expand All @@ -37,11 +38,11 @@ def __getitem__(self, __k: _KT) -> _VT:


kt1: KeysAndGetItem[int, str] = KeysAndGetItem({0: ""})
assert_type(dict(kt1), Dict[int, str])
assert_type(dict(kt1), dict[int, str])
dict(kt1, arg="a") # type: ignore

kt2: KeysAndGetItem[str, int] = KeysAndGetItem({"": 0})
assert_type(dict(kt2, arg=1), Dict[str, int])
assert_type(dict(kt2, arg=1), dict[str, int])


def test_iterable_tuple_overload(x: Iterable[tuple[int, str]]) -> dict[int, str]:
Expand All @@ -53,12 +54,12 @@ def test_iterable_tuple_overload(x: Iterable[tuple[int, str]]) -> dict[int, str]
dict(i1, arg="a") # type: ignore

i2: Iterable[tuple[str, int]] = [("a", 1), ("b", 2)]
assert_type(dict(i2, arg=1), Dict[str, int])
assert_type(dict(i2, arg=1), dict[str, int])

i3: Iterable[str] = ["a.b"]
i4: Iterable[bytes] = [b"a.b"]
assert_type(dict(string.split(".") for string in i3), Dict[str, str])
assert_type(dict(string.split(b".") for string in i4), Dict[bytes, bytes])
assert_type(dict(string.split(".") for string in i3), dict[str, str])
assert_type(dict(string.split(b".") for string in i4), dict[bytes, bytes])

dict(["foo", "bar", "baz"]) # type: ignore
dict([b"foo", b"bar", b"baz"]) # type: ignore
Expand All @@ -71,19 +72,19 @@ def test_iterable_tuple_overload(x: Iterable[tuple[int, str]]) -> dict[int, str]
int_value = 1

assert_type(d_any["key"], Any)
assert_type(d_any.get("key"), Union[Any, None])
assert_type(d_any.get("key", None), Union[Any, None])
assert_type(d_any.get("key"), Any | None)
assert_type(d_any.get("key", None), Any | None)
assert_type(d_any.get("key", any_value), Any)
assert_type(d_any.get("key", str_value), Any)
assert_type(d_any.get("key", int_value), Any)

assert_type(d_str["key"], str)
assert_type(d_str.get("key"), Union[str, None])
assert_type(d_str.get("key", None), Union[str, None])
assert_type(d_str.get("key"), str | None)
assert_type(d_str.get("key", None), str | None)
# Pyright has str instead of Any here
assert_type(d_str.get("key", any_value), Any) # pyright: ignore[reportAssertTypeFailure]
assert_type(d_str.get("key", str_value), str)
assert_type(d_str.get("key", int_value), Union[str, int])
assert_type(d_str.get("key", int_value), str | int)

# Now with context!
result: str
Expand Down Expand Up @@ -184,16 +185,16 @@ def test_dict_dot_or(
assert_type(a | b, dict[int, int])
assert_type(b | a, dict[int, int])

assert_type(a | c, dict[Union[int, str], Union[int, str]])
assert_type(a | c, dict[int | str, int | str])

# arbitrary mappings are not accepted by `dict.__or__`;
# it has to be a subclass of `dict`
a | d # type: ignore

# but Mappings such as `os._Environ` or `CustomMappingWithDunderOr`,
# which define `__ror__` methods that accept `dict`, are fine:
assert_type(a | os.environ, dict[Union[str, int], Union[str, int]])
assert_type(os.environ | a, dict[Union[str, int], Union[str, int]])
assert_type(a | os.environ, dict[int | str, int | str])
assert_type(os.environ | a, dict[int | str, int | str])

assert_type(c | os.environ, dict[str, str])
assert_type(c | e, dict[str, str])
Expand Down
3 changes: 1 addition & 2 deletions stdlib/@tests/test_cases/builtins/check_list.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

from typing import List, Union
from typing_extensions import assert_type


Expand All @@ -16,6 +15,6 @@ def asd(self) -> int:


combined = [Foo()] + [Bar()]
assert_type(combined, List[Union[Foo, Bar]])
assert_type(combined, list[Foo | Bar])
for item in combined:
assert_type(item.asd(), int)
6 changes: 3 additions & 3 deletions stdlib/@tests/test_cases/builtins/check_sum.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import Any, List, Literal, Union
from typing import Any, Literal
from typing_extensions import assert_type


Expand Down Expand Up @@ -31,10 +31,10 @@ def __radd__(self, other: Any) -> Baz:
assert_type(sum([True, False], True), int)
assert_type(sum(literal_list), int)

assert_type(sum([["foo"], ["bar"]], ["baz"]), List[str])
assert_type(sum([["foo"], ["bar"]], ["baz"]), list[str])

assert_type(sum([Foo(), Foo()], Foo()), Foo)
assert_type(sum([Baz(), Baz()]), Union[Baz, Literal[0]])
assert_type(sum([Baz(), Baz()]), Baz | Literal[0])

# mypy and pyright infer the types differently for these, so we can't use assert_type
# Just test that no error is emitted for any of these
Expand Down
14 changes: 7 additions & 7 deletions stdlib/@tests/test_cases/builtins/check_zip.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from __future__ import annotations

from typing import Sequence, Tuple
from collections.abc import Sequence
from typing_extensions import assert_type

ints: Sequence[int] = [1, 2, 3]
strs: Sequence[str] = ["one", "two", "three"]
floats: Sequence[float] = [1.0, 2.0, 3.0]
str_tuples: Sequence[Tuple[str]] = list((x,) for x in strs)
str_tuples: Sequence[tuple[str]] = list((x,) for x in strs)

assert_type(zip(ints), zip[Tuple[int]])
assert_type(zip(ints, strs), zip[Tuple[int, str]])
assert_type(zip(ints, strs, floats), zip[Tuple[int, str, float]])
assert_type(zip(strs, ints, floats, ints), zip[Tuple[str, int, float, int]])
assert_type(zip(strs, ints, floats, ints, str_tuples), zip[Tuple[str, int, float, int, Tuple[str]]])
assert_type(zip(ints), zip[tuple[int]])
assert_type(zip(ints, strs), zip[tuple[int, str]])
assert_type(zip(ints, strs, floats), zip[tuple[int, str, float]])
assert_type(zip(strs, ints, floats, ints), zip[tuple[str, int, float, int]])
assert_type(zip(strs, ints, floats, ints, str_tuples), zip[tuple[str, int, float, int, tuple[str]]])
40 changes: 20 additions & 20 deletions stdlib/@tests/test_cases/check_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import dataclasses as dc
import sys
from typing import TYPE_CHECKING, Any, Dict, FrozenSet, Tuple, Type, Union
from typing import TYPE_CHECKING, Any
from typing_extensions import Annotated, assert_type

if TYPE_CHECKING:
Expand All @@ -14,21 +14,21 @@ class Foo:
attr: str


assert_type(dc.fields(Foo), Tuple[dc.Field[Any], ...])
assert_type(dc.fields(Foo), tuple[dc.Field[Any], ...])

dc.asdict(Foo) # type: ignore
dc.astuple(Foo) # type: ignore
dc.replace(Foo) # type: ignore

# See #9723 for why we can't make this assertion
# if dc.is_dataclass(Foo):
# assert_type(Foo, Type[Foo])
# assert_type(Foo, type[Foo])

f = Foo(attr="attr")

assert_type(dc.fields(f), Tuple[dc.Field[Any], ...])
assert_type(dc.asdict(f), Dict[str, Any])
assert_type(dc.astuple(f), Tuple[Any, ...])
assert_type(dc.fields(f), tuple[dc.Field[Any], ...])
assert_type(dc.asdict(f), dict[str, Any])
assert_type(dc.astuple(f), tuple[Any, ...])
assert_type(dc.replace(f, attr="new"), Foo)

if dc.is_dataclass(f):
Expand All @@ -39,17 +39,17 @@ class Foo:

def is_dataclass_any(arg: Any) -> None:
if dc.is_dataclass(arg):
assert_type(arg, Union["DataclassInstance", Type["DataclassInstance"]])
assert_type(arg, DataclassInstance | type[DataclassInstance])


def is_dataclass_object(arg: object) -> None:
if dc.is_dataclass(arg):
assert_type(arg, Union["DataclassInstance", Type["DataclassInstance"]])
assert_type(arg, DataclassInstance | type[DataclassInstance])


def is_dataclass_type(arg: type) -> None:
if dc.is_dataclass(arg):
assert_type(arg, Type["DataclassInstance"])
assert_type(arg, type[DataclassInstance])


def check_other_isdataclass_overloads(x: type, y: object) -> None:
Expand All @@ -68,26 +68,26 @@ def check_other_isdataclass_overloads(x: type, y: object) -> None:
dc.replace(y) # type: ignore

if dc.is_dataclass(x):
assert_type(x, Type["DataclassInstance"])
assert_type(dc.fields(x), Tuple[dc.Field[Any], ...])
assert_type(x, type[DataclassInstance])
assert_type(dc.fields(x), tuple[dc.Field[Any], ...])

dc.asdict(x) # type: ignore
dc.astuple(x) # type: ignore
dc.replace(x) # type: ignore

if dc.is_dataclass(y):
assert_type(y, Union["DataclassInstance", Type["DataclassInstance"]])
assert_type(dc.fields(y), Tuple[dc.Field[Any], ...])
assert_type(y, DataclassInstance | type[DataclassInstance])
assert_type(dc.fields(y), tuple[dc.Field[Any], ...])

dc.asdict(y) # type: ignore
dc.astuple(y) # type: ignore
dc.replace(y) # type: ignore

if dc.is_dataclass(y) and not isinstance(y, type):
assert_type(y, "DataclassInstance")
assert_type(dc.fields(y), Tuple[dc.Field[Any], ...])
assert_type(dc.asdict(y), Dict[str, Any])
assert_type(dc.astuple(y), Tuple[Any, ...])
assert_type(y, DataclassInstance)
assert_type(dc.fields(y), tuple[dc.Field[Any], ...])
assert_type(dc.asdict(y), dict[str, Any])
assert_type(dc.astuple(y), tuple[Any, ...])
dc.replace(y)


Expand All @@ -103,13 +103,13 @@ class _D: ...

# Regression test for #11653
D = dc.make_dataclass(
"D", [("a", Union[int, None]), "y", ("z", Annotated[FrozenSet[bytes], "metadata"], dc.field(default=frozenset({b"foo"})))]
"D", [("a", int | None), "y", ("z", Annotated[frozenset[bytes], "metadata"], dc.field(default=frozenset({b"foo"})))]
)
# Check that it's inferred by the type checker as a class object of some kind
# (but don't assert the exact type that `D` is inferred as,
# in case a type checker decides to add some special-casing for
# `make_dataclass` in the future)
assert_type(D.__mro__, Tuple[type, ...])
assert_type(D.__mro__, tuple[type, ...])


if sys.version_info >= (3, 14):
Expand Down Expand Up @@ -148,6 +148,6 @@ def custom_dataclass(

dc.make_dataclass(
"D",
[("a", Union[int, None]), "y", ("z", Annotated[FrozenSet[bytes], "metadata"], dc.field(default=frozenset({b"foo"})))],
[("a", int | None), "y", ("z", Annotated[frozenset[bytes], "metadata"], dc.field(default=frozenset({b"foo"})))],
decorator=custom_dataclass,
)
14 changes: 7 additions & 7 deletions stdlib/@tests/test_cases/check_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from decimal import Decimal
from fractions import Fraction
from math import prod
from typing import Any, Literal, Union
from typing import Any, Literal
from typing_extensions import assert_type


Expand Down Expand Up @@ -35,16 +35,16 @@ def __rmul__(self, other: Any) -> SupportsMulAndRMul:
assert_type(prod(literal_list), int)

assert_type(prod([SupportsMul(), SupportsMul()], start=SupportsMul()), SupportsMul)
assert_type(prod([SupportsMulAndRMul(), SupportsMulAndRMul()]), Union[SupportsMulAndRMul, Literal[1]])
assert_type(prod([SupportsMulAndRMul(), SupportsMulAndRMul()]), SupportsMulAndRMul | Literal[1])

assert_type(prod([5.6, 3.2]), Union[float, Literal[1]])
assert_type(prod([5.6, 3.2], start=3), Union[float, int])
assert_type(prod([5.6, 3.2]), float | Literal[1])
assert_type(prod([5.6, 3.2], start=3), float | int)

assert_type(prod([Fraction(7, 2), Fraction(3, 5)]), Union[Fraction, Literal[1]])
assert_type(prod([Fraction(7, 2), Fraction(3, 5)]), Fraction | Literal[1])
assert_type(prod([Fraction(7, 2), Fraction(3, 5)], start=Fraction(1)), Fraction)
assert_type(prod([Decimal("3.14"), Decimal("2.71")]), Union[Decimal, Literal[1]])
assert_type(prod([Decimal("3.14"), Decimal("2.71")]), Decimal | Literal[1])
assert_type(prod([Decimal("3.14"), Decimal("2.71")], start=Decimal("1.00")), Decimal)
assert_type(prod([complex(7, 2), complex(3, 5)]), Union[complex, Literal[1]])
assert_type(prod([complex(7, 2), complex(3, 5)]), complex | Literal[1])
assert_type(prod([complex(7, 2), complex(3, 5)], start=complex(1, 0)), complex)


Expand Down
8 changes: 4 additions & 4 deletions stdlib/@tests/test_cases/check_os_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
from _typeshed import StrOrBytesPath
from os import PathLike
from os.path import abspath, expanduser, expandvars
from typing import AnyStr, Union
from typing import AnyStr
from typing_extensions import assert_type


def test_str_path(str_path: StrOrBytesPath) -> None:
# These methods are currently overloaded to work around python/mypy#17952 & python/mypy#11880
# Let's ensure that they'll still work with a StrOrBytesPath if the workaround is removed

assert_type(abspath(str_path), Union[str, bytes])
assert_type(expanduser(str_path), Union[str, bytes])
assert_type(expandvars(str_path), Union[str, bytes])
assert_type(abspath(str_path), str | bytes)
assert_type(expanduser(str_path), str | bytes)
assert_type(expandvars(str_path), str | bytes)


# See python/mypy#17952
Expand Down
Loading
Loading