Skip to content

PEP 572: Type not narrowed correctly in assignment #7316

Closed

Description

@JelleZijlstra

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"

Activity

  1. ilevkivskyi commented on Nov 22, 2019

    @ilevkivskyi
    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.

  2. added a commit that references this issue on Jan 8, 2020
    5622d2c
  3. added a commit that references this issue on Jan 8, 2020
    33fc8a3
  4. added a commit that references this issue on Jan 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions