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

PEP 572: Type not narrowed correctly in assignment #7316

Closed
JelleZijlstra opened this issue Aug 10, 2019 · 1 comment · Fixed by #8258
Closed

PEP 572: Type not narrowed correctly in assignment #7316

JelleZijlstra opened this issue Aug 10, 2019 · 1 comment · Fixed by #8258
Labels
bug mypy got something wrong priority-0-high topic-pep-572 PEP 572 (walrus operator)

Comments

@JelleZijlstra
Copy link
Member

Example from @gvanrossum in #6899:

from typing import Optional

class C:
    def foo(self):
        pass

def good(b: Optional[C]) -> None:
    a = b
    if a:
        a.foo()

def bad(b: Optional[C]) -> None:
    if a := b:
        a.foo()  # E:  Item "None" of "Optional[C]" has no attribute "foo"
@ilevkivskyi
Copy link
Member

This is actually pretty important to support, this example:

discount = 0.0
if (mo := re.search(r'(\d+)% discount', advertisement)):
    discount = float(mo.group(1)) / 100.0

is the flagship example taken from Python 3.8 What's New, and is actually the prime use case for assignment expressions in general. Raising priority to high.

hauntsaninja pushed a commit to hauntsaninja/mypy that referenced this issue Jan 8, 2020
davep added a commit to davep/journeys.py that referenced this issue Jan 30, 2020
Note that, as of the time of writing, straightforward type checks are fine,
but strict checking still fails due to
python/mypy#7316
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong priority-0-high topic-pep-572 PEP 572 (walrus operator)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants