summaryrefslogtreecommitdiff
path: root/tests/functional/g/globals.py
blob: 7f4e3077e9943dd033981908fabe24701aac6935 (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
"""Warnings about global statements and usage of global variables."""
# pylint: disable=invalid-name, redefined-outer-name, missing-function-docstring, missing-class-docstring, import-outside-toplevel, too-few-public-methods

global CSTE  # [global-at-module-level]
print(CSTE)  # [undefined-variable]


RAN_DB_SET = set()
RAN_DB_DICT = {}

CONSTANT = 1
def FUNC():
    pass

class CLASS:
    pass

def fix_contant(value):
    """all this is ok, but try not using global ;)"""
    global CONSTANT  # [global-statement]
    print(CONSTANT)
    CONSTANT = value


def other():
    """global behaviour test"""
    global HOP  # [global-variable-not-assigned]
    print(HOP)  # [undefined-variable]


def define_constant():
    """ok but somevar is not defined at the module scope"""
    global SOMEVAR  # [global-variable-undefined]
    SOMEVAR = 2


def global_with_import():
    """should only warn for global-statement when using `Import` node"""
    global sys  # [global-statement]
    import sys


def global_with_import_from():
    """should only warn for global-statement when using `ImportFrom` node"""
    global namedtuple  # [global-statement]
    from collections import namedtuple


def global_no_assign():
    """Not assigning anything to the global makes 'global' superfluous"""
    global CONSTANT  # [global-variable-not-assigned]
    print(CONSTANT)


def global_del():
    """Deleting the global name prevents `global-variable-not-assigned`"""
    global CONSTANT  # [global-statement]
    print(CONSTANT)
    del CONSTANT


def global_operator_assign():
    """Operator assigns should only throw a global statement error"""
    global CONSTANT  # [global-statement]
    print(CONSTANT)
    CONSTANT += 1


def global_function_assign():
    """Function assigns should only throw a global statement error"""
    global CONSTANT  # [global-statement]

    def CONSTANT():
        pass

    CONSTANT()


def override_func():
    """Overriding a function should only throw a global statement error"""
    global FUNC # [global-statement]

    def FUNC():
        pass

    FUNC()


def override_class():
    """Overriding a class should only throw a global statement error"""
    global CLASS  # [global-statement]

    class CLASS():
        pass

    CLASS()


def init_connection_state(alias):
    """Demonstrate that non-assignment modifications to global objects should emit message."""
    global RAN_DB_SET  # [global-variable-not-assigned]
    global RAN_DB_DICT  # [global-variable-not-assigned]
    RAN_DB_SET.add(alias)
    return RAN_DB_DICT.setdefault("color", "Palomino")


# Prevent emitting `invalid-name` for the line on which `global` is declared
# https://github.com/pylint-dev/pylint/issues/8307

_foo: str = "tomato"
def setup_shared_foo():
    global _foo  # [global-statement]
    _foo = "potato"