Skip to content

Commit 80387ea

Browse files
committed
[red-knot] Add tests for callable equivalence
1 parent 58d5fe9 commit 80387ea

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

crates/red_knot_python_semantic/resources/mdtest/type_properties/is_equivalent_to.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,51 @@ class R: ...
118118
static_assert(is_equivalent_to(Intersection[tuple[P | Q], R], Intersection[tuple[Q | P], R]))
119119
```
120120

121+
## Callable
122+
123+
```py
124+
from typing import Callable
125+
from knot_extensions import CallableTypeFromFunction, is_equivalent_to, static_assert
126+
127+
def c1() -> None: ...
128+
129+
static_assert(is_equivalent_to(CallableTypeFromFunction[c1], Callable[[], None]))
130+
```
131+
132+
Two callable types with the same signature but different parameter names are not equivalent.
133+
134+
```py
135+
def f1(a: int) -> int:
136+
return 1
137+
138+
def f2(b: int) -> int:
139+
return 1
140+
141+
static_assert(not is_equivalent_to(CallableTypeFromFunction[f1], CallableTypeFromFunction[f2]))
142+
```
143+
144+
Neither when only one of the callable types has parameter names.
145+
146+
```py
147+
static_assert(not is_equivalent_to(CallableTypeFromFunction[f1], Callable[[int], int]))
148+
```
149+
150+
If the default values of the same parameters are different, the types are not equivalent.
151+
152+
```py
153+
def f3(a: int = 1) -> int:
154+
return 1
155+
156+
def f4(a: int = 2) -> int:
157+
return 1
158+
159+
static_assert(not is_equivalent_to(CallableTypeFromFunction[f3], CallableTypeFromFunction[f4]))
160+
```
161+
162+
Neither when only one of the callable types has default values.
163+
164+
```py
165+
static_assert(not is_equivalent_to(CallableTypeFromFunction[f1], CallableTypeFromFunction[f3]))
166+
```
167+
121168
[the equivalence relation]: https://typing.readthedocs.io/en/latest/spec/glossary.html#term-equivalent

0 commit comments

Comments
 (0)