Skip to content

Rethink public types of symbols #210

Closed
astral-sh/ruff
#18750
@dcreager

Description

@dcreager

astral-sh/ruff#15676 currently has the following mdtest with incorrect results:

def top_level_return(cond1: bool, cond2: bool):
    x = 1

    def g():
        reveal_type(x)  # revealed: Unknown | Literal[1, 2, 3]

    if cond1:
        if cond2:
            x = 2
        else:
            x = 3
    return

def return_from_if(cond1: bool, cond2: bool):
    x = 1

    def g():
        reveal_type(x)  # revealed: Unknown | Literal[1]

    if cond1:
        if cond2:
            x = 2
        else:
            x = 3
        return

def return_from_nested_if(cond1: bool, cond2: bool):
    x = 1

    def g():
        reveal_type(x)  # revealed: Unknown | Literal[1, 3]

    if cond1:
        if cond2:
            x = 2
            return
        else:
            x = 3

We should determine how we want to handle the assignments in this scope.

One possibility (which mimics our current behavior for module-level scopes) would be to reveal Literal[1, 2, 3] for all three. (This would require changes to our terminal statement support: we currently purposefully resolve the x use in the nested function relative to the end of f's scope, but the return statements are preventing some of the bindings from being visible there.)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions