Skip to content

Type narrowing of global variable does not transfer into function #3149

@FlorianLindinger

Description

@FlorianLindinger

Describe the Bug

I am not sure if it is intended and could not find similar issue reports:

When I type-narrow a global variable outside a function, pyrefly does not remember this narrowing inside the function. Note: For other scopes like for-loops it remembers it.

global_var: str|None = None

if global_var is None:
    global_var = ""

lis_1:list[str]=[global_var] # no error in pyrefly

if True:
    lis_2:list[str]=[global_var] # no error in pyrefly

def fun():
    lis_3:list[str]=[global_var] # pyrefly warning: `list[str | None]` is not assignable to `list[str]`

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeSIA5lLtqlAPoBuqATogARwAurAPgDlc6GBwC8HISIA66WRDAdqteszYcIcScJiJZHA0pp1GLVuI7SQV2bKiaGARkT2eAbR6sAumLfKTat4cAMQc6LgcMKysuOYQ6BxkrDBgUKS26AocACqsAK66%2BoauDABMLprcHrw%2BfsaqZl4hYRFRMXEJSSlpGZgpHGB56AAUAJR6CcUOAMwV7p61-g1sTaFdqaQcAO5smeiUnAAGrlWeHHzaIl6HGlrh3ByocHAQlOio2LAc3BHHldXeQ4ZEAAGhA6zShB%2BAFsoBRQgAFUjJDZcDA4AgcADGwkglDyrFQ3AgwkIslCAGUYKIABbcbjEOCIAD0zIhpEIsUozJg6GZmFwWLgzJx6DxBKJJL5A1ijxY0A%2BX1F4sJxOEHFwxDV6DgZPQZG4NOEAFomFEXuqJFZpoRHKUbOg3G1YnAvLIhhBocRYtwYJhjZgIMkscSzRYAOSvcLJcOye7G5IARzyQb9xoA1jBSMbUFisfAtBJwztWOhY%2BgQABfMG50MwABi0BgFDQWDwRDIVaAA

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    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