You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: crates/red_knot_python_semantic/resources/mdtest/generics/functions.md
+33Lines changed: 33 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -71,6 +71,39 @@ def f[T](x: list[T]) -> T:
71
71
reveal_type(f([1.0, 2.0])) # revealed: Unknown
72
72
```
73
73
74
+
## Inferring a bound typevar
75
+
76
+
<!-- snapshot-diagnostics -->
77
+
78
+
```py
79
+
from typing_extensions import reveal_type
80
+
81
+
def f[T: int](x: T) -> T:
82
+
return x
83
+
84
+
reveal_type(f(1)) # revealed: Literal[1]
85
+
reveal_type(f(True)) # revealed: Literal[True]
86
+
# error: [invalid-argument-type] "Argument to this function is incorrect: Argument type `Literal["string"]` does not satisfy upper bound of type variable `T`"
87
+
reveal_type(f("string")) # revealed: Unknown
88
+
```
89
+
90
+
## Inferring a constrained typevar
91
+
92
+
<!-- snapshot-diagnostics -->
93
+
94
+
```py
95
+
from typing_extensions import reveal_type
96
+
97
+
def f[T: (int, None)](x: T) -> T:
98
+
return x
99
+
100
+
reveal_type(f(1)) # revealed: int
101
+
reveal_type(f(True)) # revealed: int
102
+
reveal_type(f(None)) # revealed: None
103
+
# error: [invalid-argument-type] "Argument to this function is incorrect: Argument type `Literal["string"]` does not satisfy constraints of type variable `T`"
104
+
reveal_type(f("string")) # revealed: Unknown
105
+
```
106
+
74
107
## Typevar constraints
75
108
76
109
If a type parameter has an upper bound, that upper bound constrains which types can be used for that
8 | # error: [invalid-argument-type] "Argument to this function is incorrect: Argument type `Literal["string"]` does not satisfy upper bound of type variable `T`"
9 | # error: [invalid-argument-type] "Argument to this function is incorrect: Argument type `Literal["string"]` does not satisfy constraints of type variable `T`"
24
+
10 | reveal_type(f("string")) # revealed: Unknown
25
+
```
26
+
27
+
# Diagnostics
28
+
29
+
```
30
+
info: revealed-type: Revealed type
31
+
--> src/mdtest_snippet.py:6:1
32
+
|
33
+
4 | return x
34
+
5 |
35
+
6 | reveal_type(f(1)) # revealed: int
36
+
| ^^^^^^^^^^^^^^^^^ `int`
37
+
7 | reveal_type(f(True)) # revealed: int
38
+
8 | reveal_type(f(None)) # revealed: None
39
+
|
40
+
41
+
```
42
+
43
+
```
44
+
info: revealed-type: Revealed type
45
+
--> src/mdtest_snippet.py:7:1
46
+
|
47
+
6 | reveal_type(f(1)) # revealed: int
48
+
7 | reveal_type(f(True)) # revealed: int
49
+
| ^^^^^^^^^^^^^^^^^^^^ `int`
50
+
8 | reveal_type(f(None)) # revealed: None
51
+
9 | # error: [invalid-argument-type] "Argument to this function is incorrect: Argument type `Literal["string"]` does not satisfy constra...
52
+
|
53
+
54
+
```
55
+
56
+
```
57
+
info: revealed-type: Revealed type
58
+
--> src/mdtest_snippet.py:8:1
59
+
|
60
+
6 | reveal_type(f(1)) # revealed: int
61
+
7 | reveal_type(f(True)) # revealed: int
62
+
8 | reveal_type(f(None)) # revealed: None
63
+
| ^^^^^^^^^^^^^^^^^^^^ `None`
64
+
9 | # error: [invalid-argument-type] "Argument to this function is incorrect: Argument type `Literal["string"]` does not satisfy constra...
65
+
10 | reveal_type(f("string")) # revealed: Unknown
66
+
|
67
+
68
+
```
69
+
70
+
```
71
+
error: lint:invalid-argument-type: Argument to this function is incorrect
72
+
--> src/mdtest_snippet.py:10:15
73
+
|
74
+
8 | reveal_type(f(None)) # revealed: None
75
+
9 | # error: [invalid-argument-type] "Argument to this function is incorrect: Argument type `Literal["string"]` does not satisfy constra...
76
+
10 | reveal_type(f("string")) # revealed: Unknown
77
+
| ^^^^^^^^ Argument type `Literal["string"]` does not satisfy constraints of type variable `T`
78
+
|
79
+
info: Type variable defined here
80
+
--> src/mdtest_snippet.py:3:7
81
+
|
82
+
1 | from typing_extensions import reveal_type
83
+
2 |
84
+
3 | def f[T: (int, None)](x: T) -> T:
85
+
| ^^^^^^^^^^^^^^
86
+
4 | return x
87
+
|
88
+
89
+
```
90
+
91
+
```
92
+
info: revealed-type: Revealed type
93
+
--> src/mdtest_snippet.py:10:1
94
+
|
95
+
8 | reveal_type(f(None)) # revealed: None
96
+
9 | # error: [invalid-argument-type] "Argument to this function is incorrect: Argument type `Literal["string"]` does not satisfy constra...
0 commit comments