Skip to content

Cannot use field twice after an is not None check? #145

@bennn

Description

@bennn

On Python 3.10.5+cinder, Cinder commit 1aeff3259dc

Example program:

class Node:
    def __init__(self) -> None:
        self.wins: int = 0
        self.losses: int = 0
        self.parent: None | Node = None

    def score1(self) -> int:
        n = 0
        if self.parent is not None:
            n += self.parent.wins
        if self.parent is not None:
            n += self.parent.losses
        return n

    def score2(self) -> int:
        n = 0
        if self.parent is not None:
            n += self.parent.losses
            n += self.parent.wins # type error !?
        return n

Expected no type error.

But got a type error inside score2 accessing self.parent.wins:

  File "foo.py", line 20
    n += self.parent.wins
        ^
cinderx.compiler.errors.TypedSyntaxError: Optional[__main__.Node]: 'NoneType' object has no attribute 'wins'

If you swap that line and the previous one, the error is on self.parent.losses instead!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions