Skip to content

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

@Keymasterer44

Description

@Keymasterer44

Describe the Bug

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-discussionAn issue where it's not clear whether there is a bug or we are behaving as expected.typechecking

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions