summaryrefslogtreecommitdiff
path: root/tests/functional/p/pattern_matching.py
blob: 4a5884812be223c87e5757c8c6d8d376c3ad7679 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# pylint: disable=missing-docstring,invalid-name,too-few-public-methods

class Point2D:
    __match_args__ = ("x", "y")

    def __init__(self, x, y):
        self.x = x
        self.y = y


# Don't emit false-positive 'unused-variable' and 'undefined-variable'
var = 42
match var:
    case [*rest1]:
        print(rest1)
    case {**rest2}:
        print(rest2)
    case Point2D(0, a):
        print(a)
    case Point2D(x=0, y=b) as new_point:
        print(b)
        print(new_point)
    case new_var:
        print(new_var)


# Test inference of variables assigned in patterns doesn't crash pylint
var = 42
match var:
    case (1, *rest3):
        if rest3 != 2:
            pass
    case {1: _, **rest4}:
        if rest4 != 2:
            pass
    case c:
        if c != 2:
            pass