diff options
| author | ostr00000 <ostr00000@gmail.com> | 2023-02-13 22:50:13 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-02-13 22:50:13 +0100 |
| commit | ade4c68d86f30d58d35e3eaf4c530af780bbf7e2 (patch) | |
| tree | f8228e8d87ca9039e86609147a7d89c2dce0fbab /astroid | |
| parent | e4f594fbd78c8014d921c4feb3d53e5fba509d61 (diff) | |
| download | astroid-git-ade4c68d86f30d58d35e3eaf4c530af780bbf7e2.tar.gz | |
Fix `are_exclusive` function for walrus operator (#2023)
Diffstat (limited to 'astroid')
| -rw-r--r-- | astroid/nodes/node_classes.py | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/astroid/nodes/node_classes.py b/astroid/nodes/node_classes.py index c47cf567..4611d86d 100644 --- a/astroid/nodes/node_classes.py +++ b/astroid/nodes/node_classes.py @@ -137,10 +137,14 @@ def are_exclusive(stmt1, stmt2, exceptions: list[str] | None = None) -> bool: # if the common parent is a If or TryExcept statement, look if # nodes are in exclusive branches if isinstance(node, If) and exceptions is None: - if ( - node.locate_child(previous)[1] - is not node.locate_child(children[node])[1] - ): + c2attr, c2node = node.locate_child(previous) + c1attr, c1node = node.locate_child(children[node]) + if "test" in (c1attr, c2attr): + # If any node is `If.test`, then it must be inclusive with + # the other node (`If.body` and `If.orelse`) + return False + if c1attr != c2attr: + # different `If` branches (`If.body` and `If.orelse`) return True elif isinstance(node, TryExcept): c2attr, c2node = node.locate_child(previous) |
