Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type not narrowed in walrus assignment inside match #14913

Closed
bersbersbers opened this issue Mar 16, 2023 · 1 comment
Closed

Type not narrowed in walrus assignment inside match #14913

bersbersbers opened this issue Mar 16, 2023 · 1 comment
Labels
bug mypy got something wrong topic-match-statement Python 3.10's match statement topic-pep-572 PEP 572 (walrus operator) topic-type-narrowing Conditional type narrowing / binder

Comments

@bersbersbers
Copy link

Bug Report

The type of var is not narrowed in match statements including a walrus assignment, such as

match (var := fun()):
    case type():
        ...

To Reproduce

def get_int_or_float() -> int | float:
    return int()

def print_int(i: int) -> None:
    print(i)

# mypy works correctly:
first_int_or_float = get_int_or_float()
match first_int_or_float:
    case int():
        # this use is correct; mypy is correct
        print_int(first_int_or_float)
    case float():
        # this use is incorrect; mypy is correct
        print_int(first_int_or_float)

# mypy fails:
match (second_int_or_float := get_int_or_float()):
    # mypy incorrectly does not narrow type of second_int_or_float
    case int():
        print_int(second_int_or_float)
    case float():
        print_int(second_int_or_float)

https://mypy-play.net/?mypy=master&python=3.10&gist=1a1347df9d076755e9dd6a947c97839a

Expected Behavior

main.py:15: error: Argument 1 to "print_int" has incompatible type "float"; expected "int"  [arg-type]
main.py:23: error: Argument 1 to "print_int" has incompatible type "float"; expected "int"  [arg-type]
Found 2 errors in 1 file (checked 1 source file)

Actual Behavior


main.py:15: error: Argument 1 to "print_int" has incompatible type "float"; expected "int"  [arg-type]
main.py:21: error: Argument 1 to "print_int" has incompatible type "Union[int, float]"; expected "int"  [arg-type]
main.py:23: error: Argument 1 to "print_int" has incompatible type "Union[int, float]"; expected "int"  [arg-type]
main.py:23: error: Name "second_int_or_float" is used before definition  [used-before-def]
Found 4 errors in 1 file (checked 1 source file)

Your Environment

mypy master on Python 3.10, see Playground

@bersbersbers bersbersbers added the bug mypy got something wrong label Mar 16, 2023
@AlexWaygood AlexWaygood added topic-match-statement Python 3.10's match statement topic-pep-572 PEP 572 (walrus operator) topic-type-narrowing Conditional type narrowing / binder labels Mar 17, 2023
@AlexWaygood
Copy link
Member

This is already fixed on mypy master; it was fixed by #14844. The fix will be included as part of the next mypy release, mypy 1.2.

Your Environment

mypy master on Python 3.10, see Playground

Note that the "mypy master" option on mypy playground is often several weeks behind the latest commit on mypy's actual master branch. (Mypy playground is a third-party project not developed by the mypy team.)

@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Mar 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-match-statement Python 3.10's match statement topic-pep-572 PEP 572 (walrus operator) topic-type-narrowing Conditional type narrowing / binder
Projects
None yet
Development

No branches or pull requests

2 participants