| 1 |
[case testUnboundTypeVar] |
| 2 |
from typing import TypeVar |
| 3 |
|
| 4 |
T = TypeVar('T') |
| 5 |
|
| 6 |
def f() -> T: # E: A function returning TypeVar should receive at least one argument containing the same TypeVar |
| 7 |
... |
| 8 |
f() |
| 9 |
|
| 10 |
U = TypeVar('U', bound=int) |
| 11 |
|
| 12 |
def g() -> U: # E: A function returning TypeVar should receive at least one argument containing the same TypeVar \ |
| 13 |
# N: Consider using the upper bound "int" instead |
| 14 |
... |
| 15 |
|
| 16 |
def g2() -> U: # type: ignore[type-var] |
| 17 |
... |
| 18 |
|
| 19 |
V = TypeVar('V', int, str) |
| 20 |
|
| 21 |
def h() -> V: # E: A function returning TypeVar should receive at least one argument containing the same TypeVar |
| 22 |
... |
| 23 |
|
| 24 |
[case testInnerFunctionTypeVar] |
| 25 |
|
| 26 |
from typing import TypeVar |
| 27 |
|
| 28 |
T = TypeVar('T') |
| 29 |
|
| 30 |
def g(a: T) -> T: |
| 31 |
def f() -> T: |
| 32 |
... |
| 33 |
return f() |
| 34 |
|
| 35 |
[case testUnboundIterableOfTypeVars] |
| 36 |
from typing import Iterable, TypeVar |
| 37 |
|
| 38 |
T = TypeVar('T') |
| 39 |
|
| 40 |
def f() -> Iterable[T]: |
| 41 |
... |
| 42 |
f() |
| 43 |
|
| 44 |
[case testBoundTypeVar] |
| 45 |
from typing import TypeVar |
| 46 |
|
| 47 |
T = TypeVar('T') |
| 48 |
|
| 49 |
def f(a: T, b: T, c: int) -> T: |
| 50 |
... |
| 51 |
|
| 52 |
[case testNestedBoundTypeVar] |
| 53 |
from typing import Callable, List, Union, Tuple, TypeVar |
| 54 |
|
| 55 |
T = TypeVar('T') |
| 56 |
|
| 57 |
def f(a: Union[int, T], b: str) -> T: |
| 58 |
... |
| 59 |
|
| 60 |
def g(a: Callable[..., T], b: str) -> T: |
| 61 |
... |
| 62 |
|
| 63 |
def h(a: List[Union[Callable[..., T]]]) -> T: |
| 64 |
... |
| 65 |
|
| 66 |
def j(a: List[Union[Callable[..., Tuple[T, T]], int]]) -> T: |
| 67 |
... |
| 68 |
[builtins fixtures/tuple.pyi] |
| 69 |
|
| 70 |
[case testUnboundedTypevarUnpacking] |
| 71 |
from typing import TypeVar |
| 72 |
T = TypeVar("T") |
| 73 |
def f(t: T) -> None: |
| 74 |
a, *b = t # E: "object" object is not iterable |
| 75 |
|
| 76 |
[case testTypeVarType] |
| 77 |
from typing import Mapping, Type, TypeVar, Union |
| 78 |
T = TypeVar("T") |
| 79 |
|
| 80 |
class A: ... |
| 81 |
class B: ... |
| 82 |
|
| 83 |
lookup_table: Mapping[str, Type[Union[A,B]]] |
| 84 |
def load(lookup_table: Mapping[str, Type[T]], lookup_key: str) -> T: |
| 85 |
... |
| 86 |
reveal_type(load(lookup_table, "a")) # N: Revealed type is "__main__.A | __main__.B" |
| 87 |
|
| 88 |
lookup_table_a: Mapping[str, Type[A]] |
| 89 |
def load2(lookup_table: Mapping[str, Type[Union[T, int]]], lookup_key: str) -> T: |
| 90 |
... |
| 91 |
reveal_type(load2(lookup_table_a, "a")) # N: Revealed type is "__main__.A" |
| 92 |
|
| 93 |
[builtins fixtures/tuple.pyi] |
| 94 |
|
| 95 |
[case testTypeVarTypeAssignment] |
| 96 |
# Adapted from https://github.com/python/mypy/issues/12115 |
| 97 |
from typing import TypeVar, Type, Callable, Union, Any |
| 98 |
|
| 99 |
t1: Type[bool] = bool |
| 100 |
t2: Union[Type[bool], Type[str]] = bool |
| 101 |
|
| 102 |
T1 = TypeVar("T1", bound=Union[bool, str]) |
| 103 |
def foo1(t: Type[T1]) -> None: ... |
| 104 |
foo1(t1) |
| 105 |
foo1(t2) |
| 106 |
|
| 107 |
T2 = TypeVar("T2", bool, str) |
| 108 |
def foo2(t: Type[T2]) -> None: ... |
| 109 |
foo2(t1) |
| 110 |
# Rejected correctly: T2 cannot be Union[bool, str] |
| 111 |
foo2(t2) # E: Value of type variable "T2" of "foo2" cannot be "bool | str" |
| 112 |
|
| 113 |
T3 = TypeVar("T3") |
| 114 |
def foo3(t: Type[T3]) -> None: ... |
| 115 |
foo3(t1) |
| 116 |
foo3(t2) |
| 117 |
|
| 118 |
def foo4(t: Type[Union[bool, str]]) -> None: ... |
| 119 |
foo4(t1) |
| 120 |
foo4(t2) |
| 121 |
[builtins fixtures/tuple.pyi] |