summaryrefslogtreecommitdiff
path: root/tests/functional/a/assignment_expression.py
blob: aa6a27874fbde8715a3833408ee3c0f3426bbc68 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""Test assignment expressions"""
# pylint: disable=missing-docstring,unused-argument,unused-import,invalid-name,blacklisted-name,unused-variable
import re

if (a := True):
    x = a
else:
    x = False

x = b if (b := True) else False
x2: bool = b2 if (b2 := True) else False
x3 = 0
x3 += b3 if (b3 := 4) else 6

a = ["a   ", "b   ", "c   "]
c = [text for el in a if (text := el.strip()) == "b"]


# https://github.com/PyCQA/pylint/issues/3347
s = 'foo' if (fval := lambda: 1) is None else fval


# https://github.com/PyCQA/pylint/issues/3953
assert (n := 2) == 1, f"Expected 1, but got {n}"
dict({1: (o := 2)}, data=o)
assert (p := 2) == 1, \
    p

FOO_PATT = re.compile("")
foo = m.group("foo") if (m := FOO_PATT.match("")) else False


# https://github.com/PyCQA/pylint/issues/3865
if (c := lambda: 2) and c():
    print("ok")

def func():
    print((d := lambda: 2) and d)


# https://github.com/PyCQA/pylint/issues/3275
values = (
    e := 1,
    f := e,
)
print(values)

function = lambda: (
    h := 1,
    i := h,
)
print(function())


# https://github.com/PyCQA/pylint/issues/3763
foo if (foo := 3 - 2) > 0 else 0  # [pointless-statement]


# https://github.com/PyCQA/pylint/issues/4238
l1 = f'The number {(count1 := 4)} ' \
     f'is equal to {count1}'
l2: str = (
    f'The number {(count2 := 4)} '
    f'is equal to {count2}'
)
l3 = "Hello "
l3 += (
    f'The number {(count3 := 4)} '
    f'is equal to {count3}'
)


# check wrong usage
assert err_a, (err_a := 2)  # [used-before-assignment]
print(err_b and (err_b := 2))  # [used-before-assignment]
values = (
    err_c := err_d,  # [used-before-assignment]
    err_d := 2,
)