summaryrefslogtreecommitdiff
path: root/tests/functional/a/assignment/assignment_expression.py
blob: bf6f8ebf0540181b53a479419b8f128dacd09e24 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"""Test assignment expressions"""
# pylint: disable=missing-docstring,unused-argument,unused-import,invalid-name
# pylint: disable=blacklisted-name,unused-variable,pointless-statement,unused-variable
# pylint: disable=unnecessary-lambda-assignment

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"]


# 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,
)


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


# https://github.com/pylint-dev/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/pylint-dev/pylint/issues/3865
if (c := lambda: 2) and c():
    print("ok")

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


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

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


# https://github.com/pylint-dev/pylint/issues/3763
foo if (foo := 3 - 2) > 0 else 0


# https://github.com/pylint-dev/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}'
)


# https://github.com/pylint-dev/pylint/issues/4301
def func2():
    return f'The number {(count := 4)} ' \
           f'is equal to {count}'


# https://github.com/pylint-dev/pylint/issues/4828
def func3():
    return bar if (bar := "") else ""


# Lambda and IfExp
def func4():
    l = lambda x: y if (y := x) else None


# Crash related to assignment expression in nested if statements
# See https://github.com/pylint-dev/pylint/issues/5178
def func5(val):
    variable = None

    if val == 1:
        variable = "value"
        if variable := "value":
            pass

    elif val == 2:
        variable = "value_two"
        variable = "value_two"

    return variable