mirror of
https://github.com/microsoft/pyright.git
synced 2024-09-11 07:55:56 +03:00
Added documentation to mypy comparison that talks about narrowing for captured variables.
This commit is contained in:
parent
d2a6b79bff
commit
ff134e6467
@ -177,6 +177,25 @@ def is_red(color: Color) -> bool:
|
|||||||
# mypy reports error: Missing return statement
|
# mypy reports error: Missing return statement
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Narrowing for Captured Variables
|
||||||
|
|
||||||
|
If a variable’s type is narrowed in an outer scope and the variable is subsequently used within an inner-scoped function or lambda, mypy does not retain the narrowed type within the inner scope. Pyright retains the narrowed type if it can determine that the value of the captured variable is not modified on any code path after the inner-scope function or lambda is defined.
|
||||||
|
|
||||||
|
```python
|
||||||
|
def func(val: int | None):
|
||||||
|
if val is not None:
|
||||||
|
|
||||||
|
def inner_1() -> None:
|
||||||
|
reveal_type(val) # pyright: int, mypy: int | None
|
||||||
|
print(val + 1) # mypy produces a false positive error here
|
||||||
|
|
||||||
|
inner_2 = lambda: reveal_type(val) + 1 # pyright: int, mypy: int | None
|
||||||
|
|
||||||
|
inner_1()
|
||||||
|
inner_2()
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### Narrowing Any
|
### Narrowing Any
|
||||||
|
|
||||||
Pyright never narrows `Any` when performing type narrowing for assignments. Mypy is inconsistent about when it applies type narrowing to `Any` type arguments.
|
Pyright never narrows `Any` when performing type narrowing for assignments. Mypy is inconsistent about when it applies type narrowing to `Any` type arguments.
|
||||||
|
Loading…
Reference in New Issue
Block a user