Updated "mypy comparison" documentation based on recent improvements in mypy. It's now able to handle higher-order functions.

This commit is contained in:
Eric Traut 2024-01-16 00:32:52 -08:00
parent b21cbdc782
commit cab9f886de

View File

@ -318,30 +318,6 @@ def func2(x: list[int], y: list[str] | int):
reveal_type(v2) # pyright: "list[int | str]" ("list[list[str] | int]" is also a valid answer)
```
#### Constraint Solver: Higher-order Functions
If a generic higher-order function is passed another generic callable as an argument, pyright is able to solve the type variables for both the target function and the argument. Mypy isnt able to handle higher-order functions.
```python
def identity(val: T) -> T:
return val
def higher_order1(cb: Callable[[S], T], arg: S) -> T:
return cb(arg)
v1 = higher_order1(identity, 1.0) # mypy generates an error
reveal_type(v1) # mypy: T, pyright: float
def higher_order2(cb: Callable[P, R], *args: P.args, **kwargs: P.kwargs) -> R:
return cb(*args, **kwargs)
v2 = higher_order2(identity, "") # mypy generates an error
reveal_type(v2) # mypy: T, pyright: str
```
### Value-Constrained Type Variables
When mypy analyzes a class or function that has in-scope value-constrained TypeVars, it analyzes the class or function multiple times, once for each constraint. This can produce multiple errors.