facebook / facebook/pyrefly

variable is treated as possibly being `None` when there's no code path that can set it to `None`

Open
#3,503 8 comments 0 reactions 0 assignees View on GitHub
needs-discussion typechecking
Dominant language
Rust
Stars
7k
Forks
519
PR merge metrics
No merged PRs in 30d

Description

### Describe the Bug

```python
from typing import *

def test() -> list[str]:
args: Optional[list[str]] = []
return args

```
It seems that when a variable is declared as `Optional`, Pyrefly doesn't pay attention to whether the initial assignation is `None` or a value and just assumes on the `return args` statement that `args` might be `None` even though no code path exists under which it could reach the `return` statement with a `None` value.

### Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeS4ATrgLYAEALqcROgOZ0Q3G6UN0AqADroRmGGEbwGACgCUdALQA%2BOlAhwGAbU2UAuohF1jdVJTZxEdAPLEGEXOlRQt6zTob69dALx0tekYmlDAMAK6U6KbmcEHGIAA0IGQhYFCkhAy0UBQAxHQACqSp6XRoWHj4dADGjpBsEaj2joQi%2BQDKMDB0ABYMDMSWAPRDKRLphLxsQzDoQ5i41XBDtej1jc1zdGC8pgBuqNCo2LA1dRANlE0OUbh2N3Ct6GQMPY6KezCUcDe%2BdEIgADMhAAjAAmAEiLRfajfQLoMLoLg8PgwTCKTAQELVeyfP4Acgu6F4MHxImJDEUIQAjmEsWjFABrGCkRSoarVeBwAkAdzM6DJ6BAAF8kuzcTAAGLQGAUco4AgkcjCoA

### (Only applicable for extension issues) IDE Information

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.