summaryrefslogtreecommitdiff
path: root/tests/functional/n/name/name_styles.py
blob: 86f395b524a9b3937efb0dcd7b041675b857587f (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""Test for the invalid-name warning."""
# pylint: disable=useless-object-inheritance, unnecessary-pass, unnecessary-comprehension, unused-private-member, unnecessary-lambda-assignment
from __future__ import print_function
import abc
import collections
import typing
from enum import Enum
from typing import ClassVar

GOOD_CONST_NAME = ''
bad_const_name = 0  # [invalid-name]


def BADFUNCTION_name():  # [invalid-name]
    """Bad function name."""
    BAD_LOCAL_VAR = 1  # [invalid-name]
    print(BAD_LOCAL_VAR)


def func_bad_argname(NOT_GOOD):  # [invalid-name]
    """Function with a badly named argument."""
    return NOT_GOOD


def no_nested_args(arg1, arg21, arg22):
    """Well-formed function."""
    print(arg1, arg21, arg22)


class bad_class_name(object):  # [invalid-name]
    """Class with a bad name."""


class CorrectClassName(object):
    """Class with a good name."""

    def __init__(self):
        self._good_private_name = 10
        self.__good_real_private_name = 11
        self.good_attribute_name = 12
        self._Bad_AtTR_name = None  # [invalid-name]
        self.Bad_PUBLIC_name = None  # [invalid-name]

    zz = 'Why Was It Bad Class Attribute?'
    GOOD_CLASS_ATTR = 'Good Class Attribute'

    def BadMethodName(self):  # [invalid-name]
        """A Method with a bad name."""

    def good_method_name(self):
        """A method with a good name."""

    def __DunDER_IS_not_free_for_all__(self):  # [invalid-name]
        """Another badly named method."""


class DerivedFromCorrect(CorrectClassName):
    """A derived class with an invalid inherited members.

    Derived attributes and methods with invalid names do not trigger warnings.
    """
    zz = 'Now a good class attribute'

    def __init__(self):
        super().__init__()
        self._Bad_AtTR_name = None  # Ignored

    def BadMethodName(self):
        """Ignored since the method is in the interface."""


V = [WHAT_Ever_inListComp for WHAT_Ever_inListComp in GOOD_CONST_NAME]

def class_builder():
    """Function returning a class object."""

    class EmbeddedClass(object):
        """Useless class."""

    return EmbeddedClass

# +1:[invalid-name]
BAD_NAME_FOR_CLASS = collections.namedtuple('Named', ['tuple'])
NEXT_BAD_NAME_FOR_CLASS = class_builder()  # [invalid-name]

GoodName = collections.namedtuple('Named', ['tuple'])
ToplevelClass = class_builder()

# Aliases for classes have the same name constraints.
AlsoCorrect = CorrectClassName
NOT_CORRECT = CorrectClassName  # [invalid-name]


def test_globals():
    """Names in global statements are also checked."""
    global NOT_CORRECT
    global AlsoCorrect  # [invalid-name]
    NOT_CORRECT = 1
    AlsoCorrect = 2


class FooClass(object):
    """A test case for property names.

    Since by default, the regex for attributes is the same as the one
    for method names, we check the warning messages to contain the
    string 'attribute'.
    """
    @property
    def PROPERTY_NAME(self):  # [invalid-name]
        """Ignored."""
        pass

    @property
    @abc.abstractmethod
    def ABSTRACT_PROPERTY_NAME(self):  # [invalid-name]
        """Ignored."""
        pass

    @PROPERTY_NAME.setter
    def PROPERTY_NAME_SETTER(self):  # [invalid-name]
        """Ignored."""
        pass

    def _nice_and_long_descriptive_private_method_name(self):
        """private method with long name"""
        pass


def good_public_function_name(good_arg_name):
    """This is a perfect public function"""
    good_variable_name = 1
    return good_variable_name + good_arg_name


def _private_scope_function_with_long_descriptive_name():
    """Private scope function are cool with long descriptive names"""
    return 12

LONG_CONSTANT_NAME_IN_PUBLIC_SCOPE_ARE_OKAY = True
# We don't emit for non-const nodes
good_name_for_funcs = lambda: None
good_name_for_lists = [1, 2, 3]

class _AnExceptionalExceptionThatOccursVeryVeryRarely(Exception):
    """A very exceptional exception with a nice descriptive name"""
    pass

class FooEnum(Enum):
    """A test case for enum names."""
    GOOD_ENUM_NAME = 1
    bad_enum_name = 2  # [invalid-name]

class Bar:
    """Class with class variables annotated with ClassVar."""
    CLASS_CONST: ClassVar[int] = 42
    CLASS_CONST2: ClassVar = "const"
    variable: ClassVar[str] = "invalid name"
    CLASS_CONST3: typing.ClassVar
    variable2: typing.ClassVar[int]