10000 Parsing of Walrus Operator · Issue #8053 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content
Parsing of Walrus Operator  #8053
Closed
Closed
@rvanlaar

Description

@rvanlaar

Mypy doesn't seem to handle variable type changes that happen with a walrus operator.

What happens here is: the walrus operator makes it a boolean:
while i:= update(i) > 0.

This is what needs to happen to check for i > 0 and i still being an int.
while (i:= update(i)) > 0 .

The following python code doesn't give any errors with mypy 0.750:

def walrus1(i: int) -> int:
    """Actually returns bool"""
    while i:= i > 0:
       return i 
    return i

def walrus2(i: int) -> int:
    """Works correctly."""
    while (i:= i) > 0:
        return i
    return i

assert walrus1(5) == True
assert walrus1(0) == False
assert walrus2(5) == 5
assert walrus2(0) == 0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0